Raw
1 ---
2 sidebar_label: "upsertWithReplace"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `upsertWithReplace Method - Data Model Repository Reference`,
9 }
10
11 # {metadata.title}
12
13 The `upsertWithReplace` method of a data model repository can do three things:
14
15 - Create a new record.
16 - Update an existing record.
17 - Delete a record related to the record being upserted. If a one-to-many or many-to-many relation is omitted in the new data of a record, the method will delete the related records if they belong to the record being upserted.
18
19 This method is useful when you're doing complex upserts with relations, or you want details of the operations performed. Otherwise, use the [upsert](../upsert/page.mdx) method.
20
21 <Note>
22
23 This reference assumes you've already resolved the data model repository, as explained in the [Data Model Repository Reference](../../page.mdx) documentation.
24
25 </Note>
26
27 ## `upsertWithReplace` Parameters
28
29 <TypeList
30 types={[
31 {
32 type: "`array`",
33 name: "data",
34 description: "An array of records to create or update. If a record object has an `id` property, the method will try to update the record. Otherwise, the method will create a new record.",
35 required: true,
36 }
37 ]}
38 sectionTitle="upsertWithReplace Parameters"
39 />
40 ---
41
42 ## Upsert Records
43
44 <Note>
45
46 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`.
47
48 </Note>
49
50 ```ts
51 import {
52 InjectTransactionManager,
53 MedusaContext,
54 MedusaService,
55 InjectManager,
56 } from "@medusajs/framework/utils"
57 import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
58 import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
59 import Post from "./models/post"
60
61 class BlogModuleService extends MedusaService({
62 Post,
63 }){
64 // ...
65 @InjectTransactionManager()
66 protected async doSomething_(
67 id: string,
68 @MedusaContext() sharedContext?: Context<EntityManager>
69 ): Promise<any> {
70 const {
71 entities,
72 performedActions,
73 } = await this.postRepository_.upsertWithReplace([
74 {
75 id: "01JSHAW6Z7KW4X6E8MFPGNEKHC",
76 title: "My Old Post",
77 author_id: null,
78 },
79 {
80 id: "123",
81 title: "My New Post",
82 },
83 ])
84
85 return {
86 entities,
87 performedActions,
88 }
89 }
90
91 @InjectManager()
92 async doSomething(
93 id: string,
94 @MedusaContext() sharedContext?: Context<EntityManager>
95 ): Promise<any> {
96 return await this.doSomething_(id, sharedContext)
97 }
98 }
99 ```
100
101 ### Parameters
102
103 The `upsertWithReplace` method accepts an array of objects, each representing a record to create or update.
104
105 If a record object has an `id` property, the method will try to update a record with that `id`. Otherwise, the method will create a new record.
106
107 If the `id` property is specified but no record with that `id` exists, the created record will have the specified `id`.
108
109 Each object has the same shape as the data model.
110
111 ### Returns
112
113 The method returns an object with two properties:
114
115 - `entities`: An array of the created or updated records.
116 - `performedActions`: An object with three properties:
117 - `created`: An object whose keys are the names of data models, and values are arrays of the records that were created.
118 - `updated`: An object whose keys are the names of data models, and values are arrays of the records that were updated.
119 - `deleted`: An object whose keys are the names of data models, and values are arrays of the records that were deleted.
120
121 For example:
122
123 ```json title="Example"
124 {
125 "entities": [
126 {
127 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",
128 "title": "My Old Post",
129 "author_id": null
130 },
131 {
132 "id": "123",
133 "title": "My New Post"
134 }
135 ],
136 "performedActions": {
137 "created": {
138 "Post": [
139 {
140 "id": "123"
141 }
142 ]
143 },
144 "updated": {
145 "Post": [
146 {
147 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC"
148 }
149 ]
150 },
151 "deleted": {
152
153 }
154 }
155 }
156 ```