find Method - Data Model Repository Reference
The find method of a data model repository retrieves a list of records.
find Parameters#
Retrieve List of Records#
To retrieve a list of records matching a set of filters, use the find method of the data model repository:
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:
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.
Returns#
The method returns an array of the first 15 records matching the filters.
Retrieve Relations#
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 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:
Select Properties#
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:
Paginate Relations#
src/modules/blog/service.ts1import { 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's15.offset: a number indicating how many records to skip before the retrieved records. By default, it's0.
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#
@medusajs/framework package. If you're using an older version of Medusa, change the import statement to @mikro-orm/knex.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:
ASCto sort by this property in the ascending order.DESCto sort by this property in the descending order.
Returns
The method returns an array of the first 15 records sorted by the specified properties.