Implementing table joins in the Mobile Apps Node.js backend using SQL

ぃ、小莉子 提交于 2019-12-11 11:12:10

问题


Since Azure Mobile Apps does not provide a way to create relationships between tables I decided to create a custom API in my Node.js backend to return data from related tables. The idea was to implement the joins in the backend using SQL, like explained at the Mobile Services Doc.

The problem is that I'm using the new Mobile Apps and not the old Mobile Services, so the above code does not work anymore. As I understand the architecture changed from Mobile Service to Mobile Apps and the Node.js SDK is an express middleware package. So now we leverage azure-mobile-apps/src/data module to handle sql operations.

So I now I have to do something like this to read from a table in a Custom API inside the Node backend:

var queries = require('azure-mobile-apps/src/query');

module.exports = {

    "get": function (req, res, next) {

        var myTable = req.azureMobile.tables('TableName');
        var query =  queries.create('TableName');
        query.where({'id':req.query.userId});

        myTable.read(query).then(function(data){
            res.send({ some:  data });
        });
    }
};

But since the SQL is not exposed anymore I cannot use the JOIN command to return data from related tables. I would have to to use loops and many request to the database, which defeats the purpose.

-Is there a way to implement the joins in the backend using SQL on the new Mobile Apps Node.js SDK? Or any other better way?

Thank you very much!


回答1:


You can generate the join sql stmt and leverage a sqlQuery object with can contain sql stmt, then use data.execute function to directlu execute the sql stmt. You can refer to the sample https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/custom-api-sql-stmt/api/completeall.js for the similar scenario.

Meanwhile, you can create a View table in Azure SQL database which contain the join stmt, for easy using in Easy APIs scripts and maintaining the View SQL stmt.



来源:https://stackoverflow.com/questions/36961075/implementing-table-joins-in-the-mobile-apps-node-js-backend-using-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!