summaryrefslogtreecommitdiff
path: root/db/seeds_2/taskSeed.ts
blob: 24fcc0e8d036ed5b9c92baee28a78c736a78ffd3 (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
39
40
41
42
43
44
45
46
import db from "@/db/db"
import { tasks } from "@/db/schema/tasks"
import { generateId } from "@/lib/id"
import { faker } from "@faker-js/faker"
export type NewTask = typeof tasks.$inferInsert


function generateRandomTask(): NewTask {
    return {
      id: generateId("task"),
      code: undefined,
      title: faker.hacker
        .phrase()
        .replace(/^./, (letter) => letter.toUpperCase()),
      status: faker.helpers.shuffle(tasks.status.enumValues)[0] ?? "todo",
      label: faker.helpers.shuffle(tasks.label.enumValues)[0] ?? "bug",
      priority: faker.helpers.shuffle(tasks.priority.enumValues)[0] ?? "low",
      archived: faker.datatype.boolean({ probability: 0.2 }),
      createdAt: new Date(),
      updatedAt: new Date(),
    }
  }


export async function seedTasks(input: { count: number }) {
  try {
    const allTasks: NewTask[] = []

    for (let i = 0; i < input.count; i++) {
      allTasks.push(generateRandomTask())
    }

    console.log("📝 Inserting tasks", allTasks.length)

    const result = await db.insert(tasks)
      .values(allTasks)
      .onConflictDoNothing()
      .returning()

    console.log(`✅ Successfully added ${result.length} new tasks`)
    return result
  } catch (err) {
    console.error("Failed to seed tasks:", err)
    throw err
  }
}