update Method - Data Model Repository Reference

The update method of a data model repository updates one or more records of the data model.

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

update Parameters#

Loading...

Update 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.
Code
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 existingPost = await this.postRepository_.find({21      where: {22        id,23      },24    })    25    26    const posts = await this.postRepository_.update([27      {28        entity: existingPost[0],29        update: {30          title: "My Post Updated",31        },32      },33    ])34
35    return posts36  }37
38  @InjectManager()39  async doSomething(40    id: string,41    @MedusaContext() sharedContext?: Context<EntityManager>42  ): Promise<any> {43    return await this.doSomething_(id, sharedContext)44  }45}

Parameters#

The update method accepts an array of objects, each representing a record to update.

Each object has two properties:

  • entity: The entity or record to update. It must be the entire record, which can be retrieved using the find or findAndCount methods.
  • update: The properties to update. The keys are the property names of the data model, and the values are the values to update.

In the example above, you retrieve the record to update using the find method. Then, you pass the entire record and the properties to update to the update method.

Returns#

The method returns an array of the updated records.

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
⇧↵