fix: implement persistent MongoDB connections and resolve race conditions

- Replace connect/disconnect pattern with persistent connection pool
- Add explicit database initialization on server startup
- Remove all dbDisconnect() calls from API endpoints to prevent race conditions
- Fix MongoNotConnectedError when scheduler runs concurrently with API requests
- Add connection pooling with proper MongoDB driver options
- Add safety check for recipes array in favorites utility

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-14 19:53:55 +02:00
parent 08d7d8541b
commit c8e542eec8
28 changed files with 198 additions and 111 deletions

View File

@@ -1,7 +1,7 @@
import type { RequestHandler } from '@sveltejs/kit';
import { Payment } from '../../../../../models/Payment';
import { PaymentSplit } from '../../../../../models/PaymentSplit';
import { dbConnect, dbDisconnect } from '../../../../../utils/db';
import { dbConnect } from '../../../../../utils/db';
import { error, json } from '@sveltejs/kit';
export const GET: RequestHandler = async ({ params, locals }) => {
@@ -26,7 +26,7 @@ export const GET: RequestHandler = async ({ params, locals }) => {
if (e.status === 404) throw e;
throw error(500, 'Failed to fetch payment');
} finally {
await dbDisconnect();
// Connection will be reused
}
};
@@ -88,7 +88,7 @@ export const PUT: RequestHandler = async ({ params, request, locals }) => {
if (e.status) throw e;
throw error(500, 'Failed to update payment');
} finally {
await dbDisconnect();
// Connection will be reused
}
};
@@ -121,6 +121,6 @@ export const DELETE: RequestHandler = async ({ params, locals }) => {
if (e.status) throw e;
throw error(500, 'Failed to delete payment');
} finally {
await dbDisconnect();
// Connection will be reused
}
};