Skip to content

Commit

Permalink
Using mongoose for db connection &
Browse files Browse the repository at this point in the history
 improved dbConfig
  • Loading branch information
ad956 committed Jul 1, 2024
1 parent c72fee4 commit f153119
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/app/utils/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mongoose from "mongoose";

const connection: { isConnected?: number } = {};

let uri: string;
if (process.env.NODE_ENV === "production") {
uri = process.env.MONGODB_URI || "mongodb://localhost:27017/";
} else {
uri = "mongodb://localhost:27017/";
}

export default async function dbConfig() {
if (connection.isConnected) {
return;
}

const db = await mongoose.connect(uri, {
dbName: "pft",
});

connection.isConnected = db.connections[0].readyState;
}

0 comments on commit f153119

Please sign in to comment.