Trying to join together two apis that use node mysql

20 views
Skip to first unread message

Robbie Sherman

unread,
Jun 30, 2015, 10:23:51 AM6/30/15
to node-...@googlegroups.com
I have two apis,

api one will fetch from mysql table, all top level data, ie id, name, location, size,....

api two fetches how much electricty has been used at a property. Now api 2 uses multiple querys. Each site has its own table that stores electrity used. ie electricy_1, electricity_2.

What Id like to do is take the result from the query used in api one, then add another column on that uses the single result from api two, that links up with the id in api, so my utlimate goal would be json that looks like
{{id:1,
name:"Test 1",
location:"London",
size:7,
electricity: 7000
},{id:2,
name:"Test 2",
location:"Birmingham",
size:40,
electricity: 80000
},{id:3,
name:"Test 3",
location:"Runcorn",
size:4,
electricity: 1200
}
}
where id, name, location, size have all come from one main table, and electricty has come from a table where the tables name is matching the id.

What I have so far is 

// api for getting top level summary data of all sites
  app.get('/api/pickUp', function(req,res){
    connection.query('SELECT id, name, tic_mwp from top_table', function(err, rows) {
      if (err){
        return res.json(err);
      } else {
        return res.json(rows);
      }
    });
  });

  // api for getting generation of all sites
  app.get('/api/testLoop', function(req,res){
    var baseQuery = [];
    var num = 1;
    var fullQuery = "";
    for (i=1;i<6;i++){
      baseQuery[i]=['SELECT sum(generation) from export_'+num+';'];
      num++;
    }
    for(y=1;y<baseQuery.length;y++){
      fullQuery = fullQuery+baseQuery[y];
    }
    connection.query(fullQuery, function(err, rows) {
      if (err){
        console.log(err);
      } else {
        return res.json(rows);
      }
    });
  });

Essentially I want to join the results of these two together.
ANy suggestion, ideas, tips?

Reply all
Reply to author
Forward
0 new messages