如何使用sequelize挂钩仅更新脏记录

时间:2018-06-20 23:52:25

标签: sql node.js postgresql sequelize.js

目前,我正在使用sequelize更新记录,效果很好。我试图实现的是仅更新已修改的记录,并跳过那些未更改的记录。

我正在尝试使用sequelize beforeBulkUpdate挂钩并进行以下更改:

1. add beforeBulkUpdate hook in the model  
2. set individualHooks: true in the controller  

// this is my model
module.exports = (sequelize, DataTypes) => {
    const ContractLineItem = sequelize.define('ContractLineItem', {
        id: {
            type: DataTypes.INTEGER,
            field: 'id',
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        //more fields defination
    }, {
        schema: 'public',
        tableName: 'ContractLineItem',
        timestamps: true
    });


    ContractLineItem.beforeBulkUpdate((contractLineItem, options) => {

        console.log("b4 update contractLineItem....contractLineItem =" + JSON.stringify(contractLineItem));
        console.log("b4 update contractLineItem....contractLineItem._change =" + contractLineItem._change);
        console.log("b4 update contractLineItem....contractLineItem._previousDataValues =" + contractLineItem._previousDataValues);

        //ContractLineItem.findById(contractLineItem.where.id).then((myLineItem) => {
            //console.log("myLineItem.changed() = " + myLineItem.changed());            
            //if (!myLineItem.changed()) {

            if (!contractLineItem._change){
                console.log("nothing changed.............");
                //skip updating record
                //return next();
            }else{
                console.log("something changed................");
                //update record
            }
        //});
    });

    ContractLineItem.associate = (models) => {
        ContractLineItem.belongsTo(models.Contract, {
            foreignKey: 'contractId',
        });
        //more relationships here
    };
    return ContractLineItem;

};

这是我的控制者:

const contractLineItem = require('../models').ContractLineItem;

updateLineItemPromise = contractLineItem
    .update({
        contractId: line.contractId,
        productId: prod,
        purchaseOptionId: line.purchaseOptionId,
        price: line.price,
        unitOfIncrement: line.unitOfIncrement,
        licenseTypeId: line.licenseTypeId,
        purchaseOptionTypeId: line.purchaseOptionTypeId,
        minQuantity: line.minQuantity,
        maxQuantity: line.maxQuantity,
        updatedById: req.body.user.id,
        deleted: line.deleted
    }, {
        where: {
            id: line.id
        },
        individualHooks: true
    }, {
        transaction: t
    });
lineItemPromises.push(updateLineItemPromise);

第一个问题:如何检查哪个记录是脏的?我尝试了两种方法,但没有一种有效。

a. if I check on the model directly 
    b4 update contractLineItem....contractLineItem._change =undefined
    b4 update contractLineItem....contractLineItem._previousDataValues =undefined

b. if I check on the instance using the commented out code above, no matter if the data is dirty or not, myLineItem.changed() is always false
    myLineItem.changed() = false
    nothing changed.............
    myLineItem.changed() = false
    nothing changed.............
    Executing (default): UPDATE "public"."ContractLineItem" SET "productId"='188',"price"=607,"updatedAt"='2018-06-20 14:15:19.694 +00:00' WHERE "id" = 23
    Executing (default): UPDATE "public"."ContractLineItem" SET "productId"='188',"price"=608,"updatedAt"='2018-06-20 14:15:19.697 +00:00' WHERE "id" = 29
    PUT /api/products/188 200 152.839 ms - 363
    Executing (1ddc6794-fc0e-41b9-a724-ab7065cb1285): COMMIT;

第二个问题:如果数据不脏,生成的更新语句仅更新productId,price和updatedAt,为什么?如果我更新其他属性,则更新的确包括以下内容:     正在执行(默认):UPDATE“ public”。“ ContractLineItem” SET“ productId” ='188',“ purchaseOptionId” = 4,“ price” = 607,“ minQuantity” = 36,“ maxQuantity” = 96,“ updatedAt” = '2018-06-20 14:22:56.654 +00:00'WHERE“ id     “ = 23     执行中(默认):UPDATE“ public”。“ ContractLineItem” SET“ productId” ='188',“ price” = 608,“ updatedAt” ='2018-06-20 14:22:56.655 +00:00'WHERE“ id“ = 29

第三个问题:有时在提交后打印出更新的ContractLineItem语句,这是否意味着它不属于事务?

0 个答案:

没有答案