rethinkdbrethinkdb-javascriptreql

Use result from another query in filter in RethinkDB/ReQL


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'))
  ))
);

Solution

  • 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.