summaryrefslogtreecommitdiff
path: root/db/seeds_2/companySeed.ts
diff options
context:
space:
mode:
Diffstat (limited to 'db/seeds_2/companySeed.ts')
-rw-r--r--db/seeds_2/companySeed.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/db/seeds_2/companySeed.ts b/db/seeds_2/companySeed.ts
new file mode 100644
index 00000000..4293b02b
--- /dev/null
+++ b/db/seeds_2/companySeed.ts
@@ -0,0 +1,38 @@
+import db from "@/db/db"
+import { companies } from "@/db/schema/companies"
+import { faker } from "@faker-js/faker"
+export type NewCompany = typeof companies.$inferInsert
+
+function generateRandomCompany(): NewCompany {
+ return {
+ // Drizzle에서 기본 키(id)는 자동 생성이므로 제외
+ name: faker.company.name(),
+ taxID: faker.number.int({ min: 100000000, max: 999999999 }),
+ // createdAt은 defaultNow()가 있지만, 예시로 faker를 써서 과거 시점을 넣고 싶다면:
+ createdAt: faker.date.past()
+ }
+}
+
+export async function seedCompanies(input: { count: number }) {
+ try {
+ const allCompanies: NewCompany[] = []
+
+ for (let i = 0; i < input.count; i++) {
+ allCompanies.push(generateRandomCompany())
+ }
+
+ console.log("📝 Inserting companies", allCompanies.length)
+
+ // onConflictDoNothing을 사용하여 중복되는 데이터는 건너뛰고 새로운 데이터만 추가
+ const result = await db.insert(companies)
+ .values(allCompanies)
+ .onConflictDoNothing()
+ .returning()
+
+ console.log(`✅ Successfully added ${result.length} new companies`)
+ return result
+ } catch (err) {
+ console.error("Failed to seed companies:", err)
+ throw err
+ }
+}