如何使用 sequelize.js 批量更新记录并忽略某些列

How can I bulk update records using sequelize.js and ignore certain columns

如何使用 sequelize.js 批量更新记录。一直以来,我将只有 150 个用户需要更新。在这种情况下,如何在 MySql 中找到要更新的唯一记录?目前没有更新记录

1.email 字段是唯一的

2.existing 记录如有更改需要更新

3.new条记录需要插入

server.js

const manageNomineesSchema = require('./server/models/managenominees');
const ManageNomineesModel  = manageNomineesSchema(sequelize, DataTypes);
app.post('/service/managenominees', upload.single('file'), async (req, res, next) => {
    try {
        if(req.file){
          let filePath = req.file.path;
          fs.createReadStream(filePath)
              .pipe(csv())
              .on('data', (data) => results.push(data))
              .on('end', async () => {
                console.log(results);
                const allNominees = results.map(
                    nominees => {
                      return {
                        name: nominees.name,
                        email: nominees.email
                      }
                    });
        const emailCount = await ManageNomineesModel.count({ col: 'email' });
        if(emailCount == 0){
          await ManageNomineesModel.bulkCreate(allNominees);
        } else {
          await ManageNomineesModel.bulkCreate(allNominees,
              { updateOnDuplicate: ["email"] },
              {attributes: { exclude: ['createdAt'] }
              })
        }
                res.status(200).json({ message: "Nominees inserted successfully !"});
              });
          }
    
      } catch (e) {
        res.status(500).json({ fail: e.message });
      }

});

managenominees.js

module.exports = (sequelize, DataTypes) => {
    const managenominees = sequelize.define('managenominees', {
        id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(200),
            allowNull: false
        },
        email: {
            type: DataTypes.STRING(100),
            allowNull: false
        },
        access: {
            type: DataTypes.STRING(10),
            allowNull: true
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        }
    }, {
        timestamps: true,
        tableName: 'managenominees'
    });

    return managenominees;
};

在 where 子句 where: { id: ['id']} 中添加 PK 后,它开始针对列 updateOnDuplicate: ["name"]

更新记录
const emailCount = await ManageNomineesModel.count({ col: 'email' });
    if(emailCount == 0){
      await ManageNomineesModel.bulkCreate(allNominees);
      res.status(200).json({ message: "Nominees inserted successfully !"});
    } else {
      await ManageNomineesModel.bulkCreate(allNominees,
          { updateOnDuplicate: ["name"],
          where: { id: ['id']}
          });
      res.status(200).json({ message: "Nominee records updated successfully !"});
    }