Azure 移动应用 CRUD 操作语法

Azure Mobile Apps CRUD Operations syntax

这是我第一次使用 SO。我来这里是因为我最近迁移到了新的 Microsoft Azure App Service,并且 node.js 语法等似乎已更改或略有不同。这是我当前使用旧移动服务的代码,我希望迁移到新语法。关于有什么变化等方面的参考资料非常有限,我在 google 上也找不到太多。

function insert(item, user, request) {
var table = tables.getTable('user');
table.where({ 
   userid: item.userid
}).read({
   success: upsertItem
});


function upsertItem(existingItems) {
    if (existingItems.length > 0) {
        item.id = existingItems[0].id;
        table.update(item, {
        success: function(updatedItem) {
        request.respond(200, updatedItem)
        }
      });
    } else {
        request.execute();
    }
 }

}

这是新脚本的示例

var table = module.exports = require('azure-mobile-apps').table();

table.read(function (context) {
 return context.execute();
});

我也看过这个 post 但它没有帮助

Azure mobile apps CRUD operations on SQL table (node.js backend)

我的objective是按照我原来的代码执行UPSERT。如果您可以通过提供确切的转换来帮助我,那将是我的首选答案。

在此先感谢您的帮助。

我在此处完成了有关新 Node.js SDK 的完整系列文章:https://shellmonger.com/30-days-of-azure-mobile-apps-the-table-of-contents/

还有官方文档,比较全面:https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/

还有一个兼容包,旨在让您更轻松地将旧服务转换为新格式:https://www.npmjs.com/package/azure-mobile-apps-compatibility

在这些之间,您应该能够轻松地将您的站点转换为新的 Azure 移动应用程序 SDK。

一般来说,您可以利用 use() 自定义您自己的中间件来处理额外的操作。

As Insert 操作通过 HTTP POST 请求实现。您可以参考 了解如何使用 POSTMAN 中的数据针对 Azure 移动应用程序实施 post 请求。

然后,您可以在您的 Azure 移动应用程序中安装 body-parser 模块来解析请求正文。

app.js中添加以下代码以启用模块:

var bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

最后,请参考 Easy Tables 脚本中的以下代码片段,了解如何使用中间件:

var insertMiddleware = function(req,res,next){
    var queries = require('azure-mobile-apps/src/query');
    var t = req.azureMobile.tables('TodoItem');
    var query = queries.create('TodoItem')
            .where({ text: req.body.text });
    t.read(query).then(function(data){
      if(data.length === 0){
        next();
      }else{
        data.text = "something new";
        console.log(data);
        var query = {
            sql: 'UPDATE TodoItem SET text = @text where id = @id',
            parameters: [
                { name: 'text', value: data.text },
                { name: 'id', value: data.id }
            ]
        };
        req.azureMobile.data.execute(query).then(function(result){
          res.status(200).json("success");
        });
      }
    });
};
table.insert.use(insertMiddleware, table.operation);
table.insert(function (context) {
  return context.execute();
});