-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
35 lines (30 loc) · 781 Bytes
/
Copy pathdb.js
File metadata and controls
35 lines (30 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;
const dbname = 'parkingSystem';
const url = 'mongodb://localhost:27017/parkingSystem';
const mongoOptions = {useNewUrlParser : true};
const state ={
db : null
};
const connect = (cb) =>{
if(state.db){
cb();
}else{
MongoClient.connect(url,mongoOptions,(err, client) => {
if(err){
console.log("Could not connect to database");
cb(err);
}else{
state.db = client.db(dbname);
cb();
}
});
}
}
const getDB = () =>{
return state.db;
}
const getPrimaryKey = (_id) =>{
return ObjectId(_id);
}
module.exports = {connect,getDB,getPrimaryKey};