From 53dc7fc63d6815125ad1d673fc0d76c9c7c129b5 Mon Sep 17 00:00:00 2001 From: Vivek Date: Sun, 10 Jul 2022 21:08:58 +0530 Subject: [PATCH] crating seperate file for db Connection --- UsedbConnFile.js | 22 ++++++++++++++++++++++ dbConnFile.js | 14 ++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 UsedbConnFile.js create mode 100644 dbConnFile.js diff --git a/UsedbConnFile.js b/UsedbConnFile.js new file mode 100644 index 0000000..76fc8eb --- /dev/null +++ b/UsedbConnFile.js @@ -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('********#############*********'); \ No newline at end of file diff --git a/dbConnFile.js b/dbConnFile.js new file mode 100644 index 0000000..9c7fa94 --- /dev/null +++ b/dbConnFile.js @@ -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;