Skip to content

Commit 74f1c45

Browse files
committed
update seed for db
1 parent 161496d commit 74f1c45

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

src/seeds/seed-data.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ProductDTO } from '../modules/products/dtos/product.dto';
1+
import { OrderCreateManyInput, OrderItemCreateManyInput, ProductCreateInput, UserCreateManyInput } from '../generated/prisma/models';
22

3-
export const productsData: ProductDTO[] = [
3+
export const productsData: ProductCreateInput[] = [
44
{ name: 'Classic T-Shirt', size: 'M', id: 'c1a9d6a2-1f3b-4a8e-9c2e-1b7f2d6a0a11', description: 'Soft cotton everyday t-shirt' },
55
{ name: 'Running Shoes', size: '42', id: 'd2b7f3c4-5a6d-4b9a-8e1c-2c8f3e7b1b22', description: 'Lightweight running shoes' },
66
{ name: 'Denim Jacket', size: 'L', id: 'e3c8a1b5-6d7e-4c2b-9f3a-3d9e4f8c2c33', description: 'Stylish blue denim jacket' },
@@ -57,3 +57,32 @@ export const productsData: ProductDTO[] = [
5757
{ name: 'Slippers', size: '43', id: '48c3c5df-091a-4c67-52c7-157c32822931', description: 'Comfortable indoor slippers' },
5858
{ name: 'Rain Jacket', size: 'XL', id: '59d4d6e0-1a2b-4d78-63d8-268d43933a42', description: 'Waterproof lightweight jacket' },
5959
];
60+
61+
export const userData: UserCreateManyInput[] = [
62+
{ id: '9aaca58e-4ea2-4008-bfc7-2007cd91c0f1', email: 'test@test.example', name: 'Test', password: '123456' },
63+
];
64+
65+
export const ordersData: OrderCreateManyInput[] = [
66+
{ id: '444e352e-acb2-46bd-a8d3-13f48379d4fe', userId: userData.find(user => user.email === 'test@test.example')!.id! },
67+
];
68+
69+
export const ordersItemData: OrderItemCreateManyInput[] = [
70+
{
71+
id: '8e4d87e8-8df6-45dc-9f0a-b379550abddf',
72+
orderId: ordersData[0].id!,
73+
name: 'Rain Jacket',
74+
size: 'XL',
75+
productId: '59d4d6e0-1a2b-4d78-63d8-268d43933a42',
76+
description: 'Waterprof lightweight jacket',
77+
quantity: 2,
78+
},
79+
{
80+
id: 'c4745c57-7315-448f-adbf-c9979a8c8b6f',
81+
orderId: ordersData[0].id!,
82+
productId: '26a1a3bd-e7f8-4a45-30a5-f36a1061070f',
83+
name: 'Gloves',
84+
size: 'M',
85+
description: 'Winter insulated gloves',
86+
quantity: 2,
87+
},
88+
];

src/seeds/seed.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'dotenv/config';
33
import { Pool } from 'pg';
44
import { PrismaPg } from '@prisma/adapter-pg';
55
import { PrismaClient } from '../generated/prisma/client';
6-
import { productsData } from './seed-data';
6+
import { ordersData, ordersItemData, productsData, userData } from './seed-data';
77

88
const connectionString = process.env.DATABASE_URL!;
99
const pool = new Pool({ connectionString });
@@ -18,6 +18,29 @@ async function main(): Promise<void> {
1818
create: data,
1919
});
2020
}
21+
22+
for (const data of userData) {
23+
await prisma.user.upsert({
24+
where: { id: data.id },
25+
update: data,
26+
create: data,
27+
});
28+
}
29+
30+
for (const data of ordersData) {
31+
await prisma.order.upsert({
32+
where: { id: data.id },
33+
update: data,
34+
create: data,
35+
});
36+
}
37+
for (const data of ordersItemData) {
38+
await prisma.orderItem.upsert({
39+
where: { id: data.id },
40+
update: data,
41+
create: data,
42+
});
43+
}
2144
}
2245

2346
main()

0 commit comments

Comments
 (0)