-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.js
198 lines (153 loc) · 5.55 KB
/
seeder.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const { MongoClient, ObjectId } = require("mongodb");
const faker = require("faker");
const mongo = new MongoClient("mongodb://localhost");
const dbName = "your_database_name"; // Replace with your actual database name
const client = await mongo.connect();
const db = client.db(dbName);
// Function to generate a random element from an array
function getRandomFromArray(array) {
const randomIndex = Math.floor(Math.random() * array.length);
return array[randomIndex];
}
async function seedUsers() {
await db.collection("users").deleteMany({});
const roles = ["Admin", "Teacher", "Student"];
const users = [];
for (let i = 0; i < 100; i++) {
const role = getRandomFromArray(roles);
const user = {
_id: new ObjectId(),
username: faker.internet.userName(),
email: faker.internet.email(),
password: faker.internet.password(),
profile: faker.lorem.sentence(),
role,
};
users.push(user);
}
await db.collection("users").insertMany(users);
}
async function seedTeacher() {
await db.collection("teachers").deleteMany({});
const teachers = [];
const users = await db.collection("users").find({ role: "Teacher" }).toArray();
for (const user of users) {
const teacher = {
_id: user._id,
bio: faker.lorem.paragraph(),
student_id: [], // Initialize with empty array
courses: [], // Initialize with empty array
chatRoom_id: new ObjectId(), // Generate new ObjectId
bluemark: faker.random.boolean(),
};
teachers.push(teacher);
}
await db.collection("teachers").insertMany(teachers);
}
async function seedAdmin() {
// Similar structure as seedUsers, but for admins
}
async function seedStudent() {
// Similar structure as seedUsers, but for students
}
async function seedChatRoom() {
await db.collection("chatRooms").deleteMany({});
const chatRooms = [];
const students = await db.collection("users").find({ role: "Student" }).toArray();
const teachers = await db.collection("users").find({ role: "Teacher" }).toArray();
for (let i = 0; i < 50; i++) {
const participantStudent = getRandomFromArray(students);
const participantTeacher = getRandomFromArray(teachers);
const chatRoom = {
_id: new ObjectId(),
participants: [participantStudent._id, participantTeacher._id],
message: [], // Initialize with empty array
};
chatRooms.push(chatRoom);
}
await db.collection("chatRooms").insertMany(chatRooms);
}
async function seedMessage() {
// Similar structure as seedUsers, but for messages
}
async function seedCourses() {
await db.collection("courses").deleteMany({});
const courses = [];
const teachers = await db.collection("teachers").find().toArray();
for (let i = 0; i < 50; i++) {
const course = {
_id: new ObjectId(),
title: faker.lorem.words(),
description: faker.lorem.paragraph(),
category: getRandomFromArray(["graphic design", "programming", "accounting"]),
price: faker.random.number({ min: 50, max: 500 }),
CourseOwner: getRandomFromArray(teachers)._id,
modules: [], // Initialize with empty array
rating: faker.random.number({ min: 1, max: 5 }),
likes: [], // Initialize with empty array
comments: [], // Initialize with empty array
};
courses.push(course);
}
await db.collection("courses").insertMany(courses);
}
async function seedModule() {
// Similar structure as seedUsers, but for modules
}
async function seedComment() {
// Similar structure as seedUsers, but for comments
}
async function seedPaymentMethod(() {
await db.collection("paymentMethods").deleteMany({});
const paymentMethods = [];
const users = await db.collection("users").find().toArray();
for (let i = 0; i < 50; i++) {
const paymentMethod = {
_id: new ObjectId(),
type: getRandomFromArray(["visa", "mastercard"]),
amount: faker.random.number({ min: 30000, max: 50000 }),
cardNumber: faker.finance.creditCardNumber(),
cardOwner: getRandomFromArray(users)._id,
CVV: faker.finance.creditCardCVV(),
created_at: new Date(),
};
paymentMethods.push(paymentMethod);
}
await db.collection("paymentMethods").insertMany(paymentMethods);
}
async function seedTransaction() {
await db.collection("transactions").deleteMany({});
const transactions = [];
const paymentMethods = await db.collection("paymentMethods").find().toArray();
const users = await db.collection("users").find().toArray();
for (let i = 0; i < 100; i++) {
const transaction = {
_id: new ObjectId(),
amount: faker.random.number({ min: 100, max: 500 }),
paymentMethod: getRandomFromArray(paymentMethods)._id,
giver: getRandomFromArray(users)._id,
teacherReceiver: getRandomFromArray(users)._id,
adminReceiver: getRandomFromArray(users)._id,
whyTran: faker.lorem.sentence(),
created_at: new Date(),
};
transactions.push(transaction);
}
await db.collection("transactions").insertMany(transactions);
}
async function seedAll() {
await seedUsers();
await seedTeacher();
await seedAdmin();
await seedStudent();
await seedChatRoom();
await seedMessage();
await seedCourse();
await seedModule();
await seedComment();
await seedPaymentMethod();
await seedTransaction();
client.close();
}
seedAll();
//npm install faker