-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatastore.js
42 lines (36 loc) · 1.07 KB
/
datastore.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
const Datastore = require("nedb");
const db = new Datastore({ filename: '.data/new_datafile', autoload: true });
function getDb() {
return db;
}
function promisify(invokee) {
return new Promise((resolve, reject) => {
invokee((err, result) => {
console.log("invokee cb", err, result);
if (err) { return reject(err); }
resolve(result);
});
});
}
function storeState(channelId, state) {
const _id = `state:${channelId}`;
const db = getDb();
const stateToUpsert = Object.assign({}, state, {_id});
return promisify((cb) => db.update({_id}, stateToUpsert, {upsert: true}, cb));
}
function getState(channelId) {
console.log("Does this work? Getting state");
const _id = `state:${channelId}`;
const db = getDb();
return promisify((cb) => db.findOne({_id}, cb));
}
function storeMessage(message, details) {
const _id = `message:${message.message_ts}`;
const db = getDb();
console.log('storeMessage', message, details);
// return promisify((cb) => db.update({_id}, details, {upsert: true}, cb));
}
module.exports = {
getState,
storeState
};