Skip to content

Commit f6da950

Browse files
committed
[FIX ERROR] Seeder menggunakan promise untuk menghindari operasi sekuensial mengakibatkan duplikasi id ketika dilakukan seeder ke dua kali
1 parent 977ca7a commit f6da950

1 file changed

Lines changed: 51 additions & 20 deletions

File tree

server/utills/insertDemoData.js

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ const { PrismaClient } = require("@prisma/client");
22

33
const prisma = new PrismaClient();
44

5+
const demoMerchant = [
6+
{
7+
id: "1",
8+
name: "Demo Merchant",
9+
description: "This is demo merchant description",
10+
phone: "1234567890",
11+
address: "123 Demo St, Demo City, DM 12345",
12+
status: "active",
13+
createdAt : new Date(),
14+
updatedAt : new Date(),
15+
}
16+
]
17+
518
const demoProducts = [
619
{
720
id: "1",
@@ -14,6 +27,7 @@ const demoProducts = [
1427
manufacturer: "Samsung",
1528
categoryId: "3117a1b0-6369-491e-8b8b-9fdd5ad9912e",
1629
inStock: 0,
30+
merchantId: "1",
1731
},
1832
{
1933
id: "2",
@@ -26,6 +40,7 @@ const demoProducts = [
2640
manufacturer: "Canon",
2741
categoryId: "659a91b9-3ff6-47d5-9830-5e7ac905b961",
2842
inStock: 0,
43+
merchantId: "1",
2944
},
3045
{
3146
id: "3",
@@ -38,6 +53,7 @@ const demoProducts = [
3853
manufacturer: "ZunVolt",
3954
categoryId: "6c3b8591-b01e-4842-bce1-2f5585bf3a28",
4055
inStock: 1,
56+
merchantId: "1",
4157
},
4258
{
4359
id: "4",
@@ -50,6 +66,7 @@ const demoProducts = [
5066
manufacturer: "Samsung",
5167
categoryId: "d30b85e2-e544-4f48-8434-33fe0b591579",
5268
inStock: 1,
69+
merchantId: "1",
5370
},
5471
{
5572
id: "5",
@@ -62,6 +79,7 @@ const demoProducts = [
6279
manufacturer: "Samsung",
6380
categoryId: "ada699e5-e764-4da0-8d3e-18a8c8c5ed24",
6481
inStock: 1,
82+
merchantId: "1",
6583
},
6684
{
6785
id: "6",
@@ -74,6 +92,7 @@ const demoProducts = [
7492
manufacturer: "Samsung",
7593
categoryId: "1cb9439a-ea47-4a33-913b-e9bf935bcc0b",
7694
inStock: 1,
95+
merchantId: "1",
7796
},
7897
{
7998
id: "7",
@@ -86,6 +105,7 @@ const demoProducts = [
86105
manufacturer: "SOWO",
87106
categoryId: "7a241318-624f-48f7-9921-1818f6c20d85",
88107
inStock: 1,
108+
merchantId: "1",
89109
},
90110
{
91111
id: "8",
@@ -98,6 +118,7 @@ const demoProducts = [
98118
manufacturer: "Bosch",
99119
categoryId: "8d2a091c-4b90-4d60-b191-114b895f3e54",
100120
inStock: 1,
121+
merchantId: "1",
101122
},
102123
{
103124
id: "9",
@@ -110,6 +131,7 @@ const demoProducts = [
110131
manufacturer: "Sony",
111132
categoryId: "4c2cc9ec-7504-4b7c-8ecd-2379a854a423",
112133
inStock: 1,
134+
merchantId: "1",
113135
},
114136
{
115137
id: "10",
@@ -122,6 +144,7 @@ const demoProducts = [
122144
manufacturer: "Samsung",
123145
categoryId: "a6896b67-197c-4b2a-b5e2-93954474d8b4",
124146
inStock: 1,
147+
merchantId: "1",
125148
},
126149
{
127150
id: "11",
@@ -134,6 +157,7 @@ const demoProducts = [
134157
manufacturer: "HP",
135158
categoryId: "782e7829-806b-489f-8c3a-2689548d7153",
136159
inStock: 1,
160+
merchantId: "1",
137161
},
138162
{
139163
id: "12",
@@ -146,6 +170,7 @@ const demoProducts = [
146170
manufacturer: "Gillete",
147171
categoryId: "313eee86-bc11-4dc1-8cb0-6b2c2a2a1ccb",
148172
inStock: 0,
173+
merchantId: "1",
149174
}
150175
];
151176

@@ -214,27 +239,33 @@ const demoCategories = [
214239
];
215240

216241
async function insertDemoData() {
217-
218-
for (const category of demoCategories) {
219-
await prisma.category.create({
220-
data: category,
221-
});
222-
}
223-
console.log("Demo categories inserted successfully!");
224-
225-
for (const product of demoProducts) {
226-
await prisma.product.create({
227-
data: product,
228-
});
242+
// Gunakan Promise.all untuk menjalankan operasi secara paralel
243+
const promises = [
244+
// Operasi untuk menyisipkan merchant
245+
...demoMerchant.map((merchant) =>
246+
prisma.merchant.create({ data: merchant })
247+
),
248+
// Operasi untuk menyisipkan categories
249+
...demoCategories.map((category) =>
250+
prisma.category.create({ data: category })
251+
),
252+
// Operasi untuk menyisipkan products
253+
...demoProducts.map((product) =>
254+
prisma.product.create({ data: product })
255+
),
256+
];
257+
258+
try {
259+
// Tunggu semua promise selesai
260+
await Promise.all(promises);
261+
262+
console.log("All demo data inserted successfully!");
263+
} catch (error) {
264+
console.error("Error inserting demo data:", error);
229265
}
230-
console.log("Demo products inserted successfully!");
231266
}
232267

233268
insertDemoData()
234-
.catch((error) => {
235-
console.error(error);
236-
process.exit(1);
237-
})
238-
.finally(async () => {
239-
await prisma.$disconnect();
240-
});
269+
.finally(async () => {
270+
await prisma.$disconnect(); // Pastikan koneksi Prisma ditutup setelah selesai
271+
});

0 commit comments

Comments
 (0)