使用Azure Mobile Apps for Node在Cosmos数据库上执行SQL查询连接

目前,我正在为使用Azure移动应用程序和Node.js和Azure Cosmos DB(文档数据库)开发移动系统的后端。

我使用的是这里给出的例子:

https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/custom-api-sql-stmt/api/completeall.js

到目前为止这么好 – 但是,每当我运行这个SQL,我得到:

TypeError: provider.read is not a function at Function.module.exports.api.execute (x:\x\node_modules\azure-mobile-apps\src\data\index.js:32:18

我需要运行的SQL如下所示:

SELECT c.id, c.Email FROM Profile c JOIN d in c.DeviceIDs WHERE d.DeviceID = @deviceId

这是因为我的数据是嵌套的,就像这样(Cosmos中的Profile集合) – 我需要获取DeviceID匹配的logging:

[ { "id": "2ca572d0-858d-4376-8537-c228a8379638", "Email": "test@user.com", "Name": "Test User 1", "OrgRoles": null, "DeviceIDs": [ { "DeviceID": "123445555555", "DeviceCode": "1234" } ], "UpdatedDate": "2017-11-10T13:18:32.0110724Z", "CreatedDate": "2017-11-10T13:18:27.220764Z", "IsDeleted": true }]

我希望能够以另一种方式做到这一点,使用这样的事情:

context.tables('Profile').where({ DeviceID: id })

但我无法弄清楚如何用这种方法来完成连接,更不用说连接在同一张桌子上了。

谁能帮助任何一种方法? 我感觉到MS的Azure移动应用程序文档数据库驱动程序没有正确完成,但我似乎无法得到任何人的看法,如果有任何方法我可以帮助。

更新:我们已经设法通过文档数据库驱动程序直接做到这一点,但这意味着我们将需要有一些我们的API方法连接到数据库的方式和其他方式连接到另一种方式。 似乎一个耻辱,微软应该充分投资宇宙 – 这是相当不错的恕我直言。

由于Azure Mobile Apps Node.js SDK不支持Cosmos DB驱动程序,因此您需要将SDK azure-documentdb-node集成到您的移动应用程序中。

 var documentClient = require("documentdb").DocumentClient; var endpoint = 'https://<account_name>.documents.azure.com:443/'; var primaryKey = '<primary_key>'; var client = new documentClient(endpoint, { "masterKey": primaryKey }); var databaseUrl = `dbs/<database_name>`; var collectionUrl = `${databaseUrl}/colls/Profile`; var querySpec = { 'query': 'SELECT c.id, c.Email FROM Profile c JOIN d in c.DeviceIDs WHERE d.DeviceID = @deviceId', "parameters": [ { "name": "@deviceId", "value": '123445555555' } ] } client.queryDocuments(collectionUrl, querySpec).toArray(function(err, results) { if(err) return console.log(err); console.log(results); });