Added Database Integration
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
"@types/react": "^19.1.1",
|
"@types/react": "^19.1.1",
|
||||||
"@types/react-dom": "^19.1.2",
|
"@types/react-dom": "^19.1.2",
|
||||||
"tailwindcss": "^4.1.3",
|
"tailwindcss": "^4.1.3",
|
||||||
"typescript": "^5.8.3"
|
"typescript": "^5.8.3",
|
||||||
|
"@types/bun": "latest"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/app/api/index.js
Normal file
10
src/app/api/index.js
Normal file
@@ -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`);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/lib/connectDB.js
Normal file
33
src/lib/connectDB.js
Normal file
@@ -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;
|
||||||
Reference in New Issue
Block a user