-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
43 lines (32 loc) · 905 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
const low = require('lowdb');
const shortId = require('shortid');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('data.json');
const db = low(adapter);
const timersDb = db.get('timers');
module.exports.getAllTimers = () => timersDb.value();
module.exports.addTimer = timer => {
const newTimer = {
id: shortId.generate(),
title: timer.title,
minutes: timer.minutes,
seconds: timer.seconds,
};
timersDb.push(newTimer).write();
};
module.exports.getTimerById = id => {
return timersDb.find({ id }).value();
};
module.exports.deleteTimer = timerId => {
timersDb.remove({ id: timerId }).write();
};
module.exports.deleteAllTimers = () => {
db.set('timers', []).write();
};
module.exports.updateTimer = (id, updatedTimer) => {
return timersDb
.find({ id })
.assign({ ...updatedTimer })
.value();
};
module.exports.db = db;