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

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

  •  0
  • soccerway  · 技术社区  · 3 年前

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

    1.电子邮件字段是唯一的

    2.现有记录如有变更,需更新

    3.需要插入新记录

    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;
    };
    

    enter image description here

    0 回复  |  直到 3 年前
        1
  •  0
  •   soccerway    3 年前

    在where子句中添加PK之后 where: { id: ['id']} ,它开始根据列更新记录 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 !"});
        }