-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdb.js
More file actions
42 lines (36 loc) · 831 Bytes
/
db.js
File metadata and controls
42 lines (36 loc) · 831 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
36
37
38
39
40
41
42
import { LowSync } from 'lowdb';
import { JSONFileSync } from 'lowdb/node';
const adapter = new JSONFileSync('db.json');
const db = new LowSync(adapter, {
token: '',
user: {},
selected_integration: {},
});
db.read();
db.write();
export function write(key, value) {
db.data[key] = value;
db.write();
}
export function read(key) {
return db.data[key];
}
export function addToList(key, value) {
const list = read(key);
if (Array.isArray(list)) {
list.push(value);
write(key, list);
} else {
console.error(`db.${key} is not a list.`);
}
}
export function query(key, query) {
const list = read(key);
if (Array.isArray(list)) {
// should this use filter ??
// what type do we want to return
return list.find(query);
} else {
console.error(`db.${key} can't be queried.`);
}
}