Back to Medusa

{metadata.title}

www/apps/resources/app/data-model-repository-reference/methods/find/page.mdx

2.14.211.8 KB
Original Source

import { TypeList } from "docs-ui"

export const metadata = { title: find Method - Data Model Repository Reference, }

{metadata.title}

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.

</Note>

find Parameters

<TypeList types={[ { type: "object", name: "data", description: "Options to filter and configure the query to retrieve the records.", required: false, children: [ { type: "object", name: "where", description: "Filters to apply to the query. The keys are the property names of the data model, and the values are the values to filter by.", required: false, }, { type: "object", name: "options", description: "Options to configure the query.", required: false, children: [ { type: "string[]", name: "populate", description: "An array of relation names to retrieve.", required: false, }, { type: "string[]", name: "fields", description: "An array of property names to retrieve.", required: false, }, { type: "number", name: "limit", description: "The number of records to retrieve.", required: false, defaultValue: "15", }, { type: "number", name: "offset", description: "The number of records to skip before the retrieved records.", required: false, defaultValue: "0", }, { type: "object", name: "orderBy", description: "An object whose keys are the property names of the data model, and values are the sorting order.", required: false, }, { type: "string | string[]", name: "groupBy", description: "Group the records by one or more properties.", required: false, }, { type: "select-in \| joined", name: "strategy", description: "The strategy to use to retrieve the records. Learn more in the MikroORM documentation. If you don't specify a strategy and you specify the limit or offset options, the default strategy is select-in. Otherwise, the default strategy is joined.", required: false, } ] } ] } ]} sectionTitle="find Parameters" openedLevel={1} />


Retrieve List of Records

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

ts
import { 
  InjectTransactionManager,
  MedusaContext,
  MedusaService,
} from "@medusajs/framework/utils"
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
import Post from "./models/post"

class BlogModuleService extends MedusaService({
  Post,
}){
  // ...
  @InjectTransactionManager()
  protected async doSomething_(
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    const posts = await this.postRepository_.find()

    return posts
  }

  @InjectManager()
  async doSomething(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    return await this.doSomething_(id, sharedContext)
  }
}

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:

ts
import { 
  InjectTransactionManager,
  MedusaContext,
  MedusaService,
} from "@medusajs/framework/utils"
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
import Post from "./models/post"

class BlogModuleService extends MedusaService({
  Post,
}){
  // ...
  @InjectTransactionManager()
  protected async doSomething_(
    id: string[],
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    const posts = await this.postRepository_.find({
      where: {
        id,
      },
    })

    return posts
  }

  @InjectManager()
  async doSomething(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    return await this.doSomething_(id, sharedContext)
  }
}

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.

</Note>

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.

</Note>
ts
import { 
  InjectTransactionManager,
  MedusaContext,
  MedusaService,
} from "@medusajs/framework/utils"
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
import Post from "./models/post"

class BlogModuleService extends MedusaService({
  Post,
}){
  // ...
  @InjectTransactionManager()
  protected async doSomething_(
    id: string[],
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    const posts = await this.postRepository_.find({
      where: {
        id,
      },
      options: {
        populate: ["author"],
      },
    })

    return posts
  }

  @InjectManager()
  async doSomething(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    return await this.doSomething_(id, sharedContext)
  }
}

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:

json
[
  {
    "id": "1",
    "author": {
      "id": "1",
      "name": "John Doe",
      // ...
    },
    // ...
  }
]

Select Properties

ts
import { 
  InjectTransactionManager,
  MedusaContext,
  MedusaService,
} from "@medusajs/framework/utils"
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
import Post from "./models/post"

class BlogModuleService extends MedusaService({
  Post,
}){
  // ...
  @InjectTransactionManager()
  protected async doSomething_(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    const posts = await this.postRepository_.find({
      where: {
        id,
      },
      options: {
        fields: ["title"],
      },
    })

    return posts
  }

  @InjectManager()
  async doSomething(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    return await this.doSomething_(id, sharedContext)
  }
}

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:

json
[
  {
    "id": "1",
    "title": "My first post"
  }
]

Paginate Relations

ts
import { 
  InjectTransactionManager,
  MedusaContext,
  MedusaService,
} from "@medusajs/framework/utils"
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
import Post from "./models/post"

class BlogModuleService extends MedusaService({
  Post,
}){
  // ...
  @InjectTransactionManager()
  protected async doSomething_(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    const posts = await this.postRepository_.find({
      where: {
        id,
      },
      options: {
        limit: 10,
        offset: 10,
      },
    })

    return posts
  }

  @InjectManager()
  async doSomething(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    return await this.doSomething_(id, sharedContext)
  }
}

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.

</Note>
ts
import { 
  InjectTransactionManager,
  MedusaContext,
  MedusaService,
} from "@medusajs/framework/utils"
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
import Post from "./models/post"

class BlogModuleService extends MedusaService({
  Post,
}){
  // ...
  @InjectTransactionManager()
  protected async doSomething_(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    const posts = await this.postRepository_.find({
      where: {
        id,
      },
      options: {
        orderBy: {
          title: "ASC",
        },
      },
    })

    return posts
  }

  @InjectManager()
  async doSomething(
    id: string,
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<any> {
    return await this.doSomething_(id, sharedContext)
  }
}

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.