Skip to content

Commit

Permalink
final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
swatimoluguri committed May 17, 2024
1 parent cedb32e commit 6c597b1
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 205 deletions.
106 changes: 91 additions & 15 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,97 @@ const { DB_URI } = process.env;
let dbConnection;

module.exports = {
connectToDb: (cb) => {
MongoClient.connect(DB_URI)
.then((client) => {
dbConnection = client.db();
return cb();
})
.catch((err) => {
console.log(err);
return cb(err);
connectToDb: async () => {
try {
const client = new MongoClient(DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await client.connect();
console.log("MongoDB connected successfully");
dbConnection = client.db("e-commerce");
return dbConnection;
} catch (err) {
console.error("MongoDB connection error:", err);
throw err;
}
},
getDb: () => dbConnection,
getOrderModel: () => dbConnection.collection("orders"),
getUserModel: () => dbConnection.collection("users"),
getCustomerEnquiries: () => dbConnection.collection("customer-enquiries"),
getNewsletter: () => dbConnection.collection("newsletters"),
getResetPwd: () => dbConnection.collection("reset-password"),
startServer: async (app, port) => {
try {
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}

await app.listen(port);
console.log(`App listening on port ${port}`);
} catch (err) {
console.error('Error starting server:', err);
throw err;
}
},

getOrderModel: () => {
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("orders");
},

getCategoryModel:()=>{
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("categories");
},

getProductsModel:()=>{
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("products");
},

getFaqsModel:()=>{
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("faqs");
},


getUserModel: () => {
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("users");
},

getCartModel: () => {
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("cart");
},


getCustomerEnquiries: () => {
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("customer-enquiries");
},

getNewsletter: () => {
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("newsletters");
},

getResetPwd: () => {
if (!dbConnection) {
throw new Error('Database connection has not been established.');
}
return dbConnection.collection("reset-password");
}
};
Loading

0 comments on commit 6c597b1

Please sign in to comment.