/** * seedIssueStages.ts * - issueStages 테이블에 테스트 데이터 삽입 */ import db from "@/db/db" import { faker } from "@faker-js/faker" import { documents, issueStages } from "../schema/vendorDocu" // 하나의 문서당 스테이지 개수 범위 const STAGE_MIN = 1 const STAGE_MAX = 3 async function seedIssueStages() { try { console.log("Seeding issue stages started...") // 먼저 documents 테이블에서 모든 문서 ID 가져오기 const allDocs = await db.select({ id: documents.id }).from(documents) console.log(`Found ${allDocs.length} documents to create stages for.`) if (allDocs.length === 0) { console.warn("No documents found. Run seedDocuments.ts first.") return } // 각 문서에 대해 스테이지 생성 및 삽입 let totalStagesCreated = 0 for (const doc of allDocs) { const stageCount = faker.number.int({ min: STAGE_MIN, max: STAGE_MAX }) const stagesData = [] for (let j = 0; j < stageCount; j++) { stagesData.push({ documentId: doc.id, stageName: faker.helpers.arrayElement([ "Issued for Review", "IFC", "AFC", "As-Built", ]), planDate: faker.helpers.maybe(() => faker.date.future({ years: 0.5 }), { probability: 0.5, }), actualDate: faker.helpers.maybe(() => faker.date.future({ years: 0.5 }), { probability: 0.5, }) }) } // 한 번에 많은 데이터를 삽입하지 않고 문서별로 삽입 try { await db.insert(issueStages).values(stagesData) totalStagesCreated += stagesData.length // 진행 상황 로깅 (50개마다) if (totalStagesCreated % 50 === 0) { console.log(`Created ${totalStagesCreated} stages so far...`) } } catch (error) { console.error(`Error inserting stages for document ${doc.id}:`, error) } } console.log("Seeding issue stages completed successfully.") } catch (err) { console.error("Seeding issue stages error:", err) process.exit(1) } } // 스크립트 직접 실행 시 seedIssueStages() .then(() => process.exit(0)) .catch(() => process.exit(1))