www/apps/resources/app/service-factory-reference/methods/update/page.mdx
export const metadata = {
title: 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.
const post = await postModuleService.updatePosts({
id: "123",
name: "My Post",
})
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.
The method returns the updated record as an object.
const posts = await postModuleService.updatePosts([
{
id: "123",
name: "My Post",
},
{
id: "321",
published_at: new Date(),
},
])
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.
The method returns an array of updated record objects.
const posts = await postModuleService.updatePosts({
selector: {
name: "My Post",
},
data: {
published_at: new Date(),
},
})
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.
Refer to the Filtering reference for more information on accepted filters and examples.
</Note>The method returns an array of updated record objects.
const posts = await postModuleService.updatePosts([
{
selector: {
name: "My Post",
},
data: {
published_at: new Date(),
},
},
{
selector: {
name: "Another Post",
},
data: {
metadata: {
external_id: "123",
},
},
},
])
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.
Refer to the Filtering reference for more information on accepted filters and examples.
</Note>The method returns an array of updated record objects.
export const highlights = [
["4", "metadata", "metadata is a JSON property."]
]
const post = await postModuleService.updatePosts({
id: "123",
name: "My Post",
metadata: {
category: "news",
tags: ["update", "json"],
},
})
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.
const post = await postModuleService.updatePosts({
id: "123",
name: "My Post",
metadata: {
is_featured: "",
},
})
To remove a property from the JSON object, you can set its value to an empty string.
Refer to the JSON Properties documentation to learn more about how JSON properties work in Medusa and how to update them.