Skip to content

Commit b086e48

Browse files
committed
♻️ Refactoring (#1124)
1 parent 732c3d7 commit b086e48

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

prisma/seed.ts

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,13 @@ async function main() {
4343
try {
4444
console.log('Seeding has been started.');
4545

46-
// Create a queue to ensure sequential execution
47-
const mainQueue = new PQueue({ concurrency: 1 });
48-
49-
await mainQueue.add(() => addUsers());
50-
await mainQueue.add(() => addTasks());
51-
await mainQueue.add(() => addWorkBooks());
52-
await mainQueue.add(() => addTags());
53-
await mainQueue.add(() => addTaskTags());
54-
await mainQueue.add(() => addSubmissionStatuses());
55-
await mainQueue.add(() => addAnswers());
46+
await addUsers();
47+
await addTasks();
48+
await addWorkBooks();
49+
await addTags();
50+
await addTaskTags();
51+
await addSubmissionStatuses();
52+
await addAnswers();
5653

5754
console.log('Seeding has been completed.');
5855
} catch (e) {
@@ -70,7 +67,7 @@ async function addUsers() {
7067
// Create a queue with limited concurrency for user operations
7168
const userQueue = new PQueue({ concurrency: 2 });
7269

73-
users.map((user) =>
70+
for (const user of users) {
7471
userQueue.add(async () => {
7572
try {
7673
const password = 'Ch0kuda1';
@@ -87,8 +84,8 @@ async function addUsers() {
8784
} catch (e) {
8885
console.error('Failed to add user', user.name, e);
8986
}
90-
}),
91-
);
87+
});
88+
}
9289

9390
await userQueue.onIdle(); // Wait for all users to complete
9491
console.log('Finished adding users.');
@@ -119,7 +116,7 @@ async function addTasks() {
119116
// Create a queue with limited concurrency for database operations
120117
const taskQueue = new PQueue({ concurrency: 3 });
121118

122-
tasks.map((task) =>
119+
for (const task of tasks) {
123120
taskQueue.add(async () => {
124121
try {
125122
const registeredTask = await prisma.task.findUnique({
@@ -135,8 +132,8 @@ async function addTasks() {
135132
} catch (e) {
136133
console.error('Failed to add task', task.id, e);
137134
}
138-
}),
139-
);
135+
});
136+
}
140137

141138
await taskQueue.onIdle(); // Wait for all tasks to complete
142139
console.log('Finished adding tasks.');
@@ -175,14 +172,14 @@ async function addWorkBooks() {
175172

176173
if (normalizedUrlSlug) {
177174
// If urlSlug exists, check by urlSlug
178-
registeredWorkBook = await prisma.workBook.findMany({
175+
registeredWorkBook = await prisma.workBook.findFirst({
179176
where: {
180177
urlSlug: normalizedUrlSlug,
181178
},
182179
});
183180
} else {
184181
// If no urlSlug, check by title and authorId to avoid duplicates
185-
registeredWorkBook = await prisma.workBook.findMany({
182+
registeredWorkBook = await prisma.workBook.findFirst({
186183
where: {
187184
title: workbook.title,
188185
authorId: workbook.authorId,
@@ -192,7 +189,7 @@ async function addWorkBooks() {
192189

193190
if (!author) {
194191
console.warn('Not found author id: ', workbook.authorId, '.');
195-
} else if (registeredWorkBook.length >= 1) {
192+
} else if (registeredWorkBook) {
196193
if (normalizedUrlSlug) {
197194
console.warn('Url slug ', workbook.urlSlug, ' has already been registered.');
198195
} else {
@@ -252,7 +249,7 @@ async function addWorkBook(workbook, workBookFactory) {
252249
* ```
253250
*/
254251
function normalizeUrlSlug(urlSlug: string | null | undefined): string | undefined {
255-
return urlSlug && urlSlug !== '' ? urlSlug.toLocaleLowerCase() : undefined;
252+
return urlSlug && urlSlug !== '' ? urlSlug.toLowerCase() : undefined;
256253
}
257254

258255
async function addTags() {
@@ -263,24 +260,24 @@ async function addTags() {
263260
// Create a queue with limited concurrency for tag operations
264261
const tagQueue = new PQueue({ concurrency: 2 });
265262

266-
tags.map((tag) =>
263+
for (const tag of tags) {
267264
tagQueue.add(async () => {
268265
try {
269-
const registeredTag = await prisma.tag.findMany({
266+
const registeredTag = await prisma.tag.findUnique({
270267
where: {
271268
id: tag.id,
272269
},
273270
});
274271

275-
if (registeredTag.length === 0) {
276-
console.log('tag id:', tag.id, 'was registered.');
272+
if (!registeredTag) {
277273
await addTag(tag, tagFactory);
274+
console.log('tag id:', tag.id, 'was registered.');
278275
}
279276
} catch (e) {
280277
console.error('Failed to add tag', tag.id, e);
281278
}
282-
}),
283-
);
279+
});
280+
}
284281

285282
await tagQueue.onIdle(); // Wait for all tags to complete
286283
console.log('Finished adding tags.');
@@ -309,7 +306,7 @@ async function addTaskTags() {
309306
// Create a queue with limited concurrency for task tag operations
310307
const taskTagQueue = new PQueue({ concurrency: 2 });
311308

312-
task_tags.map((task_tag) =>
309+
for (const task_tag of task_tags) {
313310
taskTagQueue.add(async () => {
314311
try {
315312
const registeredTaskTag = await prisma.taskTag.findMany({
@@ -339,8 +336,8 @@ async function addTaskTags() {
339336
} catch (e) {
340337
console.error('Failed to add task tag', task_tag, e);
341338
}
342-
}),
343-
);
339+
});
340+
}
344341

345342
await taskTagQueue.onIdle(); // Wait for all task tags to complete
346343
console.log('Finished adding task tags.');
@@ -367,7 +364,7 @@ async function addSubmissionStatuses() {
367364
// Create a queue with limited concurrency for submission status operations
368365
const submissionStatusQueue = new PQueue({ concurrency: 2 });
369366

370-
submission_statuses.map((submission_status) =>
367+
for (const submission_status of submission_statuses) {
371368
submissionStatusQueue.add(async () => {
372369
try {
373370
const registeredSubmissionStatus = await prisma.submissionStatus.findMany({
@@ -383,8 +380,8 @@ async function addSubmissionStatuses() {
383380
} catch (e) {
384381
console.error('Failed to add submission status', submission_status.id, e);
385382
}
386-
}),
387-
);
383+
});
384+
}
388385

389386
await submissionStatusQueue.onIdle(); // Wait for all submission statuses to complete
390387
console.log('Finished adding submission statuses.');
@@ -410,7 +407,7 @@ async function addAnswers() {
410407
// Create a queue with limited concurrency for answer operations
411408
const answerQueue = new PQueue({ concurrency: 2 });
412409

413-
answers.map((answer) =>
410+
for (const answer of answers) {
414411
answerQueue.add(async () => {
415412
try {
416413
const registeredAnswer = await prisma.taskAnswer.findMany({
@@ -441,8 +438,8 @@ async function addAnswers() {
441438
} catch (e) {
442439
console.error('Failed to add answer', answer.id, e);
443440
}
444-
}),
445-
);
441+
});
442+
}
446443

447444
await answerQueue.onIdle(); // Wait for all answers to complete
448445
console.log('Finished adding answers.');

0 commit comments

Comments
 (0)