简体   繁体   中英

BulkUpdate for Sequelize.org on MSSQL

I have a table with three fields.

--------------------------------
  id_ram   |    value  |  um 

id_ram is primary key.

I'm designing an Express API with sequelize . I am not able to update multiple rows of this table.

I am passing json array like below.

[
  {"id_ram":"54","value":"11","um":"GB"},
  {"id_ram":"34","value":"22","um":"GB"},
  {"id_ram":"70","value":"33","um":"GB"}
]

This is what I have tried so far.

router.post('/update',function (req, res) {

    var api_name = middleware_name + " /update"; 

    // - Check if the input array is passed 
    if(req.body.array == undefined) {

        var jsonErrorResponse = api_manager.PrepareJSONResponse(api_name, "", "value of input parameter [array] is undefined");
        api_manager.WriteErrorLogFile(req,api_name,jsonErrorResponse,jsonErrorResponse);
        res.send(jsonErrorResponse);

        return;
    }
    else {
        var create_values_array = "";
        try {
            //Parse INPUT JSON Array
            create_values_array = JSON.parse(req.body.array);
        }
        catch (err) {

            //Raise SyntaxError
            var jsonErrorResponse = api_manager.PrepareJSONResponse(api_name,"",err.message);
            var jsonInternalError = api_manager.PrepareJSONResponse(api_name,"",err);
            api_manager.WriteErrorLogFile(req,api_name,jsonErrorResponse,jsonInternalError);

            //Send error Response
            res.send(jsonErrorResponse);
        }

        ObjectModel.bulkCreate(
            create_values_array
        , {updateOnDuplicate: ["id_ram"]})
        .then(created_objects => { // Notice: There are no arguments here, as of right now you'll have to...

            //Send Response and Log Action
            var jsonData = api_manager.PrepareJSONResponse(api_name,created_objects,"");
            api_manager.WriteInfoLogFile(req,api_name,jsonData);

            res.send(jsonData);

        }).catch (function (err) {

            //Write Error Log
            var jsonErrorResponse = api_manager.PrepareJSONResponse(api_name,"",err.message);
            var jsonInternalError = api_manager.PrepareJSONResponse(api_name,"",err);
            api_manager.WriteErrorLogFile(req,api_name,jsonErrorResponse,jsonInternalError);

            //Send error Response
            res.send(jsonErrorResponse);
        });
    }
});

How can we implement bulkUpdate like bulkCreate in sequelize orm for MSSQL ?

For bulk create you do it this way.

//array of object to be inserted
const data = [
   {field1: "value1"}, {field2: "value2"}...
]

Model.bulkCreate(data, {returning: true}) //if you don't pass returning true it will not return the data

bulkCreate() can also be used for updating as well.

bulkCreate(data , {updateOnDuplicate : true })

Hello {updateOnDuplicate : true } raise an error because SQL Server doesn't support this feature.

Is It possible to take another way ?

You could achieve this by writing a function that calls the Sequelize upsert function in a loop like so:

const records = [
  { field1: 'value1', field2: 'value2' },
  ...
];

async function bulkUpsert(records) {
  return Promise.all(
    records.map((record) {
      return Model.upsert(record);
    })
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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