Raw
1 ---
2 sidebar_label: "update"
3 ---
4
5 export const metadata = {
6 title: `update Method - Service Factory Reference`,
7 }
8
9 # {metadata.title}
10
11 This method of a module's service updates one or more records.
12
13 The method's name is `updateDataModel`, where `DataModel` is the plural pascal-case name of the data model.
14
15 ## Update One Record
16
17 ```ts
18 const post = await postModuleService.updatePosts({
19 id: "123",
20 name: "My Post",
21 })
22 ```
23
24 ### Parameters
25
26 To update one record, pass an object that has at least an `id` property, identifying the ID of the record to update.
27
28 You can pass in the same object any other properties to update.
29
30 ### Returns
31
32 The method returns the updated record as an object.
33
34 ---
35
36 ## Update Multiple Records
37
38 ```ts
39 const posts = await postModuleService.updatePosts([
40 {
41 id: "123",
42 name: "My Post",
43 },
44 {
45 id: "321",
46 published_at: new Date(),
47 },
48 ])
49 ```
50
51 ### Parameters
52
53 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.
54
55 You can pass in each object any other properties to update.
56
57 ### Returns
58
59 The method returns an array of updated record objects.
60
61 ---
62
63 ## Update Records Matching a Filter
64
65 ```ts
66 const posts = await postModuleService.updatePosts({
67 selector: {
68 name: "My Post",
69 },
70 data: {
71 published_at: new Date(),
72 },
73 })
74 ```
75
76 ### Parameters
77
78 To update records that match specified filters, pass an object with two properties as a parameter:
79
80 - `selector`: An object of filters that a record must match to be updated.
81 - `data`: An object of the properties to update in every record that match the filters in `selector`.
82
83 In the example above, you update the `published_at` property of every post record whose name is `My Post`.
84
85 <Note>
86
87 Refer to the [Filtering](../../tips/filtering/page.mdx) reference for more information on accepted filters and examples.
88
89 </Note>
90
91 ### Returns
92
93 The method returns an array of updated record objects.
94
95 ---
96
97 ## Multiple Record Updates with Filters
98
99 ```ts
100 const posts = await postModuleService.updatePosts([
101 {
102 selector: {
103 name: "My Post",
104 },
105 data: {
106 published_at: new Date(),
107 },
108 },
109 {
110 selector: {
111 name: "Another Post",
112 },
113 data: {
114 metadata: {
115 external_id: "123",
116 },
117 },
118 },
119 ])
120 ```
121
122 ### Parameters
123
124 To update records matching different sets of filters, pass an array of objects, each having two properties:
125
126 - `selector`: An object of filters that a record must match to be updated.
127 - `data`: An object of the properties to update in every record that match the filters in `selector`.
128
129 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`.
130
131 <Note>
132
133 Refer to the [Filtering](../../tips/filtering/page.mdx) reference for more information on accepted filters and examples.
134
135 </Note>
136
137 ### Returns
138
139 The method returns an array of updated record objects.
140
141 ---
142
143 ## Update a JSON Property
144
145 export const highlights = [
146 ["4", "metadata", "`metadata` is a JSON property."]
147 ]
148
149 ```ts highlights={highlights}
150 const post = await postModuleService.updatePosts({
151 id: "123",
152 name: "My Post",
153 metadata: {
154 category: "news",
155 tags: ["update", "json"],
156 },
157 })
158 ```
159
160 When you have a [JSON property](!docs!/learn/fundamentals/data-models/json-properties) 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.
161
162 ### Remove a Property from the JSON Property
163
164 ```ts highlights={highlights}
165 const post = await postModuleService.updatePosts({
166 id: "123",
167 name: "My Post",
168 metadata: {
169 is_featured: "",
170 },
171 })
172 ```
173
174 To remove a property from the JSON object, you can set its value to an empty string.
175
176 ### Learn More about Updating JSON Properties
177
178 Refer to the [JSON Properties](!docs!/learn/fundamentals/data-models/json-properties) documentation to learn more about how JSON properties work in Medusa and how to update them.