fix: skip database connection during build when MONGO_URL is not set
All checks were successful
CI / build-and-deploy (push) Successful in 1m23s
All checks were successful
CI / build-and-deploy (push) Successful in 1m23s
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.
This commit is contained in:
@@ -7,20 +7,25 @@ import { sequence } from "@sveltejs/kit/hooks"
|
|||||||
import * as auth from "./auth"
|
import * as auth from "./auth"
|
||||||
import { initializeScheduler } from "./lib/server/scheduler"
|
import { initializeScheduler } from "./lib/server/scheduler"
|
||||||
import { dbConnect } from "./utils/db"
|
import { dbConnect } from "./utils/db"
|
||||||
|
import { env } from "$env/dynamic/private"
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
|
||||||
// Initialize database connection on server startup
|
// Initialize database connection on server startup (skip during build when no env vars)
|
||||||
console.log('🚀 Server starting - initializing database connection...');
|
if (env.MONGO_URL) {
|
||||||
await dbConnect().then(() => {
|
console.log('🚀 Server starting - initializing database connection...');
|
||||||
console.log('✅ Database connected successfully');
|
await dbConnect().then(() => {
|
||||||
// Initialize the recurring payment scheduler after DB is ready
|
console.log('✅ Database connected successfully');
|
||||||
initializeScheduler();
|
// Initialize the recurring payment scheduler after DB is ready
|
||||||
console.log('✅ Recurring payment scheduler initialized');
|
initializeScheduler();
|
||||||
}).catch((error) => {
|
console.log('✅ Recurring payment scheduler initialized');
|
||||||
console.error('❌ Failed to connect to database on startup:', error);
|
}).catch((error) => {
|
||||||
// Don't crash the server - API routes will attempt reconnection
|
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 }) {
|
async function authorization({ event, resolve }) {
|
||||||
const session = await event.locals.auth();
|
const session = await event.locals.auth();
|
||||||
|
|||||||
Reference in New Issue
Block a user