forked from Team-ByteWise/frosthacks-2k25
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseed.ts
157 lines (142 loc) · 4.49 KB
/
seed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { PrismaClient } from '@prisma/client';
import * as bcrypt from 'bcrypt';
const prisma = new PrismaClient();
async function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, 10);
}
async function main() {
console.log('Starting database seeding...');
// Hash passwords for users
const users = await Promise.all([
{ email: 'user1@example.com', username: 'user1', password: await hashPassword('password1') },
{ email: 'user2@example.com', username: 'user2', password: await hashPassword('password2') },
{ email: 'user3@example.com', username: 'user3', password: await hashPassword('password3') },
]);
const projects = await prisma.project.createMany({
data: [
{ apiProjectId: 1, name: 'Plantation 1', description: 'Plantation 1', latitude: 1, longitude: 1, minPrice: 10 },
{ apiProjectId: 2, name: 'Plantation 2', description: 'Plantation 2', latitude: 1, longitude: 1, minPrice: 5 },
],
});
const allProjects = await prisma.project.findMany();
const trees = await prisma.tree.createMany({
data: [
{ apiTreeId: 1, projectId: allProjects[0].id, name: 'Pine', price: 10, stock: 100 },
{ apiTreeId: 2, projectId: allProjects[0].id, name: 'Rose', price: 5, stock: 50 },
{ apiTreeId: 3, projectId: allProjects[1].id, name: 'Lily', price: 3, stock: 30 },
{ apiTreeId: 4, projectId: allProjects[1].id, name: 'Oak', price: 8, stock: 20 },
],
});
const allTrees = await prisma.tree.findMany();
// Create category
const category = await prisma.category.create({
data: {
name: 'Fruits',
},
});
// Create tree details
const treeDetails = [
{
treeId: allTrees[0].id,
commonNames: 'Pine',
family: 'Pinaceae',
particularities: 'Hardy',
planterLikes: 'Shady',
foliageType: 'Evergreen',
height: '10-15',
imageUrl: 'https://treenation-uploads.s3.eu-central-1.amazonaws.com/project-efa7732a9f5e5bdc2163ac00954b0ded.webp',
lifeSpan: 100,
lifetimeCo2: 300,
co2Offset: 30,
co2OffsetPeriod: 'Annual',
categoryId: category.id,
},
{
treeId: allTrees[1].id,
commonNames: 'Rose',
family: 'Rosaceae',
particularities: 'Hardy',
planterLikes: 'Shady',
foliageType: 'Deciduous',
height: '5-10',
imageUrl: 'https://treenation-uploads.s3.eu-central-1.amazonaws.com/project-efa7732a9f5e5bdc2163ac00954b0ded.webp',
lifeSpan: 50,
lifetimeCo2: 200,
co2Offset: 20,
co2OffsetPeriod: 'Annual',
categoryId: category.id,
},
{
treeId: allTrees[2].id,
commonNames: 'Lily',
family: 'Liliaceae',
particularities: 'Hardy',
planterLikes: 'Shady',
foliageType: 'Deciduous',
height: '3-5',
imageUrl: 'https://treenation-uploads.s3.eu-central-1.amazonaws.com/project-efa7732a9f5e5bdc2163ac00954b0ded.webp',
lifeSpan: 30,
lifetimeCo2: 100,
co2Offset: 10,
co2OffsetPeriod: 'Annual',
categoryId: category.id,
},
{
treeId: allTrees[3].id,
commonNames: 'Oak',
family: 'Quercus',
particularities: 'Hardy',
planterLikes: 'Shady',
foliageType: 'Deciduous',
height: '10-15',
imageUrl: 'https://treenation-uploads.s3.eu-central-1.amazonaws.com/project-efa7732a9f5e5bdc2163ac00954b0ded.webp',
lifeSpan: 100,
lifetimeCo2: 300,
co2Offset: 30,
co2OffsetPeriod: 'Annual',
categoryId: category.id,
},
];
await prisma.treeDetails.createMany({
data: treeDetails,
});
// Create users and assign initial credits
for (let user of users) {
const newUser = await prisma.user.upsert({
where: { email: user.email },
update: {},
create: user,
});
await prisma.credit.create({
data: {
userId: newUser.id,
creditsEarned: 100,
creditsConsumed: 10,
},
});
const low = 1;
const high = 10;
const randomNumber = Math.floor(Math.random() * (high - low + 1)) + low;
for (let i = 0; i < randomNumber; i++) {
await prisma.order.create({
data: {
userId: newUser.id,
treeId: 1,
quantity: 5,
date: new Date(),
totalPrice: 50,
status: i % 2 ? 'COMPLETED' : 'PENDING'
}
});
}
}
console.log('Database seeding completed!');
}
main()
.catch((e) => {
console.error('Error seeding database:', e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});