@@ -34,6 +34,18 @@ import { submission_statuses } from './submission_statuses';
3434const prisma = new PrismaClient ( ) ;
3535initialize ( { prisma } ) ;
3636
37+ // Queue concurrency configuration
38+ // Adjust these values based on your database connection limits and performance requirements
39+ // Can be overridden via environment variables (e.g., SEED_USERS_CONCURRENCY=4)
40+ const QUEUE_CONCURRENCY = {
41+ users : Number ( process . env . SEED_USERS_CONCURRENCY ) || 2 , // User creation with password hashing (CPU intensive)
42+ tasks : Number ( process . env . SEED_TASKS_CONCURRENCY ) || 3 , // Task creation (lightweight)
43+ tags : Number ( process . env . SEED_TAGS_CONCURRENCY ) || 2 , // Tag creation (lightweight)
44+ taskTags : Number ( process . env . SEED_TASK_TAGS_CONCURRENCY ) || 2 , // TaskTag relations (multiple validations)
45+ submissionStatuses : Number ( process . env . SEED_SUBMISSION_STATUSES_CONCURRENCY ) || 2 , // SubmissionStatus creation (lightweight)
46+ answers : Number ( process . env . SEED_ANSWERS_CONCURRENCY ) || 2 , // Answer creation (multiple validations)
47+ } as const ;
48+
3749// See:
3850// https://github.com/TeemuKoivisto/sveltekit-monorepo-template/blob/main/packages/db/prisma/seed.ts
3951// https://lucia-auth.com/basics/keys/#password-hashing
@@ -65,7 +77,7 @@ async function addUsers() {
6577 const keyFactory = defineKeyFactory ( { defaultData : { user : userFactory } } ) ;
6678
6779 // Create a queue with limited concurrency for user operations
68- const userQueue = new PQueue ( { concurrency : 2 } ) ;
80+ const userQueue = new PQueue ( { concurrency : QUEUE_CONCURRENCY . users } ) ;
6981
7082 for ( const user of users ) {
7183 userQueue . add ( async ( ) => {
@@ -113,7 +125,7 @@ async function addTasks() {
113125 const taskFactory = defineTaskFactory ( ) ;
114126
115127 // Create a queue with limited concurrency for database operations
116- const taskQueue = new PQueue ( { concurrency : 3 } ) ;
128+ const taskQueue = new PQueue ( { concurrency : QUEUE_CONCURRENCY . tasks } ) ;
117129
118130 for ( const task of tasks ) {
119131 taskQueue . add ( async ( ) => {
@@ -153,8 +165,7 @@ async function addTask(task, taskFactory) {
153165async function addWorkBooks ( ) {
154166 console . log ( 'Start adding workbooks...' ) ;
155167
156- const userFactory = defineUserFactory ( ) ;
157- const workBookFactory = defineWorkBookFactory ( { defaultData : { user : userFactory } } ) ;
168+ const workBookFactory = defineWorkBookFactory ( { defaultData : { user : defineUserFactory ( ) } } ) ;
158169
159170 // Note: Use a for loop to ensure each workbook is processed sequentially.
160171 for ( const workbook of workbooks ) {
@@ -257,7 +268,7 @@ async function addTags() {
257268 const tagFactory = defineTagFactory ( ) ;
258269
259270 // Create a queue with limited concurrency for tag operations
260- const tagQueue = new PQueue ( { concurrency : 2 } ) ;
271+ const tagQueue = new PQueue ( { concurrency : QUEUE_CONCURRENCY . tags } ) ;
261272
262273 for ( const tag of tags ) {
263274 tagQueue . add ( async ( ) => {
@@ -303,29 +314,31 @@ async function addTaskTags() {
303314 } ) ;
304315
305316 // Create a queue with limited concurrency for task tag operations
306- const taskTagQueue = new PQueue ( { concurrency : 2 } ) ;
317+ const taskTagQueue = new PQueue ( { concurrency : QUEUE_CONCURRENCY . taskTags } ) ;
307318
308319 for ( const task_tag of task_tags ) {
309320 taskTagQueue . add ( async ( ) => {
310321 try {
311- const registeredTaskTag = await prisma . taskTag . findUnique ( {
312- where : {
313- task_id_tag_id : {
322+ const [ registeredTaskTag , registeredTask , registeredTag ] = await Promise . all ( [
323+ prisma . taskTag . findUnique ( {
324+ where : {
325+ task_id_tag_id : {
326+ task_id : task_tag . task_id ,
327+ tag_id : task_tag . tag_id ,
328+ } ,
329+ } ,
330+ } ) ,
331+ prisma . task . findUnique ( {
332+ where : {
314333 task_id : task_tag . task_id ,
315- tag_id : task_tag . tag_id ,
316334 } ,
317- } ,
318- } ) ;
319- const registeredTask = await prisma . task . findUnique ( {
320- where : {
321- task_id : task_tag . task_id ,
322- } ,
323- } ) ;
324- const registeredTag = await prisma . tag . findUnique ( {
325- where : {
326- id : task_tag . tag_id ,
327- } ,
328- } ) ;
335+ } ) ,
336+ prisma . tag . findUnique ( {
337+ where : {
338+ id : task_tag . tag_id ,
339+ } ,
340+ } ) ,
341+ ] ) ;
329342
330343 if ( ! registeredTaskTag && registeredTag && registeredTask ) {
331344 await addTaskTag ( task_tag , taskTagFactory ) ;
@@ -360,7 +373,7 @@ async function addSubmissionStatuses() {
360373 const submissionStatusFactory = defineSubmissionStatusFactory ( ) ;
361374
362375 // Create a queue with limited concurrency for submission status operations
363- const submissionStatusQueue = new PQueue ( { concurrency : 2 } ) ;
376+ const submissionStatusQueue = new PQueue ( { concurrency : QUEUE_CONCURRENCY . submissionStatuses } ) ;
364377
365378 for ( const submission_status of submission_statuses ) {
366379 submissionStatusQueue . add ( async ( ) => {
@@ -403,25 +416,26 @@ async function addAnswers() {
403416 const answerFactory = defineTaskAnswerFactory ( ) ;
404417
405418 // Create a queue with limited concurrency for answer operations
406- const answerQueue = new PQueue ( { concurrency : 2 } ) ;
419+ const answerQueue = new PQueue ( { concurrency : QUEUE_CONCURRENCY . answers } ) ;
407420
408421 for ( const answer of answers ) {
409422 answerQueue . add ( async ( ) => {
410423 try {
411- const registeredAnswer = await prisma . taskAnswer . findUnique ( {
412- where : {
413- task_id_user_id : {
414- task_id : answer . task_id ,
415- user_id : answer . user_id ,
424+ const [ registeredAnswer , registeredUser ] = await Promise . all ( [
425+ prisma . taskAnswer . findUnique ( {
426+ where : {
427+ task_id_user_id : {
428+ task_id : answer . task_id ,
429+ user_id : answer . user_id ,
430+ } ,
416431 } ,
417- } ,
418- } ) ;
419-
420- const registeredUser = await prisma . user . findUnique ( {
421- where : {
422- id : answer . user_id ,
423- } ,
424- } ) ;
432+ } ) ,
433+ prisma . user . findUnique ( {
434+ where : {
435+ id : answer . user_id ,
436+ } ,
437+ } ) ,
438+ ] ) ;
425439
426440 if ( ! registeredAnswer && registeredUser ) {
427441 await addAnswer ( answer , answerFactory ) ;
0 commit comments