/** * Canlı başlatma (Docker entrypoint, uygulamadan önce): replica set kontrolü, bekleyen migration'lar * ve SEED_ADMIN_* tanımlıysa ilk admin hesabı (varsa dokunulmaz). İdempotent; her deploy'da çalışır. * İmajda esbuild ile tek dosyaya derlenir (scripts/bootstrap.mjs). Lokal: npm run bootstrap */ import mongoose from "mongoose"; import { getDb, getMongoClient } from "@/lib/db/mongo"; import { env } from "@/lib/env"; import { runMigrations } from "./migrations"; import { closeSeedConnections, runSeed } from "./seed/run"; async function main() { const e = env(); const hello = await getDb().admin().command({ hello: 1 }); if (!hello.setName) { throw new Error( "MongoDB replica set olarak çalışmıyor; transaction'lı yazmalar başarısız olur. " + "docs/DEPLOY.md › 'MongoDB replica set' adımlarını uygulayın.", ); } if (!hello.isWritablePrimary) throw new Error("MongoDB düğümü PRIMARY değil (yazılamaz)."); console.log(`• MongoDB ${hello.setName} PRIMARY`); if (e.NODE_ENV === "production" && e.SEED_ADMIN_EMAIL && e.SEED_ADMIN_PASSWORD) { // Production'da seed yalnızca migration + admin yapar (örnek veri oluşturulmaz). await runSeed(); } else { await runMigrations(); if (e.NODE_ENV === "production") console.log("• SEED_ADMIN_* tanımlı değil; admin kontrolü atlandı."); } } main() .catch((err: unknown) => { console.error(`✗ Başlatma hatası: ${err instanceof Error ? err.message : err}`); process.exitCode = 1; }) .finally(async () => { await closeSeedConnections(); await mongoose.disconnect(); await getMongoClient().close(); });