-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbFeeder.js
251 lines (238 loc) · 9.8 KB
/
dbFeeder.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// import all json file from fixtures
const users = require("./fixtures/user.json");
const event = require("./fixtures/event.json");
const formations = require("./fixtures/formations.json");
const mission = require("./fixtures/mission.json");
const quiz_answer = require("./fixtures/quiz_answer.json");
const quizz_question = require("./fixtures/quizz_question.json");
const quizz = require("./fixtures/quizz.json");
const roadmap = require("./fixtures/roadmap.json");
const skill_levels = require("./fixtures/skill_levels.json");
const skills = require("./fixtures/skills.json");
const { randomInt } = require("crypto");
const collection = require("./pb_schema.json");
import("pocketbase")
.then((module) => {
const PocketBase = module.default;
const pb = new PocketBase("http://localhost:8090");
let quizzAnswerId = [];
pb.admins
.create({
email: "[email protected]",
password: "1234567890",
passwordConfirm: "1234567890",
avatar: 8,
})
.then((createdRecord) => {
pb.admins
.authWithPassword("[email protected]", "1234567890")
.then((response) => {
pb.collections.import(collection).then(() => {
async function createUser(users, missions, defaultSkill) {
let userId;
for (const user of users) {
try {
user.missions = [
missions[randomInt(0, 3)],
missions[randomInt(0, 3)],
missions[randomInt(0, 3)],
];
user.defaultSkills = [
defaultSkill[randomInt(0, 5)],
defaultSkill[randomInt(0, 5)],
defaultSkill[randomInt(0, 5)],
];
const createdRecord = await pb
.collection("users")
.create(user, { $autoCancel: false });
userId = createdRecord.id;
} catch (error) {
console.error("Error creating user:", error);
}
}
return userId;
}
async function createEvent(event, userId) {
for (const singleEvent of event) {
singleEvent.author = userId;
pb.collection("events")
.create(singleEvent, { $autoCancel: false })
.then((createdRecord) => {
// console.log("Created event:", createdRecord);
})
.catch((error) => {
//console.error("Error creating event:", error);
});
}
}
async function createMission(mission) {
let missionId = [];
for (const singleMission of mission) {
try {
const createdRecord = await pb
.collection("missions")
.create(singleMission, { $autoCancel: false });
missionId.push(createdRecord.id);
} catch (error) {
console.error("Error creating mission:", error);
}
}
return missionId;
}
async function createQuizAnswer(quiz_answer, quizzAnswerId) {
for (const quiz_a of quiz_answer) {
try {
const createdRecord = await pb
.collection("quizz_answers")
.create(quiz_a, { $autoCancel: false });
quizzAnswerId.push(createdRecord.id);
} catch (error) {
console.error("Error creating user:", error);
}
}
return quizzAnswerId;
}
async function createQuizQuestion(quizz_question, quizzAnswerId) {
let quizzQuestionId = [];
for (const quiz_q of quizz_question) {
try {
quiz_q.field = [
quizzAnswerId[0],
quizzAnswerId[1],
quizzAnswerId[2],
];
const createdRecord = await pb
.collection("quizz_questions")
.create(quiz_q, { $autoCancel: false });
quizzQuestionId.push(createdRecord.id);
} catch (error) {
console.error("Error creating user:", error);
}
}
return quizzQuestionId;
}
async function createQuizz(quizz, quizzQuestionId) {
let quizId;
for (const quiz of quizz) {
try {
quiz.questions = [
quizzQuestionId[0],
quizzQuestionId[1],
quizzQuestionId[2],
];
const createdRecord = await pb
.collection("quizz")
.create(quiz, { $autoCancel: false });
quizId = createdRecord.id;
} catch (error) {
console.error("Error creating user:", error);
}
}
return quizId;
}
async function createFormation(formations, quizId) {
let formationId = [];
for (const formation of formations) {
try {
formation.quizz = quizId;
const createdRecord = await pb
.collection("formations")
.create(formation, { $autoCancel: false });
formationId.push(createdRecord.id);
} catch (error) {
console.error("Error creating user:", error);
}
}
return formationId;
}
async function createRoadmap(roadmap, formationId, quizId) {
for (const road of roadmap) {
try {
road.formations = [formationId[2]];
road.quizz = quizId;
console.log(road);
const createdRecord = await pb
.collection("roadmaps")
.create(road, { $autoCancel: false });
} catch (error) {
console.error("Error creating user:", error);
}
}
}
createQuizAnswer(quiz_answer, quizzAnswerId).then(
(quizzAnswerId) => {
console.log("QuizzAnswerId : " + quizzAnswerId);
createQuizQuestion(quizz_question, quizzAnswerId).then(
(quizzQuestionId) => {
quizzQuestionId = quizzQuestionId;
console.log("QuizzQuestionId : " + quizzQuestionId);
createQuizz(quizz, quizzQuestionId).then((quizId) => {
quizId = quizId;
console.log("QuizId : " + quizId);
createFormation(formations, quizId).then(
(formationId) => {
formationId = formationId;
console.log("FormationId : " + formationId);
createRoadmap(roadmap, formationId, quizId).then(
() => {
console.log("Roadmap created");
}
);
}
);
});
}
);
}
);
async function createSkillLevel(skill_levels) {
let skillId = [];
for (const skill_lvl of skill_levels) {
try {
const createdRecord = await pb
.collection("skill_levels")
.create(skill_lvl, { $autoCancel: false });
skillId.push(createdRecord.id);
} catch (error) {
console.error("Error creating user:", error);
}
}
return skillId;
}
async function createSkill(skills, skillId) {
let skillIdArray = [];
for (const skill of skills) {
skill.skill_level = [skillId[randomInt(0, 4)]];
const createdRecord = await pb
.collection("skills")
.create(skill, { $autoCancel: false });
skillIdArray.push(createdRecord.id);
}
return skillIdArray;
}
createSkillLevel(skill_levels).then((skillId) => {
console.log("SkillId : " + skillId);
createSkill(skills, skillId).then((skillIdArray) => {
skillId = skillIdArray;
console.log("SkillId : " + skillId);
createMission(mission).then((missionId) => {
missionId = missionId;
console.log("MissionId : " + missionId);
createUser(users, missionId, skillId).then((userId) => {
userId = userId;
console.log("UserId : " + userId);
createEvent(event, userId).then(() => {
console.log("Event created");
});
});
});
});
});
});
});
})
.catch((error) => {});
})
.catch((error) => {
console.error("Error importing pocketbase:", error);
});