Add missing Payment model and database connection utilities
- 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>
This commit is contained in:
44
src/lib/db/db.ts
Normal file
44
src/lib/db/db.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
14
src/lib/models/Payment.ts
Normal file
14
src/lib/models/Payment.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import mongoose from 'mongoose';
|
||||||
|
|
||||||
|
const paymentSchema = new mongoose.Schema({
|
||||||
|
paid_by: { type: String, required: true },
|
||||||
|
total_amount: { type: Number, required: true },
|
||||||
|
for_self: { type: Number, default: 0 },
|
||||||
|
for_other: { type: Number, default: 0 },
|
||||||
|
currency: { type: String, default: 'CHF' },
|
||||||
|
description: String,
|
||||||
|
date: { type: Date, default: Date.now },
|
||||||
|
receipt_image: String
|
||||||
|
});
|
||||||
|
|
||||||
|
export const Payment = mongoose.models.Payment || mongoose.model('Payment', paymentSchema);
|
Reference in New Issue
Block a user