Skip to content

Instantly share code, notes, and snippets.

@sunnygleason
Created April 21, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnygleason/5fabc8fd0d7042f04b4f0a9e4549bd5b to your computer and use it in GitHub Desktop.
Save sunnygleason/5fabc8fd0d7042f04b4f0a9e4549bd5b to your computer and use it in GitHub Desktop.
Advanced BLOCKS Techniques - KV Store BLOCK
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