question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implementation for conditional updates

See original GitHub issue

Problem

It would be nice to update a field if a specific condition is met. For example an enum or string field “role”:

UPDATE `User` SET role = IF(role = 'Incomplete', 'Premium', role), verified = 1
WHERE emailValidated = 1

Suggested solution

Add “filter” to StringFieldUpdateOperationsInput and other types:

  export type StringFieldUpdateOperationsInput = {
      set?: string
      filter?: { condition: StringFilter, set: string }
  }

Alternatives

  • Doing a select and update will be an easy workaround, but it is not atomic anymore.
  • Doing two updates will be atomic at the cost of speed/DB usage …
  • Write raw sql 😦

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
Duckicommented, Nov 1, 2021

I’d like to add another example use case: Update a record based on the unique id AND a second field, e.g. if a createdByUserId field matches the currently loggend in user. In raw SQL, that’s perfectly fine, but with Prisma, I first habe to fetch the company row, then check my second condition, and then update.

7reactions
asciantcommented, Oct 5, 2021

I appreciate this is an old issue, although I just wanted to add that it would be nice to have an atomic way of doing this.

In my case, I’m needing to do an initial query to obtain the record, once it has been retrieved I check a condition and then update accordingly. It would be good to do this in an atomic way.

Using the example from Matthew (because my data, demonstrated here as the user’s role, is not immediately available to the function, I have to obtain it first and then do an update separately):

const user = await prisma.user.findUnique({
  where: {
    id: 1
  }
})

await prisma.user.update({
  data: {
    role: user.role === 'Incomplete' ? 'Premium' : user.role,
    verified: 1
  },
  where: {
    id: 1
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional updating
The Conditional Update action allows you to set the value of a single new or existing field by using calculated expressions based on...
Read more >
Tablestore:Conditional update - Alibaba Cloud
Conditional update can be used to implement optimistic locking. When you update a row, the value of the specified column is obtained.
Read more >
Performing a Conditional Put - Amazon SimpleDB
Conditional updates are useful for preventing lost updates when different sources concurrently write to the same item. Note. Conditional puts can only match ......
Read more >
How to implement complex conditional bulk partial updates ...
Sure there will be both audit and database and the rest. The question was rather about the transmission JSON data format that would...
Read more >
Conditionally Updating Columns - GeeksforGeeks
In this article, we will discuss how we can conditionally update columns and discuss how IF, IF EXISTS and IF NOT EXISTS conditional...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found