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.

Tip: Learn more about the data model repository in the Database Operations documentation.

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:

Code
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.

Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break