update Method - Service Factory Reference

This method of a module's service updates one or more records.

The method's name is updateDataModel, where DataModel is the plural pascal-case name 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 has at least 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 must have 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 updated record objects.


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 an object with two properties as a parameter:

  • 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: Refer to the Filtering reference for more information on accepted filters and examples.

Returns#

The method returns an array of updated record objects.


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: Refer to the Filtering reference for more information on accepted filters and examples.

Returns#

The method returns an array of updated record objects.


Update a JSON Property#

Code
1const post = await postModuleService.updatePosts({2  id: "123",3  name: "My Post",4  metadata: {5    category: "news",6    tags: ["update", "json"],7  },8})

When you have a JSON property in your data model, you can update it by adding, updating, or removing properties within that JSON object. Medusa will merge the properties you pass in the update method with the existing JSON object.

Remove a Property from the JSON Property#

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

To remove a property from the JSON object, you can set its value to an empty string.

Learn More about Updating JSON Properties#

Refer to the JSON Properties documentation to learn more about how JSON properties work in Medusa and how to update them.

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