So I want to update my table column based on the condition here is the code.
await CourseSubscriber.update({
completion_percentage: completion_percentage,
completed_on: **, // over here i want to check if completion_percentage<100 then update this one
}, {
where: {
course_id: course_id,
academy_id,
user_id: user_id
}
})
CodePudding user response:
One way to do this is to add 2 queries for update based on completion_percentage
if(completion_percentage < 100){
await CourseSubscriber.update({
completion_percentage: completion_percentage,
completed_on: null,
}, {
where: {
course_id: course_id,
academy_id,
user_id: user_id
}
})
} else {
await CourseSubscriber.update({
completion_percentage: completion_percentage
}, {
where: {
course_id: course_id,
academy_id,
user_id: user_id
}
})
}
CodePudding user response:
Here is another solution
const payload={
completion_percentage: completion_percentage,
}
if(completion_percentage < 100){
payload.completed_on = null
}
await CourseSubscriber.update(payload, {
where: {
course_id: course_id,
academy_id,
user_id: user_id
}
})
by this we only do one query to database.