Skip to content
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

Closed
pantharshit00 opened this issue Sep 23, 2020 · 6 comments · Fixed by prisma/prisma-engines#1753
Closed
Assignees
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: scalar list []
Milestone

Comments

@pantharshit00
Copy link
Contributor

Problem

Right now we only provide a set method for scalar lists

For example, lets say we have this model:

model User {
  id         String   @id @default(cuid())
  permission String[]
}

We generate the following update type:

export type UserUpdatePermissionsInput = {
  set?: Enumerable<string> | null
}

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

export type UserUpdatePermissionsInput = {
  set?: Enumerable<string> | null
  push?: Enumerable<string> | null
}

Under the hood this can be backed by the || postgres operator or the array_append method

Alternatives

I am not sure how we can provide a better way of doing this right now.

Additional context

Suggested by @kitze in his livestream

@pantharshit00 pantharshit00 transferred this issue from prisma/prisma Sep 23, 2020
@pantharshit00 pantharshit00 changed the title Add a push method to scalar list update input in addition to select Add a push method to scalar list update input in addition to set Sep 23, 2020
@pantharshit00 pantharshit00 transferred this issue from prisma/prisma-client-js Jan 13, 2021
@pantharshit00 pantharshit00 added kind/feature A request for a new feature. topic: postgres list team/client Issue for team Client. labels Jan 13, 2021
@matthewmueller matthewmueller added this to the 2.18.0 milestone Feb 17, 2021
@matthewmueller matthewmueller modified the milestones: 2.18.0, 2.19.0 Mar 3, 2021
@matthewmueller
Copy link
Contributor

matthewmueller commented Mar 9, 2021

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.

@pantharshit00
Copy link
Contributor Author

pantharshit00 commented Mar 9, 2021

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.

@kladnik
Copy link

kladnik commented Mar 16, 2021

This would come in very handy. This would simplify things quite a bit.

@jakeleventhal
Copy link

jakeleventhal commented Mar 2, 2023

What do folks in this thread think about adding a "pushUnique"

which essentially would skip duplicates

Update: #19370

@David-crypto919
Copy link

How can this functionality be achieved in a MongoDB database?

@Viriatto
Copy link

How can this functionality be achieved in a MongoDB database?

What do folks in this thread think about adding a "pushUnique"

which essentially would skip duplicates

Update: #19370

In MongoDB it's called $addToSet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: scalar list []
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants