summaryrefslogtreecommitdiff
path: root/db/seeds_2/companySeed.ts
blob: 4293b02b8363a3730c4fa6f7196ec61bb0474a2e (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
36
37
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
  }
}