I have created add product API like this. This is working fine. I’m posting successfully varient data by sharing product id as a foreign key, but I’m confused about how can I update product data. Can I update data by using this code?
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] } }) } }) }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Yes, there are ways to do it.
I don’t find them as expressive and as clear as multiple one.
1. Creating Query on own
You can create function like this
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. Using bulkCreate
and updateOnDuplicate
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
)
It had problem before but is updated now this PR
Other methods but quite complicated are also here.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0