|
| 1 | +import { prisma } from "./app/db.server"; |
| 2 | +import { createOrganization } from "./app/models/organization.server"; |
| 3 | +import { createProject } from "./app/models/project.server"; |
| 4 | +import { AuthenticationMethod } from "@trigger.dev/database"; |
| 5 | + |
| 6 | +async function seed() { |
| 7 | + console.log("🌱 Starting seed..."); |
| 8 | + |
| 9 | + // Create or find the local user |
| 10 | + let user = await prisma.user.findUnique({ |
| 11 | + where: { email: "[email protected]" }, |
| 12 | + }); |
| 13 | + |
| 14 | + if (!user) { |
| 15 | + console.log("Creating local user..."); |
| 16 | + user = await prisma.user.create({ |
| 17 | + data: { |
| 18 | + |
| 19 | + authenticationMethod: AuthenticationMethod.MAGIC_LINK, |
| 20 | + name: "Local Developer", |
| 21 | + displayName: "Local Developer", |
| 22 | + admin: true, |
| 23 | + confirmedBasicDetails: true, |
| 24 | + }, |
| 25 | + }); |
| 26 | + console.log(`✅ Created user: ${user.email} (${user.id})`); |
| 27 | + } else { |
| 28 | + console.log(`✅ User already exists: ${user.email} (${user.id})`); |
| 29 | + } |
| 30 | + |
| 31 | + // Create or find the references organization |
| 32 | + // Look for an organization where the user is a member and the title is "References" |
| 33 | + let organization = await prisma.organization.findFirst({ |
| 34 | + where: { |
| 35 | + title: "References", |
| 36 | + members: { |
| 37 | + some: { |
| 38 | + userId: user.id, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + if (!organization) { |
| 45 | + console.log("Creating references organization..."); |
| 46 | + organization = await createOrganization({ |
| 47 | + title: "References", |
| 48 | + userId: user.id, |
| 49 | + companySize: "1-10", |
| 50 | + }); |
| 51 | + console.log(`✅ Created organization: ${organization.title} (${organization.slug})`); |
| 52 | + } else { |
| 53 | + console.log(`✅ Organization already exists: ${organization.title} (${organization.slug})`); |
| 54 | + } |
| 55 | + |
| 56 | + // Define the reference projects with their specific project refs |
| 57 | + const referenceProjects = [ |
| 58 | + { |
| 59 | + name: "hello-world", |
| 60 | + externalRef: "proj_rrkpdguyagvsoktglnod", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "d3-chat", |
| 64 | + externalRef: "proj_cdmymsrobxmcgjqzhdkq", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "realtime-streams", |
| 68 | + externalRef: "proj_klxlzjnzxmbgiwuuwhvb", |
| 69 | + }, |
| 70 | + ]; |
| 71 | + |
| 72 | + // Create or find each project |
| 73 | + for (const projectConfig of referenceProjects) { |
| 74 | + let project = await prisma.project.findUnique({ |
| 75 | + where: { externalRef: projectConfig.externalRef }, |
| 76 | + }); |
| 77 | + |
| 78 | + if (!project) { |
| 79 | + console.log(`Creating project: ${projectConfig.name}...`); |
| 80 | + project = await createProject({ |
| 81 | + organizationSlug: organization.slug, |
| 82 | + name: projectConfig.name, |
| 83 | + userId: user.id, |
| 84 | + version: "v3", |
| 85 | + }); |
| 86 | + |
| 87 | + // Update the externalRef to match the expected value |
| 88 | + project = await prisma.project.update({ |
| 89 | + where: { id: project.id }, |
| 90 | + data: { externalRef: projectConfig.externalRef }, |
| 91 | + }); |
| 92 | + |
| 93 | + console.log(`✅ Created project: ${project.name} (${project.externalRef})`); |
| 94 | + } else { |
| 95 | + console.log(`✅ Project already exists: ${project.name} (${project.externalRef})`); |
| 96 | + } |
| 97 | + |
| 98 | + // List the environments for this project |
| 99 | + const environments = await prisma.runtimeEnvironment.findMany({ |
| 100 | + where: { projectId: project.id }, |
| 101 | + select: { |
| 102 | + slug: true, |
| 103 | + type: true, |
| 104 | + apiKey: true, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + console.log(` Environments for ${project.name}:`); |
| 109 | + for (const env of environments) { |
| 110 | + console.log(` - ${env.type.toLowerCase()} (${env.slug}): ${env.apiKey}`); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + console.log("\n🎉 Seed complete!\n"); |
| 115 | + console.log("Summary:"); |
| 116 | + console.log(`User: ${user.email}`); |
| 117 | + console.log(`Organization: ${organization.title} (${organization.slug})`); |
| 118 | + console.log(`Projects: ${referenceProjects.map((p) => p.name).join(", ")}`); |
| 119 | + console.log("\n⚠️ Note: Update the .env files in d3-chat and realtime-streams with:"); |
| 120 | + console.log(` - d3-chat: TRIGGER_PROJECT_REF=proj_cdmymsrobxmcgjqzhdkq`); |
| 121 | + console.log(` - realtime-streams: TRIGGER_PROJECT_REF=proj_klxlzjnzxmbgiwuuwhvb`); |
| 122 | +} |
| 123 | + |
| 124 | +seed() |
| 125 | + .catch((e) => { |
| 126 | + console.error("❌ Seed failed:"); |
| 127 | + console.error(e); |
| 128 | + process.exit(1); |
| 129 | + }) |
| 130 | + .finally(async () => { |
| 131 | + await prisma.$disconnect(); |
| 132 | + }); |
0 commit comments