diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-03-26 00:37:41 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-03-26 00:37:41 +0000 |
| commit | e0dfb55c5457aec489fc084c4567e791b4c65eb1 (patch) | |
| tree | 68543a65d88f5afb3a0202925804103daa91bc6f /db/seeds_2/itmeSeed.ts | |
3/25 까지의 대표님 작업사항
Diffstat (limited to 'db/seeds_2/itmeSeed.ts')
| -rw-r--r-- | db/seeds_2/itmeSeed.ts | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/db/seeds_2/itmeSeed.ts b/db/seeds_2/itmeSeed.ts new file mode 100644 index 00000000..6d725c22 --- /dev/null +++ b/db/seeds_2/itmeSeed.ts @@ -0,0 +1,108 @@ +// db/seed/itemSeed.ts +import { faker } from "@faker-js/faker" +import db from "@/db/db" +import { items } from "../schema/items" +import { desc } from "drizzle-orm" + +function generateItemName(category: string): string { + const namePattern = Math.random() + if (namePattern < 0.4) { + // 일반적인 제품명 + return `${category} ${faker.commerce.productName()}` + } else if (namePattern < 0.7) { + // 기술 사양이 포함된 이름 + const spec = faker.number.int({ min: 100, max: 9999 }) + return `${category} ${faker.commerce.productName()} ${spec}` + } else { + // 브랜드가 포함된 이름 + const brand = faker.company.name().split(" ")[0] + return `${brand} ${category} ${faker.commerce.productName()}` + } +} + +async function generateItem(industry: string, category: string, index: number) { + const itemCode = `${industry.substring(0, 3).toUpperCase()}-${category.substring(0, 3).toUpperCase()}-${String(index + 1).padStart(4, "0")}` + + return { + itemCode, + itemName: generateItemName(category), + description: `${industry} - ${category}: ${faker.commerce.productDescription()}`, + } +} + +export async function seedItems(input: { itemsPerCategory: number }) { + try { + // 다양한 산업군별 아이템 카테고리 정의 + const categories = { + Electronics: ["Semiconductors", "PCB Components", "Sensors", "Displays", "Batteries"], + Automotive: ["Engine Parts", "Transmission", "Suspension", "Electrical", "Interior"], + Chemical: ["Industrial Chemicals", "Polymers", "Coatings", "Adhesives", "Solvents"], + Construction: ["Steel", "Concrete", "Lumber", "Insulation", "Hardware"], + Manufacturing: ["Tools", "Machinery", "Spare Parts", "Raw Materials", "Equipment"] + } as const; + + // 현재 가장 큰 itemCode 찾기 + const lastItems = await db.select() + .from(items) + .orderBy(desc(items.itemCode)) + .limit(Object.keys(categories).length * 5); // 각 산업별 서브카테고리 수 + + // 각 카테고리별 마지막 인덱스 추적 + const lastIndices: Record<keyof typeof categories, Record<string, number>> = { + Electronics: {}, + Automotive: {}, + Chemical: {}, + Construction: {}, + Manufacturing: {}, + }; + + // 기존 아이템의 itemCode에서 카테고리와 인덱스 추출 + for (const item of lastItems) { + if (!item.itemCode) continue; + + const [industryCode, categoryCode, indexStr] = item.itemCode.split("-"); + if (!industryCode || !categoryCode || !indexStr) continue; + + // 코드로부터 원래 카테고리 찾기 + const industry = Object.keys(categories).find(ind => ind.substring(0, 3).toUpperCase() === industryCode) as keyof typeof categories; + if (!industry) continue; + + const category = categories[industry].find(cat => cat.substring(0, 3).toUpperCase() === categoryCode); + if (!category) continue; + + if (!lastIndices[industry]) { + lastIndices[industry] = {}; + } + + const itemIndex = parseInt(indexStr); + if (!lastIndices[industry][category] || itemIndex > lastIndices[industry][category]) { + lastIndices[industry][category] = itemIndex; + } + } + + const allItems = []; + + for (const [industry, subcategories] of Object.entries(categories)) { + for (const category of subcategories) { + const startIndex = (lastIndices[industry as keyof typeof categories]?.[category] ?? 0); + + for (let i = 0; i < input.itemsPerCategory; i++) { + allItems.push(await generateItem(industry, category, startIndex + i)); + } + } + } + + console.log("📝 Inserting items", allItems.length); + + const result = await db.insert(items) + .values(allItems) + .onConflictDoNothing() + .returning(); + + console.log(`✅ Successfully added ${result.length} new items across ${Object.keys(categories).length} industries`); + return result; + } catch (err) { + console.error("Failed to seed items:", err); + throw err; + } +}
\ No newline at end of file |
