Skip to content

How to perform conditional update operation? (Alternately, updating row with single function when received data have missing fields) #263

Answered by mukundshah
mukundshah asked this question in Q&A
Discussion options

You must be logged in to vote

Okay, I found the solution.

async function updateEvent(id: number, res: IEventUpdate) {
  let query = {
    in: 'Events',
    where: { id: id },
    set: { updatedAt: new Date() } as IEventUpdate,
  };
  if (res.title) query.set.title = res.title;
  if (res.description) query.set.description = res.description;
  if (res.startDateTime) query.set.startDateTime = res.startDateTime;
  if (res.endDateTime) query.set.endDateTime = res.endDateTime;
  return await connection.update(query);
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by mukundshah
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant