Skip to content

Commit

Permalink
crating seperate file for db Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Jul 10, 2022
1 parent e77304d commit 53dc7fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions UsedbConnFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const dbConnect = require('./dbConnFile');

//handling the returned promise using promise

dbConnect().then((res) => {
res.find().toArray().then((data) => {
console.log(data);
});
});



//handling the returned promise using async-await

// const main = async () => {
// let data = await dbConnect();
// data = await data.find().toArray();
// console.log(data);
// }
// main();

console.log('********#############*********');
14 changes: 14 additions & 0 deletions dbConnFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {MongoClient} = require('mongodb');
const url = 'mongodb://localhost:27017';
const database = 'e-comm';
const Client = new MongoClient(url);


//connecting to database
async function dbConnect(){
let result = await Client.connect();
let db = result.db(database);
return db.collection('products');
}

module.exports = dbConnect;

0 comments on commit 53dc7fc

Please sign in to comment.