update Method - Service Factory Reference

This method updates one or more records of the data model.

Update One Record#

Code
1const post = await postModuleService.updatePosts({2  id: "123",3  name: "My Post",4})

Parameters#

To update one record, pass an object that at least has an id property, identifying the ID of the record to update.

You can pass in the same object any other properties to update.

Returns#

The method returns the updated record as an object.


Update Multiple Records#

Code
1const posts = await postModuleService.updatePosts([2  {3    id: "123",4    name: "My Post",5  },6  {7    id: "321",8    published_at: new Date(),9  },10])

Parameters#

To update multiple records, pass an array of objects. Each object has at least an id property, identifying the ID of the record to update.

You can pass in each object any other properties to update.

Returns#

The method returns an array of objects of updated records.


Update Records Matching a Filter#

Code
1const posts = await postModuleService.updatePosts({2  selector: {3    name: "My Post",4  },5  data: {6    published_at: new Date(),7  },8})

Parameters#

To update records that match specified filters, pass as a parameter an object having two properties:

  • selector: An object of filters that a record must match to be updated.
  • data: An object of the properties to update in every record that match the filters in selector.

In the example above, you update the published_at property of every post record whose name is My Post.

Note: Learn more about accepted filters in this documentation.

Returns#

The method returns an array of objects of updated records.


Multiple Record Updates with Filters#

Code
1const posts = await postModuleService.updatePosts([2  {3    selector: {4      name: "My Post",5    },6    data: {7      published_at: new Date(),8    },9  },10  {11    selector: {12      name: "Another Post",13    },14    data: {15      metadata: {16        external_id: "123",17      },18    },19  },20])

Parameters#

To update records matching different sets of filters, pass an array of objects, each having two properties:

  • selector: An object of filters that a record must match to be updated.
  • data: An object of the properties to update in every record that match the filters in selector.

In the example above, you update the published_at property of post records whose name is My Post, and update the metadata property of post records whose name is Another Post.

Note: Learn more about accepted filters in this documentation.

Returns#

The method returns an array of objects of updated records.

Was this page helpful?
Ask Anything
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