-
Notifications
You must be signed in to change notification settings - Fork 862
Atomic operations #1349
Comments
Very much looking forward to this functionality, as my app relies heavily on operations like adding/subtracting for managing balances on user types. Using a custom built mutex is such a pain and adds extra/unnecessary time to a mutation (plus can be unreliable at times). Thanks for adding this issue/update! |
I look forward to using this as it reduces a lot of complexity when working with |
Just an example of how much complexity these atomic operations would reduce in my case. Compare the two implementations Model
Resolver
Resolver ImplementationCreate or Update + Increment
VS Upsert with atomic operations
|
Thanks Marc! This is exactly the kind of motivating use case that help us prioritise feature requests. |
any ETA on this? We also need auto-increment (I'm surprised this isn't more widely needed!) @sorenbs |
Any update on atomic operation? Right now the two solutions are
This would be so simple in SQL, please find a way to implement. At least any timeline on this? |
Any update on this ? |
This continues to be an important feature for us. I'll update this issue when we have a concrete timeframe. See also this explanation for why we were unable to ship this feature in Q3 as planned. |
Hi, since we're already talking about a similar issue on another thread, maybe I can chime in here with a suggestion:
This is relatively simple and the use of |
@mcmar - do you think this is cleaner than the proposed One concern I have is that you will have a very long auto-complete list in your IDE containing all these atomic operations for each field. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 10 days if no further activity occurs. Thank you for your contributions. |
@sorenbs Any update on this? |
any ETA ? |
any updates to this ? is it available ? |
any updates? |
Hope this could be resolved soon. |
Any update on this? Would be amazing to have this one. |
Keep the thread alive. Any news about this? Thanks |
Need this as well |
I hope this will be implemented soon. |
looking forward seeing this soon, what is the best working around for now? |
I hate to spam you guys, but I'm also looking forward to this feature. I've been watching this thread for more than a year now hoping it arrives soon. It has stopped my company from releasing certain features on Prisma various times. |
Push it to hope this will be implemented soon. |
I hope this will get implemented by 2020.. |
|
Are there any recommended workarounds? I've seen people grabbing the last result and incrementing it, but that could still lead to problems if multiple requests come in at the same time. I also saw this library implementing a mutex strategy for Any other recommendations? |
For everyone who wants to see this feature for Prisma 2, please upvote the respective issue for atomic operations in Prisma 2. |
GraphQL limitation and workaround
The most natural way to express atomic operations would be to allow them inline in any place where you would normally specify a value to set:
Set age to 7:
Increment age by 1:
Unfortunately, this is not valid GraphQL.
[RFC] GraphQL Input Union type
There is an RFC trying to add union input types to GraphQL. It proposes using an extra field to specify the input type.
The example above updated according to the RFC could look like this:
Set age to 7:
Increment age by 1:
Increment age and lives by 1:
The added overhead of an extra field just to specify the type of the input object has a prohibitively negative impact on developer ergonomics.
Furthermore, it results in a combinatorial explosion as it should be possible perform atomic operations on multiple fields at the same time.
Workaround for missing union input type support
As we can not use a union input type, we will work around it by adding an extra field to all data input types:
The field is prefixed with
_
in order to avoid clashing with a field on the type calledatomic
.What does it look like
Atomic Operations for top level
update
mutationThe data argument receives an extra optional
atomic
field containing all model fields that are able to perform atomic operations.Given this schema
This will increment the age by 1:
Multiple operations can be performed transactionally at once:
Atomic Operations for top level
upsert
mutationThe update part of
upsert
is identical to the normalupdate
mutation described above:Atomic operations for nested
update
mutationSupported both on one and many relations:
Atomic operations for nested
upsert
mutationThe update part of the upsert is similar to a normal nested update:
Supported atomic operation
Int
inc
add given valuemul
Multiply by given valuemin
Set to given value if it is smaller than currentmax
Set to given value if it is larger than currentFloat
inc
add given valuemul
Multiply by given valuemin
Set to given value if it is smaller than currentmax
Set to given value if it is larger than currentBoolean
flip
Set to the opposite of current value. No change if nullOthers
Precedence
If both an atomic operation and a normal set data operation is specified for a field the atomic operation it takes precedence. For example, this mutation increments the age by 1 instead of setting it to 7:
The text was updated successfully, but these errors were encountered: