1

Any idea how can I select the Agent IDs inside the clients filter?

This is what I have now and it works, but I do not like that first we are waiting for the Agents to be selected and then we do another trip to the database to select the clients.

Is there a way that it can be all done in one trip with chaining, without the "then".

const clients = await r.table('agents').getAll(teamId, {index: 'teamId'})('id').then(agentIds =>
  r.table('clients').filter(client => r.or(
    r.expr(agentIds).contains(client('agentId')),
    r.table('tasks').filter(
      task => r.expr(agentIds).contains(task('agentId'))
    )('clientId').contains(client('id'))
  ))
);
Ivo Sabev
  • 5,230
  • 1
  • 26
  • 38

1 Answers1

1

You can write:

r.table('agents').getAll(teamId, {index: 'teamId'})('id').coerceTo('array').do(agentIds =>
  ...
)

To construct a single RethinkDB query that gets the agentIds, stores them in a variable and then does something else.

mlucy
  • 5,249
  • 1
  • 17
  • 21
  • Is there a way to not first select the agents, but do it inside the Clients filter function? r.table('clients').filter(client => { // DO IT HERE }); – Ivo Sabev Aug 26 '16 at 12:40
  • 1
    You could put the query inside the `filter`, but it would probably be slower because it would be issuing an extra read for every document in the `clients` table, while this one issues a single extra ready up front. – mlucy Aug 26 '16 at 19:26
  • I was hoping RethinkDB would optimize those kinds of nested queries – Ivo Sabev Aug 28 '16 at 15:35