-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crating seperate file for db Connection
- Loading branch information
1 parent
e77304d
commit 53dc7fc
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('********#############*********'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |