-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdal.js
40 lines (35 loc) · 1019 Bytes
/
dal.js
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
36
37
38
39
40
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
let db = null;
// Connect to mongo
MongoClient.connect(url, { useNewUrlParser: true }, (err, client) => {
try{
db = client.db('banking');
console.log('Connected to MongoDB');
} catch(err){
console.log(err);
}
});
// Create user account
function create(name, email, password){
return new Promise((resolve, reject) => {
const collection = db.collection('users');
const doc = {name, email, password, balance: 0, deposits: [], withdraws: []};
collection.insertOne(doc, (err, result) => {
err ? reject(err) : resolve(result);
});
})
}
// Get all users
function getAll(){
return new Promise((resolve, reject) => {
const collection = db.collection('users');
collection.find({}).toArray((err, result) => {
err ? reject(err) : resolve(result);
});
});
}
module.exports = {
create,
getAll
}