0

In RethinkDB I am trying to chain multiple queries to multiple database tables. The idea is the same as stored procs for traditional dB's. Basically I query all the users connected to a device and then for each users try to get the rules attached from a rules table. Here is a gist of the ReQL query I am writing. But forEach does not work as it wants a write query and not read and also do() is failing. Any suggestions?

const get_distance = function(location){

const final_location = 500;

return r.expr(500).sub(location);

};

​

const run_rule = function(device,distance){

return r.db('locationtracker_development').table('customer_details').filter(function(cust){

return cust("deviceId").contains(device);

}).pluck("userId").forEach(function(userName){

//TODO Work on each user

return r.db('locationtracker_development').table('user_rules').filter({'userId':userName('userId')});

});

};

r.do(get_distance(100)).do(function(dist){

return run_rule('gXtzAawbc6',dist);

});
Sourav Chatterjee
  • 780
  • 2
  • 13
  • 26

1 Answers1

1

I have gotten it resolved with help from the Slack Channel of RethinkDB.

Here is the code :

const get_distance = function(location){
  const final_location = 500;
  return r.expr(500).sub(location);
};

const run_rule = function(device,distance){
  return r.db('locationtracker_development').table('customer_details').filter(function(cust){
    return cust("deviceId").contains(device);
  }).coerceTo('array').map(function(doc){
    return doc('userId');

  }).do(function(userIds){
    //return userIds;
    return r.db('locationtracker_development').table('user_rules').
      getAll(r.args(userIds),{index:'userId'});
  });
};
   r.do(get_distance(100)).do(function(dist){
      return run_rule('gXtzAawbc6',dist);
   });

Basically the objects returned needs to be coercedTo and Array and then using map and do functionality we can achieve querying multiple DBs.

Sourav Chatterjee
  • 780
  • 2
  • 13
  • 26