fac140b793
Fix dev-mode reconnect storm by persisting mongoose connection state on globalThis instead of a module-level flag that resets on Vite HMR. Eliminate redundant in_season DB query on /rezepte — derive seasonal subset from all_brief client-side. Parallelize all page load fetches. Replace N+1 settlement queries in balance route with single batch $in query. Parallelize balance sum and recent splits aggregations. Trim unused dateModified/dateCreated from recipe brief projections. Add indexes: Payment(date, createdAt), PaymentSplit(username), Recipe(short_name), Recipe(season).
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import mongoose from 'mongoose';
|
|
import { MONGO_URL } from '$env/static/private';
|
|
|
|
// Use globalThis to persist connection promise across Vite HMR module reloads
|
|
const g = globalThis as unknown as { __mongoosePromise?: Promise<typeof mongoose> };
|
|
|
|
export const dbConnect = async () => {
|
|
// Already connected — return immediately
|
|
if (mongoose.connection.readyState === 1) {
|
|
return mongoose.connection;
|
|
}
|
|
|
|
// Connection in progress — await the existing promise
|
|
if (mongoose.connection.readyState === 2 && g.__mongoosePromise) {
|
|
await g.__mongoosePromise;
|
|
return mongoose.connection;
|
|
}
|
|
|
|
try {
|
|
const options = {
|
|
maxPoolSize: 10,
|
|
serverSelectionTimeoutMS: 5000,
|
|
socketTimeoutMS: 45000,
|
|
};
|
|
|
|
g.__mongoosePromise = mongoose.connect(MONGO_URL ?? '', options);
|
|
await g.__mongoosePromise;
|
|
|
|
console.log('MongoDB connected with persistent connection');
|
|
return mongoose.connection;
|
|
} catch (error) {
|
|
console.error('MongoDB connection failed:', error);
|
|
g.__mongoosePromise = undefined;
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// No longer disconnect - let the connection pool manage connections
|
|
export const dbDisconnect = async () => {
|
|
// Keep connections persistent for performance and to avoid race conditions
|
|
// MongoDB driver will handle connection pooling and cleanup automatically
|
|
return;
|
|
};
|