Created
April 21, 2017 16:37
-
-
Save sunnygleason/5fabc8fd0d7042f04b4f0a9e4549bd5b to your computer and use it in GitHub Desktop.
Advanced BLOCKS Techniques - KV Store BLOCK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (request) => { | |
const db = require('kvstore'); | |
var by = request.message.by || request.params.uuid; | |
var last_key = by + ":last_msg"; | |
var count_key = by + ":count"; | |
if (request.message && (request.message.text == '/my_stats')) { | |
// | |
// special "my_stats" command handling | |
// | |
return db.get(last_key).then((last_msg) => { | |
return db.getCounter(count_key).then((count) => { | |
request.message.last_msg = last_msg || "<not found>"; | |
request.message.count = count || 0; | |
return request.ok(); | |
}); | |
}); | |
} | |
// | |
// normal case, store the message details | |
// | |
request.message.at = new Date().toISOString(); | |
return db.set(last_key, request.message).then((last_result) => { | |
return db.incrCounter(count_key).then((count_result) => { | |
return request.ok(); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment