summaryrefslogtreecommitdiff
path: root/db/seeds/companyseed.ts
diff options
context:
space:
mode:
Diffstat (limited to 'db/seeds/companyseed.ts')
-rw-r--r--db/seeds/companyseed.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/db/seeds/companyseed.ts b/db/seeds/companyseed.ts
new file mode 100644
index 00000000..b9d477f5
--- /dev/null
+++ b/db/seeds/companyseed.ts
@@ -0,0 +1,35 @@
+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 }) {
+ const count = input.count ?? 100
+
+ try {
+ const allCompanies: NewCompany[] = []
+
+ for (let i = 0; i < count; i++) {
+ allCompanies.push(generateRandomCompany())
+ }
+
+ await db.delete(companies)
+
+ console.log("📝 Inserting companies", allCompanies.length)
+
+ await db.insert(companies).values(allCompanies).onConflictDoNothing()
+ } catch (err) {
+ console.error(err)
+ }
+}