|
1
|
import { ChildDocs } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Data Model Repository Reference`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
This section of the documentation provides a reference of the methods generated for data model repositories that are injected into a module's container. |
|
10
|
|
|
11
|
<Note title="Tip"> |
|
12
|
|
|
13
|
Learn more about the data model repository in the [Database Operations](!docs!/learn/fundamentals/modules/db-operations) documentation. |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
## When to Use the Data Model Repository? |
|
18
|
|
|
19
|
If your module has data models, then you should always extend the [service factory](!docs!/learn/fundamentals/modules/service-factory). It generates basic data-management operations for your module's data models. |
|
20
|
|
|
21
|
However, there are some cases where you might need to perform more complex database operations and need more control over the parameters you're passing. |
|
22
|
|
|
23
|
In those cases, you can use the data model repository. |
|
24
|
|
|
25
|
--- |
|
26
|
|
|
27
|
## Data Model Repository Registration Name |
|
28
|
|
|
29
|
When the Medusa application injects a data model repository into a module's container, it formats the registration name by: |
|
30
|
|
|
31
|
- Taking the data model's name that's passed as the first parameter of `model.define` |
|
32
|
- Lower-casing the first character |
|
33
|
- Suffixing the result with `Repository`. |
|
34
|
|
|
35
|
For example: |
|
36
|
|
|
37
|
- `Post` model: `postRepository` |
|
38
|
- `My_Custom` model: `my_CustomRepository` |
|
39
|
|
|
40
|
--- |
|
41
|
|
|
42
|
## Resolve Data Model Repository |
|
43
|
|
|
44
|
To resolve a data model repository from a module's container, pass the data model's name in the first parameter of the module's constructor. |
|
45
|
|
|
46
|
For example: |
|
47
|
|
|
48
|
export const resolveRepoHighlights = [ |
|
49
|
["17", "postRepository", "Resolve the data model repository for the `Post` model."], |
|
50
|
] |
|
51
|
|
|
52
|
```ts highlights={resolveRepoHighlights} |
|
53
|
import { MedusaService } from "@medusajs/framework/utils" |
|
54
|
import { InferTypeOf, DAL } from "@medusajs/framework/types" |
|
55
|
import Post from "./models/post" |
|
56
|
|
|
57
|
type Post = InferTypeOf<typeof Post> |
|
58
|
|
|
59
|
type InjectedDependencies = { |
|
60
|
postRepository: DAL.RepositoryService<Post> |
|
61
|
} |
|
62
|
|
|
63
|
class BlogModuleService extends MedusaService({ |
|
64
|
Post, |
|
65
|
}){ |
|
66
|
protected postRepository_: DAL.RepositoryService<Post> |
|
67
|
|
|
68
|
constructor({ |
|
69
|
postRepository, |
|
70
|
}: InjectedDependencies) { |
|
71
|
super(...arguments) |
|
72
|
this.postRepository_ = postRepository |
|
73
|
} |
|
74
|
} |
|
75
|
|
|
76
|
export default BlogModuleService |
|
77
|
``` |
|
78
|
|
|
79
|
In this example, the `BlogModuleService` class resolves the `postRepository` from its container. |
|
80
|
|
|
81
|
--- |
|
82
|
|
|
83
|
## Methods Reference |
|
84
|
|
|
85
|
Learn how to use the data model repository's methods to perform database operations on the data model. |
|
86
|
|
|
87
|
<ChildDocs showItems={["Methods"]} hideTitle /> |