New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a push
method to scalar list update input in addition to set
#5078
Comments
push
method to scalar list update input in addition to selectpush
method to scalar list update input in addition to set
I talked to @pantharshit00 about this and he mentioned it was really painful to do this right now. Next Step: Create an example to demonstrate this pain. |
Here is the example: In this example, I want to make the user admin so I need to append the admin permission to the user. Here is how you would do it right now. const existingRecord = await prisma.user.findOne({ where: { id });
const updatedUser = await prisma.user.update({ where: { id }, data: { permission : { set: existingRecord.permission.push("ADMIN") }); You will need to read the record if you want to just add another item to the scalar list. vs if we had a push method const updatedUser = await prisma.user.update({ where: { id }, data: { permission : { push: ["ADMIN"] }); // push can be enumerable This example is fairly common for an app which has roles. We already implemented some additional scalar list filters so I think we can also add some write helpers here if we have the capacity. |
This would come in very handy. This would simplify things quite a bit. |
What do folks in this thread think about adding a "pushUnique" which essentially would skip duplicates Update: #19370 |
How can this functionality be achieved in a MongoDB database? |
Problem
Right now we only provide a
set
method for scalar listsFor example, lets say we have this model:
We generate the following update type:
This is not ideal when you want to push items to the array. For that right now you will need to read the whole record and add the previous items to the old array and then set it which costs a db request and it is not the best DX.
Suggested solution
Add a push key to the update input which will add the new item instead of resetting the
set
Under the hood this can be backed by the
||
postgres operator or thearray_append
methodAlternatives
I am not sure how we can provide a better way of doing this right now.
Additional context
Suggested by @kitze in his livestream
The text was updated successfully, but these errors were encountered: