-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeyValueDB.js
More file actions
63 lines (51 loc) · 1.29 KB
/
KeyValueDB.js
File metadata and controls
63 lines (51 loc) · 1.29 KB
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: database;
class KeyValueDB {
constructor(filename) {
if (filename.length == 0) {
console.error("No filename found");
}
this.fileManager = FileManager.local();
var directory = this.fileManager.libraryDirectory() + '/KeyValueDB/';
this.fileManager.createDirectory( directory, true );
this.filename = directory + filename + '.json';
if (!this.fileManager.fileExists( this.filename )) {
this.fileManager.writeString( this.filename, "{}" );
}
}
/*
* Get's the whole Key-Value Pairs
*/
getAll() {
return JSON.parse(
this.fileManager.readString(this.filename)
);
}
/*
* Get's a value by key
*/
getValue(key) {
return this.getAll()[ key ];
}
/*
* Set's the value of the specified key
*/
setValue(key, value) {
var db = this.getAll();
db[ key ] = value;
this.fileManager.writeString( this.filename, JSON.stringify(db) );
}
/*
* Append's a value to an array with the specified key
*/
append(key, value) {
var db = this.getAll();
if (db[key] == null) {
db[key] = [];
}
db[ key ].push( value );
this.fileManager.writeString( this.filename, JSON.stringify(db) );
}
}
module.exports = KeyValueDB;