From b847b8f1c8090eb41b175e7f2a5dbace94cbec79 Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Sun, 31 Aug 2025 21:03:15 +0200 Subject: [PATCH] Add missing Payment model and database connection utilities MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/lib/db/db.ts | 44 +++++++++++++++++++++++++++++++++++++++ src/lib/models/Payment.ts | 14 +++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/lib/db/db.ts create mode 100644 src/lib/models/Payment.ts diff --git a/src/lib/db/db.ts b/src/lib/db/db.ts new file mode 100644 index 0000000..5f4aeef --- /dev/null +++ b/src/lib/db/db.ts @@ -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; + } +} \ No newline at end of file diff --git a/src/lib/models/Payment.ts b/src/lib/models/Payment.ts new file mode 100644 index 0000000..9632b4a --- /dev/null +++ b/src/lib/models/Payment.ts @@ -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); \ No newline at end of file