- Created Payment model with mongoose schema for cospend functionality - Added database connection utilities with proper connection caching - Fixed build errors related to missing imports - Build now succeeds and dev server starts correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import mongoose from 'mongoose';
|
|
|
|
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/recipes';
|
|
|
|
if (!MONGODB_URI) {
|
|
throw new Error('Please define the MONGODB_URI environment variable inside .env.local');
|
|
}
|
|
|
|
/**
|
|
* Global is used here to maintain a cached connection across hot reloads
|
|
* in development. This prevents connections growing exponentially
|
|
* during API Route usage.
|
|
*/
|
|
let cached = (global as any).mongoose;
|
|
|
|
if (!cached) {
|
|
cached = (global as any).mongoose = { conn: null, promise: null };
|
|
}
|
|
|
|
export async function dbConnect() {
|
|
if (cached.conn) {
|
|
return cached.conn;
|
|
}
|
|
|
|
if (!cached.promise) {
|
|
const opts = {
|
|
bufferCommands: false,
|
|
};
|
|
|
|
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
|
|
return mongoose;
|
|
});
|
|
}
|
|
cached.conn = await cached.promise;
|
|
return cached.conn;
|
|
}
|
|
|
|
export async function dbDisconnect() {
|
|
if (cached.conn) {
|
|
await cached.conn.disconnect();
|
|
cached.conn = null;
|
|
cached.promise = null;
|
|
}
|
|
} |