summaryrefslogtreecommitdiff
path: root/db/seeds/companyseed.ts
blob: b9d477f55b8c997d01c6262c3d303d263efb7be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)
  }
}