From 20163c4583a9d1bcdab897f221d17c7050ea5181 Mon Sep 17 00:00:00 2001 From: SKULL-GOD Date: Sat, 12 Apr 2025 21:47:08 -0400 Subject: [PATCH] Added Database Integration --- package.json | 3 ++- src/app/api/index.js | 10 ++++++++++ src/lib/connectDB.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/app/api/index.js create mode 100644 src/lib/connectDB.js diff --git a/package.json b/package.json index bf24238..0a9186d 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@types/react": "^19.1.1", "@types/react-dom": "^19.1.2", "tailwindcss": "^4.1.3", - "typescript": "^5.8.3" + "typescript": "^5.8.3", + "@types/bun": "latest" } } diff --git a/src/app/api/index.js b/src/app/api/index.js new file mode 100644 index 0000000..a34bd2d --- /dev/null +++ b/src/app/api/index.js @@ -0,0 +1,10 @@ +export default function handler(req, res) { + if (req.method === 'GET') { + // Handle GET request + res.status(200).json({ message: 'Hello, this is a GET request!' }); + } else { + // Handle unsupported methods + res.setHeader('Allow', ['GET']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +} \ No newline at end of file diff --git a/src/lib/connectDB.js b/src/lib/connectDB.js new file mode 100644 index 0000000..d5be20a --- /dev/null +++ b/src/lib/connectDB.js @@ -0,0 +1,33 @@ +import mongoose from "mongoose"; + +const DATABASE_URL = process.env.DATABASE_URL; + +if (!DATABASE_URL) { + throw new Error("Please define the DATABASE_URL environment variable inside .env.local"); +} + +let cached = global.mongoose; + +if (!cached) { + cached = global.mongoose = { conn: null, promise: null }; +} + +async function connectDB() { + if (cached.conn) { + return cached.conn; + } + + if (!cached.promise) { + const opts = { + bufferCommands: false, + }; + + cached.promise = mongoose.connect(DATABASE_URL, opts).then((mongoose) => { + return mongoose; + }); + } + cached.conn = await cached.promise; + return cached.conn; +} + +export default connectDB; \ No newline at end of file