-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstorage.js
143 lines (122 loc) · 2.85 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const os = require('os');
const path = require('path');
const fs = require('fs');
let dbdir = os.homedir() + '/cxdb';
let dbPath = path.join(os.homedir(), 'gx.json');
let dbHistoryPath = path.join(dbdir, 'history.json');
let dbCollectionPath = path.join(dbdir, 'collection.json');
let db;
function createDbFolderIfNotExist() {
if (!fs.existsSync(dbdir)) {
fs.mkdirSync(dbdir)
}
}
function createDbIfNotExist(dbPath) {
if (!fs.existsSync(dbPath)) {
try {
fs.appendFileSync(dbPath, '');
} catch (err) {
console.log(err);
}
}
}
class Database {
constructor() {
createDbFolderIfNotExist();
createDbIfNotExist(dbHistoryPath);
createDbIfNotExist(dbCollectionPath);
this.collections = [];
this.data = [];
this.hist = [];
this.historyCount = 10;
// adapter = new FileSync(dbPath);
db = {
_dbs: {
history: low(new FileSync(dbHistoryPath)),
collections: low(new FileSync(dbCollectionPath))
},
get(which) {
return this._dbs[which].get(which)
},
on(which) {
return this._dbs[which]
}
}
// db = low(adapter);
db._dbs.history.defaults({ history: [] })
.write()
db._dbs.collections.defaults({ collections: {} })
.write()
}
getCollection(collectionName) {
return db.get('collections')
.value()[collectionName]
}
addCollection(collectionName) {
return db.on('collections').set(`collections.${collectionName}`, [])
.write()
}
addRequestToCollection(collectionName, requestObject) {
return db.get('collections')
.get(collectionName)
.push(requestObject)
.write()
}
getRequestFromHistory(id) {
return db.get('history')
.find({ id: id })
.value()
}
getRequestFromCollection(collectionName, id) {
return db.get('collections')
.get(collectionName)
.find({ id: id })
.value()
}
removeRequestFromHistory(id) {
return db.get('history')
.remove({ id: id })
.write()
}
removeRequestFromCollection(collectionName, id) {
return db.get('collections')
.get(collectionName)
.remove({ id: id })
.write()
}
getCollections() {
return db.get('collections')
.value()
}
getHistory() {
return db.get('history')
.reverse()
.value()
}
addToHistory(cmd) {
let count = db.get('history')
.size()
.value()
if (count == this.historyCount) {
// remove the top most element
let firstElement = db.get('history')
.value()[0]
db.get('history')
.remove(firstElement)
.write()
}
db.get('history')
.push(cmd)
.write()
}
clearHistory() {
db.get('history')
.remove()
.write()
}
}
module.exports = {
Database
}