0

I am trying to periodically calculate complex top score for all items in post table.

const {log10, max, abs, round} = Math;

const topScore = post => { // from Reddit
  const {score, createdAt} = post;
  const order = log10(max(abs(score), 1));
  const sign = score > 0 ? 1 : (score < 0 ? -1 : 0);
  const seconds = Date.now() - createdAt;
  return sign * order + seconds / 45000;
};

With the above function, I want to perform something like this:

// Update topScore every 60 seconds.
setInterval(() =>
  r.table('post').update(post => post.topScore = topScore(post)).run();
, 60000);

How do I do this with RethinkDB javascript driver?

Joon
  • 9,346
  • 8
  • 48
  • 75

1 Answers1

0

You can write r.table('post').update(r.js('(function(post) { ... })'), {nonAtomic: true}) where ... is arbitrary JS code. Otherwise you'd either have to translate that code into ReQL or pull down the documents to your client, update them, and then write them back to the server.

mlucy
  • 5,249
  • 1
  • 17
  • 21