|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Seed Data with Custom CLI Script`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn how to seed data using a [custom CLI script](../page.mdx). |
|
8
|
|
|
9
|
## How to Seed Data |
|
10
|
|
|
11
|
To seed dummy data for development or demo purposes, use a custom CLI script. |
|
12
|
|
|
13
|
In the CLI script, use your custom workflows or Medusa's existing workflows, which you can browse in [this reference](!resources!/medusa-workflows-reference), to seed data. |
|
14
|
|
|
15
|
--- |
|
16
|
|
|
17
|
## Example: Seed Dummy Products |
|
18
|
|
|
19
|
In this section, you'll follow an example of creating a custom CLI script that seeds fifty dummy products. |
|
20
|
|
|
21
|
First, install the [Faker](https://fakerjs.dev/) library to generate random data in your script: |
|
22
|
|
|
23
|
```bash npm2yarn |
|
24
|
npm install --save-dev @faker-js/faker |
|
25
|
``` |
|
26
|
|
|
27
|
Then, create the file `src/scripts/demo-products.ts` with the following content: |
|
28
|
|
|
29
|
export const highlights = [ |
|
30
|
["16", "salesChannelModuleService", "Resolve the Sales Channel Module's service."], |
|
31
|
["19", "logger", "Resolve the logger to log messages in the terminal."], |
|
32
|
["22", "query", "Resolve Query to retrieve data later."], |
|
33
|
["26", "defaultSalesChannel", "Retrieve the default sales channel to associate products with."], |
|
34
|
["31", "sizeOptions", "Declare the size options to be used in the products' variants."], |
|
35
|
["32", "colorOptions", "Declare the color options to be used in the products' variants."], |
|
36
|
["33", "currency_code", "Declare the currency code to use in products' prices."], |
|
37
|
["34", "productsNum", "The number of products to seed."] |
|
38
|
] |
|
39
|
|
|
40
|
```ts title="src/scripts/demo-products.ts" highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports" |
|
41
|
import { ExecArgs } from "@medusajs/framework/types" |
|
42
|
import { faker } from "@faker-js/faker" |
|
43
|
import { |
|
44
|
ContainerRegistrationKeys, |
|
45
|
Modules, |
|
46
|
ProductStatus, |
|
47
|
} from "@medusajs/framework/utils" |
|
48
|
import { |
|
49
|
createInventoryLevelsWorkflow, |
|
50
|
createProductsWorkflow, |
|
51
|
} from "@medusajs/medusa/core-flows" |
|
52
|
|
|
53
|
export default async function seedDummyProducts({ |
|
54
|
container, |
|
55
|
}: ExecArgs) { |
|
56
|
const salesChannelModuleService = container.resolve( |
|
57
|
Modules.SALES_CHANNEL |
|
58
|
) |
|
59
|
const logger = container.resolve( |
|
60
|
ContainerRegistrationKeys.LOGGER |
|
61
|
) |
|
62
|
const query = container.resolve( |
|
63
|
ContainerRegistrationKeys.QUERY |
|
64
|
) |
|
65
|
|
|
66
|
const defaultSalesChannel = await salesChannelModuleService |
|
67
|
.listSalesChannels({ |
|
68
|
name: "Default Sales Channel", |
|
69
|
}) |
|
70
|
|
|
71
|
const sizeOptions = ["S", "M", "L", "XL"] |
|
72
|
const colorOptions = ["Black", "White"] |
|
73
|
const currency_code = "eur" |
|
74
|
const productsNum = 50 |
|
75
|
|
|
76
|
// TODO seed products |
|
77
|
} |
|
78
|
``` |
|
79
|
|
|
80
|
So far, in the script, you: |
|
81
|
|
|
82
|
- Resolve the [Sales Channel Module](!resources!/commerce-modules/sales-channel)'s service to retrieve the application's default sales channel. This is the sales channel the dummy products will be available in. |
|
83
|
- Resolve the [Logger](../../../debugging-and-testing/logging/page.mdx) to log messages in the terminal, and Query to later retrieve data useful for the seeded products. |
|
84
|
- Initialize some default data to use when seeding the products next. |
|
85
|
|
|
86
|
Next, replace the `TODO` with the following: |
|
87
|
|
|
88
|
```ts title="src/scripts/demo-products.ts" |
|
89
|
const productsData = new Array(productsNum).fill(0).map((_, index) => { |
|
90
|
const title = faker.commerce.product() + "_" + index |
|
91
|
return { |
|
92
|
title, |
|
93
|
is_giftcard: false, |
|
94
|
description: faker.commerce.productDescription(), |
|
95
|
status: ProductStatus.PUBLISHED, |
|
96
|
options: [ |
|
97
|
{ |
|
98
|
title: "Size", |
|
99
|
values: sizeOptions, |
|
100
|
}, |
|
101
|
{ |
|
102
|
title: "Color", |
|
103
|
values: colorOptions, |
|
104
|
}, |
|
105
|
], |
|
106
|
images: [ |
|
107
|
{ |
|
108
|
url: faker.image.urlPlaceholder({ |
|
109
|
text: title, |
|
110
|
}), |
|
111
|
}, |
|
112
|
{ |
|
113
|
url: faker.image.urlPlaceholder({ |
|
114
|
text: title, |
|
115
|
}), |
|
116
|
}, |
|
117
|
], |
|
118
|
variants: new Array(10).fill(0).map((_, variantIndex) => ({ |
|
119
|
title: `${title} ${variantIndex}`, |
|
120
|
sku: `variant-${variantIndex}${index}`, |
|
121
|
prices: new Array(10).fill(0).map((_, priceIndex) => ({ |
|
122
|
currency_code, |
|
123
|
amount: 10 * priceIndex, |
|
124
|
})), |
|
125
|
options: { |
|
126
|
Size: sizeOptions[Math.floor(Math.random() * 3)], |
|
127
|
}, |
|
128
|
})), |
|
129
|
shipping_profile_id: "sp_123", |
|
130
|
sales_channels: [ |
|
131
|
{ |
|
132
|
id: defaultSalesChannel[0].id, |
|
133
|
}, |
|
134
|
], |
|
135
|
} |
|
136
|
}) |
|
137
|
|
|
138
|
// TODO seed products |
|
139
|
``` |
|
140
|
|
|
141
|
You generate fifty products using the sales channel and variables you initialized, and using Faker for random data, such as the product's title or images. |
|
142
|
|
|
143
|
Then, replace the new `TODO` with the following: |
|
144
|
|
|
145
|
```ts title="src/scripts/demo-products.ts" |
|
146
|
const { result: products } = await createProductsWorkflow(container).run({ |
|
147
|
input: { |
|
148
|
products: productsData, |
|
149
|
}, |
|
150
|
}) |
|
151
|
|
|
152
|
logger.info(`Seeded ${products.length} products.`) |
|
153
|
|
|
154
|
// TODO add inventory levels |
|
155
|
``` |
|
156
|
|
|
157
|
You create the generated products using the `createProductsWorkflow` imported previously from `@medusajs/medusa/core-flows`. It accepts the product data as input, and returns the created products. |
|
158
|
|
|
159
|
The only thing left is to create [inventory levels](!resources!/commerce-modules/inventory/concepts#inventorylevel) for the product variants. So, replace the last `TODO` with the following: |
|
160
|
|
|
161
|
```ts title="src/scripts/demo-products.ts" |
|
162
|
logger.info("Seeding inventory levels.") |
|
163
|
|
|
164
|
const { data: stockLocations } = await query.graph({ |
|
165
|
entity: "stock_location", |
|
166
|
fields: ["id"], |
|
167
|
}) |
|
168
|
|
|
169
|
const { data: inventoryItems } = await query.graph({ |
|
170
|
entity: "inventory_item", |
|
171
|
fields: ["id"], |
|
172
|
}) |
|
173
|
|
|
174
|
const inventoryLevels = inventoryItems.map((inventoryItem) => ({ |
|
175
|
location_id: stockLocations[0].id, |
|
176
|
stocked_quantity: 1000000, |
|
177
|
inventory_item_id: inventoryItem.id, |
|
178
|
})) |
|
179
|
|
|
180
|
await createInventoryLevelsWorkflow(container).run({ |
|
181
|
input: { |
|
182
|
inventory_levels: inventoryLevels, |
|
183
|
}, |
|
184
|
}) |
|
185
|
|
|
186
|
logger.info("Finished seeding inventory levels data.") |
|
187
|
``` |
|
188
|
|
|
189
|
You use [Query](../../query/page.mdx) to retrieve the first stock location in the application and the inventory items. |
|
190
|
|
|
191
|
Then, you generate inventory levels for each inventory item, associating it with the first stock location. |
|
192
|
|
|
193
|
Finally, you use the `createInventoryLevelsWorkflow` from Medusa's core workflows to create the inventory levels. |
|
194
|
|
|
195
|
### Test Script |
|
196
|
|
|
197
|
To test out the script, run the following command in your project's directory: |
|
198
|
|
|
199
|
```bash npx2yarn |
|
200
|
npx medusa exec ./src/scripts/demo-products.ts |
|
201
|
``` |
|
202
|
|
|
203
|
This seeds the products to your database. If you run your Medusa application and view the products in the dashboard, you'll find fifty new products. |