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