Database connect and Mongoose models

This commit is contained in:
Brad Traversy
2024-07-09 11:51:16 -04:00
parent 26440f4118
commit abfd74dc44
5 changed files with 430 additions and 0 deletions
+24
View File
@@ -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;