-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.js
55 lines (45 loc) · 1.46 KB
/
redis.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
var fs = require('fs');
var util = require('util');
var icebox = require('./icebox.js');
var EventEmitter = require('events').EventEmitter;
const redis = require('redis');
const client = redis.createClient(process.env.REDIS_URL);
const { promisify } = require("util");
const getAsync = promisify(client.get).bind(client);
const keysAsync = promisify(client.keys).bind(client);
// //////////////////////////////////////////////////////////////////////
function DB() {
this.prototypeMap = {};
EventEmitter.call(this);
console.log('opening database');
var db = this;
db.on('all', function () { db.emit('load', 0); });
}
util.inherits(DB, EventEmitter);
DB.prototype.registerObject = function(constructor) {
this.prototypeMap[constructor.name] = constructor;
}
DB.prototype.get = async function(key) {
const json = await getAsync(key);
const data = JSON.parse(json);
const game = icebox.thaw(data, this.prototypeMap);
return game;
}
DB.prototype.set = function(key, object) {
data = icebox.freeze(object);
client.set(key, JSON.stringify(data));
}
DB.prototype.all = async function() {
const keys = await keysAsync('*');
var retval = [];
for (let i = 0; i < keys.length; i++) {
const jsn = await getAsync(keys[i]);
const value = JSON.parse(jsn);
retval.push(value);
}
return retval;
}
DB.prototype.snapshot = function() {
console.log("Called redis stub snapshot function")
}
exports.DB = DB;