是否可以使用 sequelize 更新多个数据?

Is this possible to update multiple data with sequlize?

我已经创建了这样的添加产品 API。这工作正常。我通过共享产品 ID 作为外键成功发布了各种数据,但我对如何更新产品数据感到困惑。我可以使用此代码更新数据吗?

try {
            
const { name, description, photo, tag, productId ,lableType,reccomendedProduct, varientDetails, isTex,GSTrate,GSTtyp, HSNcode, categoryId, subCategoryId,  videoUpload,} = req.body;
const data=  db.product.findOne({ where: { id: productId },
     include: [{ model: db.tagModel, attributes: ["id","name","productId"]}, { model: db.reccomendProduct, attributes: ["id", "productName","productId"]},
                { model: db.varientModel, attributes: ["id", "sort","sku","productId","waightunitno","unit","mrp","discount","price","stock","minstock","outofstock"]}]
}).then(product => {
    if (product) {
        db.product.update({
            categoryId: categoryId ? categoryId : product.categoryId,
            subCategoryId: subCategoryId ? subCategoryId : product.subCategoryId,
            name:name,
            description:description,
            lableType:lableType,
            isTex:isTex,
            // photo:req.file ? req.file.location:'',
            photo:photo,
            GSTrate:GSTrate,
            GSTtyp:GSTtyp,
            HSNcode:HSNcode,
            categoryId:categoryId,
            subCategoryId:subCategoryId,
            videoUpload:videoUpload }, { where: { id: product.id } 
        })
    }
    if(varientDetails ) {
        db.varientModel.findAll ({ where: { productId:productId }})
        .then(varient => {
            console.log(varient+data)
            for (let i=0; i < varientDetails.length; i++) {
                db.varientModel.update({
                    productId:productId,
                    sort:varientDetails[i].sort,
                    sku: varientDetails[i].sku,
                    waightunitno:varientDetails[i].waightunitno,
                    unit:varientDetails[i].unit,
                    mrp:varientDetails[i].mrp,
                    discount: varientDetails[i].discount,
                    price: varientDetails[i].price,
                    stock: varientDetails[i].stack,
                    minstock: varientDetails[i].minstock,
                    outofstock: varientDetails[i].outofstock
                    }, { where: { productId:productId[i] }
                })
            }     
        })
    }  

是的,有很多方法可以做到。 我不觉得它们像 multiple one 那样富有表现力和清晰度。

1。自行创建查询

你可以像这样创建函数

function updateUsers(updateFirstValue, updateSecondValue, productIds) {
  let query = "";
  for (let index = 0; index < productIds.length; index++) {
    query += `update tableName set firstUpdateValue="${updateFirstValue[index]}",secondUpdateValue="${updateSecondValue[index]}" where productId="${productIds[index]}";`;
  }
  return query;
}
//This is vulnerable to SQL Injection so change according to your needs
//It's just idea of doing it
let firstUpdates = [800, 900, 10];
let secondUpdates = [1.23, 2.23, 8.97];
let productIds = [1, 9, 3];
let generatedQuery = updateUsers(firstUpdates, secondUpdates, productIds);
console.log(generatedQuery);
// to run this with sequelize we can execute plain query with this 
//sequelize.query(generatedQuery);

2。使用 bulkCreateupdateOnDuplicate

let updatingValue = [
    {productId:1, sort:100,sku:800},
    {productId:2, sort:800,sku:8.27},
    {productId:3, sort:400,sku:7.77}
    ];



model.bulkCreate(updatingValue,{
    fields:["productid","sort","sku"],
    updateOnDuplicate: ["sort","sku"]
}
    // these are the column name that gets updated if primaryKey(productId) gets matched you have to update these accordingly
    )

之前有问题,现在更新了this PR

其他比较复杂的方法也是