diff options
| author | schererleander <leander@schererleander.de> | 2025-07-02 22:17:57 +0200 |
|---|---|---|
| committer | schererleander <leander@schererleander.de> | 2025-07-02 22:17:57 +0200 |
| commit | ab03900adf080da08a0b2a3628fd0dcf0af28420 (patch) | |
| tree | a54ddb49b949929c5c6b4b0bd2b7ca5c504da54d /src/lib/mongodb.ts | |
| parent | 198f95f079078e60e05d1ea6607ee14f79721a7e (diff) | |
feat: add libraries and utilities
Diffstat (limited to 'src/lib/mongodb.ts')
| -rw-r--r-- | src/lib/mongodb.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/lib/mongodb.ts b/src/lib/mongodb.ts new file mode 100644 index 0000000..1fc60d4 --- /dev/null +++ b/src/lib/mongodb.ts @@ -0,0 +1,30 @@ +import mongoose from "mongoose"; + +const MONGODB_URI = process.env.MONGODB_URI; + +if (!MONGODB_URI) { + throw new Error("Please define the MONGODB_URI environment variable inside .env.local"); +} + +interface Connection { + isConnected?: number; +} + +const connection: Connection = {}; + +async function dbConnect(): Promise<void> { + if (connection.isConnected) { + return; + } + + try { + const db = await mongoose.connect(MONGODB_URI!); + connection.isConnected = db.connections[0].readyState; + console.log("MongoDB connected successfully"); + } catch (error) { + console.error("MongoDB connection error:", error); + throw error; + } +} + +export default dbConnect;
\ No newline at end of file |
