Database connect and Mongoose models
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
let connected = false;
|
||||
|
||||
const connectDB = async () => {
|
||||
mongoose.set('strictQuery', true);
|
||||
|
||||
// If the database is already connected, don't connect again
|
||||
if (connected) {
|
||||
console.log('MongoDB is already connected...');
|
||||
return;
|
||||
}
|
||||
|
||||
// Connect to MongoDB
|
||||
try {
|
||||
await mongoose.connect(process.env.MONGODB_URI);
|
||||
connected = true;
|
||||
console.log('MongoDB connected...');
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
|
||||
export default connectDB;
|
||||
Reference in New Issue
Block a user