|
1
|
--- |
|
2
|
sidebar_label: "upsert" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `upsert Method - Data Model Repository Reference`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
The `upsert` method of a data model repository creates or 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
|
## `upsert` Parameters |
|
22
|
|
|
23
|
<TypeList |
|
24
|
types={[ |
|
25
|
{ |
|
26
|
type: "`array`", |
|
27
|
name: "data", |
|
28
|
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.", |
|
29
|
required: true, |
|
30
|
} |
|
31
|
]} |
|
32
|
sectionTitle="upsert Parameters" |
|
33
|
/> |
|
34
|
--- |
|
35
|
|
|
36
|
## Upsert Records |
|
37
|
|
|
38
|
<Note> |
|
39
|
|
|
40
|
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`. |
|
41
|
|
|
42
|
</Note> |
|
43
|
|
|
44
|
```ts |
|
45
|
import { |
|
46
|
InjectTransactionManager, |
|
47
|
MedusaContext, |
|
48
|
MedusaService, |
|
49
|
InjectManager, |
|
50
|
} from "@medusajs/framework/utils" |
|
51
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
52
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
53
|
import Post from "./models/post" |
|
54
|
|
|
55
|
class BlogModuleService extends MedusaService({ |
|
56
|
Post, |
|
57
|
}){ |
|
58
|
// ... |
|
59
|
@InjectTransactionManager() |
|
60
|
protected async doSomething_( |
|
61
|
id: string, |
|
62
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
63
|
): Promise<any> { |
|
64
|
const posts = await this.postRepository_.upsert([ |
|
65
|
{ |
|
66
|
id: "01JSHAW6Z7KW4X6E8MFPGNEKHC", |
|
67
|
title: "My Old Post", |
|
68
|
}, |
|
69
|
{ |
|
70
|
title: "My New Post", |
|
71
|
}, |
|
72
|
]) |
|
73
|
|
|
74
|
return posts |
|
75
|
} |
|
76
|
|
|
77
|
@InjectManager() |
|
78
|
async doSomething( |
|
79
|
id: string, |
|
80
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
81
|
): Promise<any> { |
|
82
|
return await this.doSomething_(id, sharedContext) |
|
83
|
} |
|
84
|
} |
|
85
|
``` |
|
86
|
|
|
87
|
### Parameters |
|
88
|
|
|
89
|
The `upsert` method accepts an array of objects, each representing a record to create or update. |
|
90
|
|
|
91
|
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. |
|
92
|
|
|
93
|
If the `id` property is specified but no record with that `id` exists, the created record will have the specified `id`. |
|
94
|
|
|
95
|
Each object has the same shape as the data model. |
|
96
|
|
|
97
|
### Returns |
|
98
|
|
|
99
|
The method returns an array of the created or updated records. |