summaryrefslogtreecommitdiff
path: root/db/seeds/vendorDocu.ts
diff options
context:
space:
mode:
Diffstat (limited to 'db/seeds/vendorDocu.ts')
-rw-r--r--db/seeds/vendorDocu.ts144
1 files changed, 144 insertions, 0 deletions
diff --git a/db/seeds/vendorDocu.ts b/db/seeds/vendorDocu.ts
new file mode 100644
index 00000000..eb96ef7b
--- /dev/null
+++ b/db/seeds/vendorDocu.ts
@@ -0,0 +1,144 @@
+/**
+ * seed.ts
+ * - 드릴즐 + faker로 documents / issueStages / revisions 테이블에 테스트 데이터 삽입 예시
+ */
+
+import db from "@/db/db"
+import { eq } from "drizzle-orm"
+// faker-js/faker 또는 @faker-js/faker
+import { faker } from "@faker-js/faker"
+import { documents, issueStages, revisions } from "../schema/vendorDocu"
+
+// 몇 개의 도큐먼트를 만들지
+const NUM_DOCUMENTS = 200
+
+// 하나의 문서당 스테이지 개수 범위
+const STAGE_MIN = 1
+const STAGE_MAX = 3
+
+// 하나의 스테이지당 리비전 개수 범위
+const REV_MIN = 1
+const REV_MAX = 2
+
+// 가상으로 계약 ID가 1~5 범위라고 가정
+const CONTRACT_ID_MIN = 96
+const CONTRACT_ID_MAX = 125
+
+async function seed() {
+ try {
+ console.log("Seeding started...")
+
+ // 1) documents 테이블에 NUM_DOCUMENTS 개 생성
+ const docsData = []
+ for (let i = 0; i < NUM_DOCUMENTS; i++) {
+ const randomContractId = faker.number.int({
+ min: CONTRACT_ID_MIN,
+ max: CONTRACT_ID_MAX,
+ })
+
+ docsData.push({
+ contractId: randomContractId,
+ docNumber: `DOC-${faker.number.int({ min: 1000, max: 9999 })}`,
+ title: faker.lorem.sentence(3), // 예: "dolor sit amet"
+ status: faker.helpers.arrayElement(["ACTIVE", "REVIEW", "APPROVED"]),
+ issuedDate: faker.helpers.maybe(() => faker.date.past({ years: 1 }), {
+ probability: 0.7,
+ })
+ // createdAt, updatedAt은 defaultNow()로 자동
+ })
+ }
+
+ // documents 테이블 INSERT
+ const insertedDocs = await db.insert(documents).values(docsData).returning()
+ console.log(`Inserted ${insertedDocs.length} documents.`)
+
+ // 2) 각 Document에 대해 스테이지(issueStages) 생성
+ for (const doc of insertedDocs) {
+ // doc.id가 undefined인지 확인
+ if (!doc.id) {
+ console.error("Document ID is undefined:", doc)
+ continue
+ }
+
+ 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,
+ })
+ // 50% 확률로 실제일
+ })
+ }
+
+ // 스테이지 데이터가 비어 있는지 확인
+ if (stagesData.length === 0) {
+ console.warn(`No stages created for document ${doc.id}`)
+ continue
+ }
+
+ try {
+ const insertedStages = await db.insert(issueStages).values(stagesData).returning()
+ console.log(`Inserted ${insertedStages.length} stages for document ${doc.id}`)
+
+ // 3) 각 스테이지별 리비전(revisions) 생성
+ for (const stage of insertedStages) {
+ // stage.id가 undefined인지 확인
+ if (!stage.id) {
+ console.error("Stage ID is undefined:", stage)
+ continue
+ }
+
+ const revCount = faker.number.int({ min: REV_MIN, max: REV_MAX })
+ const revData = []
+ for (let k = 0; k < revCount; k++) {
+ revData.push({
+ issueStageId: stage.id,
+ documentId: doc.id, // 문서 ID도 추가
+ revision: faker.helpers.arrayElement(["A", "B", "C", "0", "1"]),
+ filePath: faker.system.filePath(),
+ approvedDate: faker.helpers.maybe(() => faker.date.recent({ days: 10 })),
+ })
+ }
+
+ // 리비전 데이터가 비어 있는지 확인
+ if (revData.length === 0) {
+ console.warn(`No revisions created for stage ${stage.id}`)
+ continue
+ }
+
+ try {
+ const insertedRevs = await db.insert(revisions).values(revData).returning()
+ console.log(`Inserted ${insertedRevs.length} revisions for stage ${stage.id}`)
+ } catch (error) {
+ console.error(`Error inserting revisions for stage ${stage.id}:`, error)
+ }
+ }
+ } catch (error) {
+ console.error(`Error inserting stages for document ${doc.id}:`, error)
+ }
+ }
+
+ console.log("Seeding completed successfully.")
+ } catch (err) {
+ console.error("Seeding error:", err)
+ process.exit(1)
+ } finally {
+ // 필요하다면 DB connection 종료 로직
+ }
+}
+
+// 스크립트 직접 실행 시
+seed()
+ .then(() => process.exit(0))
+ .catch(() => process.exit(1)) \ No newline at end of file