-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.js
48 lines (39 loc) · 1.07 KB
/
store.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
'use strict';
const jsonfile = require('jsonfile');
const fileExists = require('file-exists');
const DATA_FILE = './db/data.json';
let data;
function init() {
!fileExists.sync(DATA_FILE) && jsonfile.writeFileSync(DATA_FILE, {});
data = jsonfile.readFileSync(DATA_FILE);
}
function getValues() {
let keys = Object.keys(data);
return keys.map(v => { return data[v]; });
}
function getUsers() {
return Object.keys(data);
}
var saveSettings = save.bind(null, 'settings');
var saveToken = save.bind(null, 'token');
var getSettings = get.bind(null, 'settings');
var getToken = get.bind(null, 'token');
function save(key, userId, obj) {
return new Promise((resolve, reject) => {
data[userId] = data[userId] || {};
data[userId][key] = obj;
jsonfile.writeFile(DATA_FILE, data, { spaces: 2 }, err => err ? reject(err) : resolve());
});
}
function get(key, userId) {
return !!data[userId] && data[userId][key];
}
module.exports = {
init,
getUsers,
getValues,
saveToken,
getToken,
saveSettings,
getSettings
};