Warren Mira
Warren Mira

Reputation: 242

RethinkDB, add derived columns on result

I can seem to do this via map+merge

map( function(row) {
   return row.merge( { newCol: 'ABC' } );
});

Problem is if I want to do a lookup from a static map e.g.

var LOOKUP_MAP = {key1: {text: 'Key 1'}};

Then below doesn't work

map( function(row) {
   return row.merge({ newCol: LOOKUP_MAP[row('key')].text });
});

row('key'); seems to be lazily evaluated. Any idea how to do this?

Upvotes: 0

Views: 56

Answers (1)

kureikain
kureikain

Reputation: 2314

You can use sth like this:

var LOOKUP_MAP = {key1: {text: 'Key 1'}};

r.db('salaries').table('salaries')
.map( function(row) {
   return row.merge({ newCol: r.expr(LOOKUP_MAP)(row('key'))('text') });
});

Upvotes: 1

Related Questions