-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
50 lines (46 loc) · 972 Bytes
/
db.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
41
42
43
44
45
46
47
48
49
50
const Datastore = require('nedb');
const path = require('path');
// Import Datastore
const db = new Datastore({
filename: path.resolve(__dirname, './todo.db'),
autoload: true,
});
db.loadDatabase((err) => {
if (err) {
throw err;
}
});
// Adds an item to the NeDB
const newItem = item =>
new Promise((resolve, reject) =>
db.insert(item, (err) => {
if (err) {
reject(err);
return;
}
resolve();
}));
// Gets the items from the NeDB
const getItems = () =>
new Promise((resolve, reject) => {
db
.find({})
.sort({ stamp: 1 })
.exec((err, items) => {
if (err) {
reject(err);
return;
}
resolve(items);
});
});
const deleteItem = stamp =>
new Promise((resolve, reject) => {
db.remove({ stamp }, {}, (err) => {
if (err) {
reject(err);
}
resolve();
});
});
module.exports = { newItem, getItems, deleteItem };