find Method - Data Model Repository Reference

The find method of a data model repository retrieves a list of records.

Note: This reference assumes you've already resolved the data model repository, as explained in the Data Model Repository Reference documentation.

find Parameters#

Loading...

Retrieve List of Records#

To retrieve a list of records matching a set of filters, use the find method of the data model repository:

src/modules/blog/service.ts
1import { 2  InjectTransactionManager,3  MedusaContext,4  MedusaService,5  InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10
11class BlogModuleService extends MedusaService({12  Post,13}){14  // ...15  @InjectTransactionManager()16  protected async doSomething_(17    @MedusaContext() sharedContext?: Context<EntityManager>18  ): Promise<any> {19    const posts = await this.postRepository_.find()20
21    return posts22  }23
24  @InjectManager()25  async doSomething(26    id: string,27    @MedusaContext() sharedContext?: Context<EntityManager>28  ): Promise<any> {29    return await this.doSomething_(id, sharedContext)30  }31}

Returns#

By default, the method returns an array of the first 15 records of the data model.


Filter Records#

You can filter the retrieved records by passing filters as a first parameter of the find method:

src/modules/blog/service.ts
1import { 2  InjectTransactionManager,3  MedusaContext,4  MedusaService,5  InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10
11class BlogModuleService extends MedusaService({12  Post,13}){14  // ...15  @InjectTransactionManager()16  protected async doSomething_(17    id: string[],18    @MedusaContext() sharedContext?: Context<EntityManager>19  ): Promise<any> {20    const posts = await this.postRepository_.find({21      where: {22        id,23      },24    })25
26    return posts27  }28
29  @InjectManager()30  async doSomething(31    id: string,32    @MedusaContext() sharedContext?: Context<EntityManager>33  ): Promise<any> {34    return await this.doSomething_(id, sharedContext)35  }36}

Parameters#

The first object parameter is an object that has a where property used to apply filters. where's value is an object whose keys are the property names of the data model, and values are the values to filter by.

In the example above, you filter the retrieved posts by their id property.

Note: Refer to the Filtering reference for more information on how to filter records.

Returns#

The method returns an array of the first 15 records matching the filters.


Retrieve Relations#

Note: This applies to relations between data models of the same module. To retrieve linked records of different modules, use Query.
src/modules/blog/service.ts
1import { 2  InjectTransactionManager,3  MedusaContext,4  MedusaService,5  InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10
11class BlogModuleService extends MedusaService({12  Post,13}){14  // ...15  @InjectTransactionManager()16  protected async doSomething_(17    id: string[],18    @MedusaContext() sharedContext?: Context<EntityManager>19  ): Promise<any> {20    const posts = await this.postRepository_.find({21      where: {22        id,23      },24      options: {25        populate: ["author"],26      },27    })28
29    return posts30  }31
32  @InjectManager()33  async doSomething(34    id: string,35    @MedusaContext() sharedContext?: Context<EntityManager>36  ): Promise<any> {37    return await this.doSomething_(id, sharedContext)38  }39}

Parameters#

The first object parameter of find accepts an option property, whose value is an object of retrieval options.

It accepts a populate property, whose value is an array of relation names to retrieve.

In the example above, you retrieve the author relation of the posts.

Returns#

The method returns an array of the first 15 records. Each record will have the specified relations populated:

Example
1[2  {3    "id": "1",4    "author": {5      "id": "1",6      "name": "John Doe",7      // ...8    },9    // ...10  }11]

Select Properties#

src/modules/blog/service.ts
1import { 2  InjectTransactionManager,3  MedusaContext,4  MedusaService,5  InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10
11class BlogModuleService extends MedusaService({12  Post,13}){14  // ...15  @InjectTransactionManager()16  protected async doSomething_(17    id: string,18    @MedusaContext() sharedContext?: Context<EntityManager>19  ): Promise<any> {20    const posts = await this.postRepository_.find({21      where: {22        id,23      },24      options: {25        fields: ["title"],26      },27    })28
29    return posts30  }31
32  @InjectManager()33  async doSomething(34    id: string,35    @MedusaContext() sharedContext?: Context<EntityManager>36  ): Promise<any> {37    return await this.doSomething_(id, sharedContext)38  }39}

Parameters#

By default, retrieved records have all their properties. The first object parameter of find accepts in its options property a fields property, whose value is an array of property names to retrieve.

In the example above, you retrieve only the title property of the posts.

Returns#

The method returns an array of the first 15 records. Each record will have only the specified properties:

Example
1[2  {3    "id": "1",4    "title": "My first post"5  }6]

Paginate Relations#

src/modules/blog/service.ts
1import { 2  InjectTransactionManager,3  MedusaContext,4  MedusaService,5  InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10
11class BlogModuleService extends MedusaService({12  Post,13}){14  // ...15  @InjectTransactionManager()16  protected async doSomething_(17    id: string,18    @MedusaContext() sharedContext?: Context<EntityManager>19  ): Promise<any> {20    const posts = await this.postRepository_.find({21      where: {22        id,23      },24      options: {25        limit: 10,26        offset: 10,27      },28    })29
30    return posts31  }32
33  @InjectManager()34  async doSomething(35    id: string,36    @MedusaContext() sharedContext?: Context<EntityManager>37  ): Promise<any> {38    return await this.doSomething_(id, sharedContext)39  }40}

Parameters#

By default, the find method retrieves the first 15 records.

To change pagination configurations, you can pass to the first object parameter the options property, whose value is an object that accepts the following properties for pagination:

  • limit: a number indicating how many records to retrieve. By default, it's 15.
  • offset: a number indicating how many records to skip before the retrieved records. By default, it's 0.

In the example above, you retrieve 10 records, skipping the first 10 records.

Returns#

The method returns an array of records. The number of records is less than or equal to limit's value.


Sort Records#

Note: As of Medusa v2.11.0, MikroORM dependencies are included in the @medusajs/framework package. If you're using an older version of Medusa, change the import statement to @mikro-orm/knex.
src/modules/blog/service.ts
1import { 2  InjectTransactionManager,3  MedusaContext,4  MedusaService,5  InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10
11class BlogModuleService extends MedusaService({12  Post,13}){14  // ...15  @InjectTransactionManager()16  protected async doSomething_(17    id: string,18    @MedusaContext() sharedContext?: Context<EntityManager>19  ): Promise<any> {20    const posts = await this.postRepository_.find({21      where: {22        id,23      },24      options: {25        orderBy: {26          title: "ASC",27        },28      },29    })30
31    return posts32  }33
34  @InjectManager()35  async doSomething(36    id: string,37    @MedusaContext() sharedContext?: Context<EntityManager>38  ): Promise<any> {39    return await this.doSomething_(id, sharedContext)40  }41}

Parameters#

To sort records by one or more properties, the first object parameter of find accepts in its options property an orderBy property. Its value is an object whose keys are the property names of the data model, and values are the sorting order.

The sorting order can be one of the following:

  • ASC to sort by this property in the ascending order.
  • DESC to sort by this property in the descending order.

Returns#

The method returns an array of the first 15 records sorted by the specified properties.

Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...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
⇧↵