findAndCount Method - Data Model Repository Reference
The findAndCount method of a data model repository retrieves a list of records and the total count of records that match the query.
findAndCount Parameters#
Retrieve List of Records#
To retrieve a list of records matching a set of filters, use the findAndCount method of the data model repository:
@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, count] = await this.postRepository_.findAndCount()21 22 return {23 posts,24 count,25 }26 }27 28 @InjectManager()29 async doSomething(30 id: string,31 @MedusaContext() sharedContext?: Context<EntityManager>32 ): Promise<any> {33 return await this.doSomething_(id, sharedContext)34 }35}
Returns#
The method returns a tuple of two elements:
- An array of the first
15records of the data model. - The total count of records that match the query.
Filter Records#
You can filter the retrieved records by passing filters as a first parameter of the findAndCount 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, count] = await this.postRepository_.findAndCount({21 where: {22 id,23 },24 })25 26 return {27 posts,28 count,29 }30 }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 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 a tuple of two elements:
- An array of the first
15records matching the filters. - The total count of records that match the query.
Retrieve 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, count] = await this.postRepository_.findAndCount({21 where: {22 id,23 },24 options: {25 populate: ["author"],26 },27 })28 29 return {30 posts,31 count,32 }33 }34 35 @InjectManager()36 async doSomething(37 id: string,38 @MedusaContext() sharedContext?: Context<EntityManager>39 ): Promise<any> {40 return await this.doSomething_(id, sharedContext)41 }42}
Parameters#
The first object parameter of findAndCount accepts an options 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 a tuple of two elements:
- An array of the first
15records. - The total count of records that match the query.
Each record in the array 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, count] = await this.postRepository_.findAndCount({21 where: {22 id,23 },24 options: {25 fields: ["title"],26 },27 })28 29 return {30 posts,31 count,32 }33 }34 35 @InjectManager()36 async doSomething(37 id: string,38 @MedusaContext() sharedContext?: Context<EntityManager>39 ): Promise<any> {40 return await this.doSomething_(id, sharedContext)41 }42}
Parameters#
By default, retrieved records have all their properties. The first object parameter of findAndCount 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 a tuple of two elements:
- An array of the first
15records. - The total count of records that match the query.
Each record in the array will have only the specified properties:
Paginate 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, count] = await this.postRepository_.findAndCount({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
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, count] = await this.postRepository_.findAndCount({21 where: {22 id,23 },24 options: {25 orderBy: {26 title: "ASC",27 },28 },29 })30 31 return {32 posts,33 count,34 }35 }36 37 @InjectManager()38 async doSomething(39 id: string,40 @MedusaContext() sharedContext?: Context<EntityManager>41 ): Promise<any> {42 return await this.doSomething_(id, sharedContext)43 }44}
Parameters#
To sort records by one or more properties, the first object parameter of findAndCount 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 a tuple of two elements:
- An array of the first
15records. - The total count of records that match the query.