代码之家  ›  专栏  ›  技术社区  ›  hamncheez

Sailsjs MVC将参数从外部API映射到多个模型

  •  1
  • hamncheez  · 技术社区  · 8 年前

    我需要创建一个shopify订单数据库,这样我就可以运行高级查询和销售报告,而这在ShopifyAdmin区域是做不到的。我正在构建Sails.12和mysql。Shopify允许您注册一个webhook,这样每次下订单时,它都会创建一个到指定URL的帖子,其中订单数据在正文中以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多个键值对,我正在保存所有这些键值对。我已经创建了两个定义数据类型的模型,所以现在我有了很长的顺序。js和Line_item。js文件,我正在使用

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

    按我的顺序。js,以及

    order_id: {
        model: 'order'
    },
    

    在my 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, ...)
        });
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Craig van Tonder eggyal    8 年前

    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中,服务仍将工作,但将此逻辑移入 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
        });
    });
    

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

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

    但你真的应该看看蓝鸟的承诺,因为第一版sails中的模型方法有选择加入支持,这有助于否定我的例子中开始的末日金字塔,这也是你想要避免的:P