Implementation for conditional updates
See original GitHub issueProblem
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:
- Created 3 years ago
- Reactions:11
- Comments:8 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d like to add another example use case: Update a record based on the unique
id
AND a second field, e.g. if acreatedByUserId
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.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):