Raw
1 ---
2 sidebar_label: "update"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `update Method - Data Model Repository Reference`,
9 }
10
11 # {metadata.title}
12
13 The `update` method of a data model repository updates one or more records of the data model.
14
15 <Note>
16
17 This reference assumes you've already resolved the data model repository, as explained in the [Data Model Repository Reference](../../page.mdx) documentation.
18
19 </Note>
20
21 ## `update` Parameters
22
23 <TypeList
24 types={[
25 {
26 type: "`array`",
27 name: "data",
28 description: "An array of update details.",
29 required: true,
30 children: [
31 {
32 type: "`object`",
33 name: "entity",
34 description: "The entity to update. It must be the entire entity, which can be retrieved using the `find` method.",
35 required: true,
36 },
37 {
38 type: "`object`",
39 name: "update",
40 description: "The properties to update. The keys are the property names of the data model, and the values are the values to update.",
41 required: true,
42 }
43 ]
44 }
45 ]}
46 openedLevel={1}
47 sectionTitle="update Parameters"
48 />
49 ---
50
51 ## Update Records
52
53 <Note>
54
55 As of [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/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`.
56
57 </Note>
58
59 ```ts
60 import {
61 InjectTransactionManager,
62 MedusaContext,
63 MedusaService,
64 InjectManager,
65 } from "@medusajs/framework/utils"
66 import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
67 import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
68 import Post from "./models/post"
69
70 class BlogModuleService extends MedusaService({
71 Post,
72 }){
73 // ...
74 @InjectTransactionManager()
75 protected async doSomething_(
76 id: string,
77 @MedusaContext() sharedContext?: Context<EntityManager>
78 ): Promise<any> {
79 const existingPost = await this.postRepository_.find({
80 where: {
81 id,
82 },
83 })
84
85 const posts = await this.postRepository_.update([
86 {
87 entity: existingPost[0],
88 update: {
89 title: "My Post Updated",
90 },
91 },
92 ])
93
94 return posts
95 }
96
97 @InjectManager()
98 async doSomething(
99 id: string,
100 @MedusaContext() sharedContext?: Context<EntityManager>
101 ): Promise<any> {
102 return await this.doSomething_(id, sharedContext)
103 }
104 }
105 ```
106
107 ### Parameters
108
109 The `update` method accepts an array of objects, each representing a record to update.
110
111 Each object has two properties:
112
113 - `entity`: The entity or record to update. It must be the entire record, which can be retrieved using the [find](../find/page.mdx) or [findAndCount](../findAndCount/page.mdx) methods.
114 - `update`: The properties to update. The keys are the property names of the data model, and the values are the values to update.
115
116 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.
117
118 ### Returns
119
120 The method returns an array of the updated records.