简体   繁体   English

Sails.js Waterline UPDATE:如何处理多个更新

[英]Sails.js Waterline UPDATE: How to handle multiple updates

I have an array of objects that I want to update, thus a multiple update. 我有一个我想要更新的对象数组,因此需要多次更新。 I am looking for the best way to implement this. 我正在寻找实现这一目标的最佳方法。 Each object includes the same properties, but the properties for each object have different values. 每个对象包含相同的属性,但每个对象的属性具有不同的值。

Do I implement something like this? 我实现这样的东西吗?

var people = [
    {'firstName': 'Brian', 'clockedIn': '2016-4-12', 'type': 'developer'},
    {'firstName': 'Taylor', 'clockedIn': '2016-4-14', 'type': 'developer'},
    {'firstName': 'Jake', 'clockedIn': '2016-4-14', 'type': 'manager'}
];

PersonModel.update({'type': 'developer'}, people, function(err, records) {
    // ...
    // ...
});

If I do something like the previous code, what exactly does it do? 如果我像以前的代码那样做什么,它到底做了什么? Does it automatically try to match the primary key of each record in the people array, update any of the properties that are listed, and then pass the records that were updated into the callback function? 它是否会自动尝试匹配people数组中每条记录的主键,更新列出的任何属性,然后将更新的记录传递给回调函数?

I noticed in the Sails.js documentation here that the second argument for the update function can be an object OR an array, I am just unclear if I can use it like this. 我的Sails.js文档中注意到这里 ,对于更新功能的第二个参数可以是对象或数组,我只是不清楚,如果我可以这样使用它。 The documentation is unclear. 文件不清楚。

If I cannot use it like this and I wanted to try an iterative or recursive approach, how would you suggest I implement it? 如果我不能像这样使用它并且我想尝试迭代或递归方法,你会建议我实现它吗?

What is your adapter ? 你的适配器是什么?

I had to do this with MySQL, I make the query my self , and then call PersonModel.query(myQuery) 我曾与MySQL来做到这一点,我做了查询我的自我 ,然后调用PersonModel.query(myQuery)

I follow this answer to make it (example with two field) : 我按照这个答案来制作它(两个字段的例子):

values = [
     {
         id: 1,
         param_1: 'NewValueParam1',
         param_2: 'NewValueParam2'
     },
     {
         id: 2,
         param_1: 'AnotherNewValueParam1',
         param_2: 'AnotherNewValueParam2'
     }
];

function multiValuesUpdate(values) {
    var request = "UPDATE MyTable SET ";
    var part_param_1 = "`param_1` = (CASE `id` ";
    var part_param_2 = "`param_2` = (CASE `id` ";
    var part_ids = "WHERE `id` IN (";

    _.forEach(values, function (v) {
        part_param_1 += "WHEN '" + v.id + "' THEN '" + v.param_1 + "' ";
        part_param_2 += "WHEN '" + v.id + "' THEN '" + v.param_2 + "' ";
        part_ids += v.id + ",";
    });
    part_param_1 += "ELSE `param_1` END), "; // Be careful with the comma !
    part_param_2 += "ELSE `param_2` END) ";
    part_ids = part_ids.slice(0, -1) + ");"; // Remove the last "," and add ");"

    request += part_param_1 + part_param_2 + part_ids;
    return request;
}

console.log(multiValuesUpdate(values));

Output : 输出:

"UPDATE MyTable 
   SET `param_1` = (
   CASE `id` 
      WHEN '1' THEN 'NewValueParam1' 
      WHEN '2' THEN 'AnotherNewValueParam1' 
      ELSE `param_1` 
   END), 
   `param_2` = (
   CASE `id` 
      WHEN '1' THEN 'NewValueParam2' 
      WHEN '2' THEN 'AnotherNewValueParam2' 
      ELSE `param_2` 
   END) 
 WHERE `id` IN (1,2);"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM