Data Model Repository Reference
This section of the documentation provides a reference of the methods generated for data model repositories that are injected into a module's container.
When to Use the Data Model Repository?#
If your module has data models, then you should always extend the service factory. It generates basic data-mangement operations for your module's data models.
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.
In those cases, you can use the data model repository.
Data Model Repository Registration Name#
When the Medusa application injects a data model repository into a module's container, it formats the registration name by:
- Taking the data model's name that's passed as the first parameter of
model.define
- Lower-casing the first character
- Suffixing the result with
Repository
.
For example:
Post
model:postRepository
My_Custom
model:my_CustomRepository
Resolve Data Model Repository#
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.
For example:
1import { MedusaService } from "@medusajs/framework/utils"2import { InferTypeOf, DAL } from "@medusajs/framework/types"3import Post from "./models/post"4 5type Post = InferTypeOf<typeof Post>6 7type InjectedDependencies = {8 postRepository: DAL.RepositoryService<Post>9}10 11class BlogModuleService extends MedusaService({12 Post,13}){14 protected postRepository_: DAL.RepositoryService<Post>15 16 constructor({ 17 postRepository18 }: InjectedDependencies) {19 super(...arguments)20 this.postRepository_ = postRepository21 }22}23 24export default BlogModuleService
In this example, the BlogModuleService
class resolves the postRepository
from its container.
Methods Reference#
Learn how to use the data model repository's methods to perform database operations on the data model.