【问题标题】:Sailsjs MVC map params from external API to multiple modelsSailsjs MVC 将参数从外部 API 映射到多个模型
【发布时间】:2017-05-26 20:35:15
【问题描述】:

我需要创建一个 shopify 订单数据库,以便我可以运行在 shopify 管理区域中无法执行的高级查询和销售报告。我在 Sails .12 和 mysql 中构建。 Shopify 允许您注册一个 webhook,以便每次下订单时,它都会创建一个指向指定 URL 的 POST,其中包含 JSON 格式的正文中的订单数据。订购的产品是一个 JSON 对象数组,作为 POST 中的值之一:

{
  "id": 123456,
  "email": "jon@doe.ca",
  "created_at": "2017-01-10T14:26:25-05:00",
...//many more entires
  "line_items": [
    {
        "id": 24361829895,
        "variant_id": 12345,
        "title": "T-Shirt",
        "quantity": 1,
        "price": "140.00",
    },
    {
        "id": 44361829895,
        "variant_id": 42345,
        "title": "Hat",
        "quantity": 1,
        "price": "40.00",
    },
  ]
}

我需要将订单保存到 Orders 表中,并将订购的产品保存到 line_items 表中,该表是一对多关系;一个订单可以有多个 line_items(订购的产品)。 webhook 发送了 100 多个键值对,我将其全部保存。我已经创建了我定义数据类型的两个模型,所以现在我有很长的 Order.js 和 Line_item.js 文件,我正在使用

    line_items: {
    collection: 'line_item',
    via: 'order_id'
},

在我的 Order.js 中,以及

order_id: {
    model: 'order'
},

在我的 Line_item.js 模型中关联它们。这是定义我的两个表的正确方法吗?另外,我应该把将 JSON 映射到模型参数的代码放在哪里?如果我将该代码放入控制器中,我是否必须再键入 100 多行代码才能将每个 json 值映射到其正确的参数。我将如何保存到两个不同的模型/表格?例如:

    var newOrder = {};
    newOrder.id =req.param('id');
    newOrder.email = req.param('email');
    newOrder.name = req.param('name');
    ...//over 100 lines more, then Order.create(newOrder, ...)

    var newLine_items = req.params('line_items'); //an array
    _.forEach(newLine_items, function(line_item){
        var newLine_item = {};
        newLine_item.id = line_item.id;
        newLine_item.order_id = newOrder.id;
        newLine_item.title = line_item.title;
        //etc for over 20 more lines, then Line_item.create(newLine_item, ...)
    });

【问题讨论】:

    标签: mysql node.js model-view-controller sails.js waterline


    【解决方案1】:

    I need to save the order into an Orders table, and the products ordered into a line_items table that is a one to many relation; one order can have many line_items (products ordered).

    这听起来完全合理,嗯,除了使用牛津逗号:)

    There are over 100 key-value pairs sent by the webhook

    我不确定我是否完全理解这是什么或它在此过程中的用途。

    话虽如此,在您的模型中为此设置一个具有 JSON 值的属性可能会有所帮助,然后将其作为 JSON 检索并使用,而不是尝试手动考虑每个属性(如果您是这样的话)在那边做什么?

    这实际上取决于您的用例以及您将如何使用数据,但我认为如果格式发生更改,您可能会遇到问题,如果只是作为 JSON 对象存储和解析,则不是这样?

    Also, where would I put the code that maps the JSON to the model parameters

    在 v0.12.x 中查看Services

    在 v1 中,Services 仍然可以工作,但将此逻辑移至 Helpers 可能是一个不错的选择,但似乎 custom model method 会更好。

    这是您的代码的较短版本:

    var newOrder = req.allParams();
    newLine_items = {};
    _.forEach(newOrder.line_items, function(line_item) {
        newLine_items.push(line_item);
    });
    

    您的逻辑可能如下所示:

    var newOrder = req.allParams();
    
    // Store the order
    Order
    .create(newOrders)
    .exec(function (err, result) {
        if (err) // handle the error
    
        var newLine_items = {};
    
        _.forEach(newOrder.line_items, function(line_item) {
            // Add the order id for association
            line_item.order_id = result.id;
            // Add the new line item with the orders id
            newLine_items.push(line_item);
        });
    
        // Store the orders line items
        LineItems
        .create(newLine_items)
        .exec(function (err, result) {
            if (err) // handle the error
    
            // Handle success
        });
    });
    

    以及 Order 模型中的生命周期回调:

    beforeCreate: function (values, cb) {
        delete(values.line_items);
        cb();
    }
    

    但是您确实应该研究 bluebird promises,因为sails 第一版中的模型方法选择支持它们,它有助于否定在我的示例中开始的厄运金字塔,也是您想要避免的事情:P

    【讨论】:

    • “webhook 发送的键值对”表示 shopify 向我发送了一个超过 100 项的 json 对象。如果我只想将所有内容存储为 json 对象,我可以使用 mongodb,但我需要执行关系查询,例如计算在给定日期范围内每种产品的销售量。
    • @hamncheez 那么您所做的听起来很理智,如果您在处理数据时遇到太多代码问题,那么请查看服务,构建一个具有所有所需逻辑方法的服务在那部分内。在我看来,您可能只需要这样做一次,然后就可以使用该功能了?
    • @hamncheez 在我的答案末尾检查我的编辑,这应该使它更易于管理?
    • 服务似乎是一个很好的解决方案,可以让我的控制器保持简短,但我仍然不确定如何正确保存到这两个表中。
    • @很高兴有帮助。如果您想让 beforeCreate 工作,可以查看 ORM 中生命周期回调的sails 文档。根据文档,不确定我可能做错了什么。但是,嘿,它在那里没有问题,祝你的报告应用程序好运,听起来很酷;)
    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2016-04-29
    相关资源
    最近更新 更多