Skip to content

Instantly share code, notes, and snippets.

@neumino
Last active August 29, 2015 14:06
Show Gist options
  • Save neumino/8105b19497b904c4814d to your computer and use it in GitHub Desktop.
Save neumino/8105b19497b904c4814d to your computer and use it in GitHub Desktop.
var r = require('rethinkdbdash')();
setInterval(function() {
r.table("logs").insert({
date: r.now()
}).run({noreply: true})
, 1000)
r.table("logs").changes().run().then(function(feed) {
feed.each(function(change) {
if (change.old_val === null) { // this change is an insert
r.table("aggregated").get(r.expr(change.new_val.date).date()).replace(function(doc) {
return doc.default({id: r.expr(change.new_val.date).date(), count: 0}).merge(function(valid_doc) { // if the doc doesn't exist, we replace it using `default`
return {count: valid_doc("count").add(1) } // increment `count`
})
}).run().error(console.log)
}
else if (change.new_val == null) {
// sub(1)
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment