-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-manager.js
86 lines (60 loc) · 1.93 KB
/
db-manager.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
const redis = require("redis");
const client = redis.createClient()
client.on("connect", () => {
console.log("Connected to database")
})
module.exports.connectDatabase = async function () {
await client.connect();
}
module.exports.readDatabase = async function () {
return new Promise(async (resolve, reject) => {
var keys = await client.keys("*");
var data = []
for (let i = 0; i < keys.length; i++) {
const value = JSON.parse(await client.get(keys[i]));
if (keys[i] != "currentSKU") {
data.push(JSON.stringify(value));
}
}
resolve(data)
})
}
module.exports.readDatabaseSKU = async function () {
return new Promise(async (resolve, reject) => {
// get value of key currentSKU
var currentSKU = await client.get("currentSKU");
// if currentSKU is null, set it to 0
if (currentSKU == null) {
await client.set("currentSKU", 0);
currentSKU = 0;
}
// return new value
resolve(currentSKU);
})
}
module.exports.setDatabaseSKU = async function (sku) {
return new Promise(async (resolve, reject) => {
// set value of key currentSKU
await client.set("currentSKU", sku);
resolve();
})
}
module.exports.writeDatabase = async function (value) {
return new Promise(async (resolve, reject) => {
value = JSON.parse(value)
await client.set(value.transactionID, JSON.stringify(value));
var keys = await client.keys("*");
var data = []
for (let i = 0; i < keys.length; i++) {
const item = JSON.parse(await client.get(keys[i]));
data.push(JSON.stringify(item));
}
resolve(data);
})
}
module.exports.deleteDatabase = async function (key) {
return new Promise(async (resolve, reject) => {
await client.del(key);
resolve();
})
}