'How to increment a particular column values by 1 using sequelize command in Node.js?
What I basically want to do is that, every time I enter a data, all other data's priority in my table should be incremented by 1 and then the newly entered data's priority should be set to 1. In the below code I'm first counting the number of data(rows) , that would be my dataCount and I'm trying to increment the value of a column(priority) by one using the function 'upsert'. I do not know what this upsert function is, I just tried it, anyways my code is not working. let dataCount = await this.testimonialRepository.count({});
let data;
if(dataCount>0){
await this.testimonialRepository.upsert({priority:+1},{})
data = await this.testimonialRepository.add({
...this.requestBody,
status: Number(statusEnum.ACTIVE),
createdBy: admin.id,
priority: 1
});
}
else{
data = await this.testimonialRepository.add({
...this.requestBody,
status: Number(statusEnum.ACTIVE),
createdBy: admin.id,
priority: 1
});
}
Solution 1:[1]
If you want to update existing records then it's enough to use update instead of upsert. However if you want to increase a value of a certain column use increment method:
await this.testimonialRepository.increment('priority', { by: 1})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anatoly |
