-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
64 lines (51 loc) · 1.61 KB
/
db.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
const { promisify } = require('util');
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
const getAsync = promisify(client.get).bind(client);
const setAsync = promisify(client.set).bind(client);
const hsetAsync = promisify(client.hset).bind(client);
const hgetAsync = promisify(client.hget).bind(client);
const hgetallAsync = promisify(client.hgetall).bind(client);
class DB {
constructor() {
}
async has(id, lastOne) {
console.log(id)
const result = await getAsync(id + lastOne.href);
return result !== null;
}
async add(id, result) {
let status = await this.has(id, result);
if (status) {
return false
}
status = await setAsync(id + result.href, result.text);
return status;
}
async hasService(name) {
return null !== await this.getSubscribers(name)
}
async setService(name) {
return await hsetAsync('services', name, JSON.stringify([]))
}
async getSubscribers(name) {
let services = Object.keys(await hgetallAsync('services'));
if (services.indexOf(name) !== -1) {
let subscribers = await hgetAsync('services', name)
return JSON.parse(subscribers)
}
return null
}
async setSubscriber(name, value) {
let currentSubsciber = JSON.parse(await this.getService(name));
currentSubsciber.push(value)
return await hsetAsync('services', name, JSON.stringify(currentSubsciber))
}
}
const db = new DB();
module.exports = {
db
}