Last active
August 29, 2015 14:06
-
-
Save neumino/8105b19497b904c4814d to your computer and use it in GitHub Desktop.
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
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