file-db.js
1.74 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const nedb = require('nedb');
const path = require('path');
const db = new nedb({
filename: path.join(__dirname, '../db/user.db'),
autoload: true
})
const fileDb = {
findAll() {
return new Promise((resolve, reject) => {
db.find({}, function (err, doc) {
if (err) {
return reject(err);
}
return resolve(doc);
});
})
},
findOne(info) {
return new Promise((resolve, reject) => {
db.findOne(info, function (err, doc) {
if (err) {
return reject(err);
}
resolve(doc);
})
})
},
insert(info) {
return new Promise((resolve, reject) => {
db.insert(info, function (err, doc) {
if (err) {
return reject(err)
}
resolve(doc);
})
})
},
update(id, doc) {
return new Promise((resolve, reject) => {
db.update({
_id: id
}, {
$set: doc
}, {}, function (err, numAffected) {
if (err) {
return reject(err);
}
resolve(numAffected);
});
})
},
remove(id) {
return new Promise((resolve, reject) => {
db.remove({
_id: id
}, {
returnUpdatedDocs: true
}, function (err, numAffected, affectedDocuments) {
if (err) {
return reject(err);
}
resolve(affectedDocuments);
});
})
}
}
module.exports = fileDb;