From 3bb6fd21871deef39680ea6b0087930c3faea649 Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Tue, 9 Dec 2025 11:38:17 +0100 Subject: [PATCH] fix: skip database connection during build when MONGO_URL is not set Add check for MONGO_URL existence before attempting database connection in hooks.server.ts. This prevents build failures in CI environments where no .env file is present, while still initializing the connection properly at runtime on the server. --- src/hooks.server.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index e60cae0..ed3dba7 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -7,20 +7,25 @@ import { sequence } from "@sveltejs/kit/hooks" import * as auth from "./auth" import { initializeScheduler } from "./lib/server/scheduler" import { dbConnect } from "./utils/db" +import { env } from "$env/dynamic/private" import fs from 'fs' import path from 'path' -// Initialize database connection on server startup -console.log('🚀 Server starting - initializing database connection...'); -await dbConnect().then(() => { - console.log('✅ Database connected successfully'); - // Initialize the recurring payment scheduler after DB is ready - initializeScheduler(); - console.log('✅ Recurring payment scheduler initialized'); -}).catch((error) => { - console.error('❌ Failed to connect to database on startup:', error); - // Don't crash the server - API routes will attempt reconnection -}); +// Initialize database connection on server startup (skip during build when no env vars) +if (env.MONGO_URL) { + console.log('🚀 Server starting - initializing database connection...'); + await dbConnect().then(() => { + console.log('✅ Database connected successfully'); + // Initialize the recurring payment scheduler after DB is ready + initializeScheduler(); + console.log('✅ Recurring payment scheduler initialized'); + }).catch((error) => { + console.error('❌ Failed to connect to database on startup:', error); + // Don't crash the server - API routes will attempt reconnection + }); +} else { + console.log('⚠️ MONGO_URL not set - skipping database initialization (build mode)'); +} async function authorization({ event, resolve }) { const session = await event.locals.auth();