@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 102 lines 2.27 KB
Raw
1 import "dotenv/config";
2 import { closeDb, getDb } from "../src/prisma/db";
3
4 const seedUsers = [
5 {
6 email: "alice@example.com",
7 name: "Alice",
8 posts: [
9 {
10 title: "Deploying TanStack Start with Alchemy",
11 excerpt: "One stack owns Prisma Postgres, Prisma Compute, and app env.",
12 },
13 {
14 title: "Local dev without cloud credentials",
15 excerpt: "alchemy dev starts @prisma/dev and passes DATABASE_URL in.",
16 },
17 ],
18 },
19 {
20 email: "bob@example.com",
21 name: "Bob",
22 posts: [
23 {
24 title: "Prisma ORM query helpers",
25 excerpt: "The route loader reads through src/prisma/queries.ts.",
26 },
27 ],
28 },
29 ] as const;
30
31 async function main() {
32 const db = getDb();
33 const runtime = db.runtime();
34
35 for (const user of seedUsers) {
36 const existing = await runtime.query(
37 db.sql.public.user
38 .select("id", "email")
39 .where((fields, fns) => fns.eq(fields.email, user.email))
40 .limit(1)
41 .build(),
42 );
43
44 const existingUser = existing[0];
45 if (existingUser) {
46 await runtime.execute(
47 db.sql.public.post
48 .delete()
49 .where((fields, fns) => fns.eq(fields.userId, existingUser.id))
50 .build(),
51 );
52 await runtime.execute(
53 db.sql.public.user
54 .delete()
55 .where((fields, fns) => fns.eq(fields.id, existingUser.id))
56 .build(),
57 );
58 }
59
60 const created = await runtime.query(
61 db.sql.public.user
62 .insert([
63 {
64 email: user.email,
65 name: user.name,
66 },
67 ])
68 .returning("id", "email")
69 .build(),
70 );
71
72 const createdUser = created[0];
73 if (!createdUser) {
74 throw new Error(`Failed to seed ${user.email}`);
75 }
76
77 for (const post of user.posts) {
78 await runtime.execute(
79 db.sql.public.post
80 .insert([
81 {
82 title: post.title,
83 excerpt: post.excerpt,
84 userId: createdUser.id,
85 },
86 ])
87 .build(),
88 );
89 }
90 }
91
92 console.log(`Seeded ${seedUsers.length} users.`);
93 }
94
95 main()
96 .catch((error) => {
97 console.error("Seed failed:", error);
98 process.exitCode = 1;
99 })
100 .finally(async () => {
101 await closeDb();
102 });