diff --git a/prisma/seed.ts b/prisma/seed.ts index 3c870ee..7d2d143 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -133,7 +133,18 @@ async function main() { console.log("Database seeding completed."); } -main().catch((error) => { - console.error("Error during seeding:", error); - process.exit(1); -}); +// Only auto-execute when run directly (not when imported) +const isMainModule = + typeof require !== "undefined" + ? require.main === module + : import.meta.path.endsWith("seed.ts"); + +if (isMainModule) { + main().catch((error) => { + console.error("Error during seeding:", error); + process.exit(1); + }); +} + +// Export for programmatic use +export { seedAdminUser, seedDemoUsers, main as runSeed }; diff --git a/src/index.ts b/src/index.ts index e228069..a8ab986 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,18 @@ const PORT = process.env.PORT || 3000; const isProduction = process.env.NODE_ENV === "production"; +// Auto-seed database in production (ensure admin user exists) +if (isProduction && process.env.ADMIN_EMAIL) { + try { + console.log("🌱 Running database seed in production..."); + const { runSeed } = await import("../prisma/seed.ts"); + await runSeed(); + } catch (error) { + console.error("⚠️ Production seed failed:", error); + // Don't crash the server if seed fails + } +} + const app = new Elysia().use(api); if (!isProduction) {