-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
63 lines (51 loc) · 1.57 KB
/
storage.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
51
52
53
54
55
56
57
58
59
60
61
62
63
const storage = require('node-persist');
const _ = require('lodash');
async function clearStorage () {
try {
await storage.clear();
console.log('[STORE] Storage cleared');
} catch (err) {
console.error(err);
}
}
async function initStorage() {
try {
await storage.init();
console.log('[STORE] Storage initiated');
} catch (err) {
console.error(err);
}
}
async function readKeys () {
try {
return await storage.keys();
} catch (err) {
console.error(err);
}
}
// IN [{name, tz_id}, {}, {}]
// will check if storage already has the keys
async function writeLocations (arr) {
try {
const exisitngKeys = await readKeys();
const newKeys = _.map(arr, loc => loc.name);
const missingKeys = _.difference(newKeys, exisitngKeys);
// console.log(exisitngKeys, newKeys, missingKeys);
if (missingKeys.length > 0) {
console.log(`[STORE] Will add ${missingKeys.join(', ')}`);
const missingArr = _.filter(arr, loc => _.includes(missingKeys, loc.name));
await missingArr.forEach(async lz => {
const {name, tz_id} = lz;
// console.log(name, tz_id);
await storage.setItem(name, tz_id);
})
return missingArr;
} else {
console.log('[STORE] No new locations written');
return [];
}
} catch (err) {
console.error(err);
}
};
module.exports = { initStorage, clearStorage ,writeLocations, readKeys };