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

Is there a way to do a PATCH of an entity? #229

Open
grrenier opened this issue Aug 26, 2023 · 7 comments
Open

Is there a way to do a PATCH of an entity? #229

grrenier opened this issue Aug 26, 2023 · 7 comments

Comments

@grrenier
Copy link

Hello,
I was trying to update a single attribute of an entity but I just see update or save and they send all the attributes of the object in the payload. For example it would be nice to have something like:

remult.repo(MyEntity).patch(id, {isActive: true});

Thank you

@noam-honig
Copy link
Collaborator

I think that update will do exactly that:

remult.repo(MyEnity).update(id, {isActive:true})

Another option is change a member and send the update:

let myEntity = remult.repo(MyEntity).findFirst()
myEntity.isActive=true
remult.repo(MyEntity).save(myEntity)

@grrenier
Copy link
Author

I tried and it works, just the HTTP method is a PUT instead of a PATCH, but it does the job.

@groszdaniel
Copy link

@noam-honig Is update supposed to keep all fields that are not specified intact? It seems to me that in some circumstances other fields get overwritten (perhaps primitive types and/or allowNull?). Indeed, in the PUT network request some other fields (not specified in the second parameter of update) are specified with null values.

@noam-honig
Copy link
Collaborator

@groszdaniel yes - unspecified fields shoud stay intact. If not, it's a bug and I would like to fix it.
Can you give me some information on how to reproduce it?

@groszdaniel
Copy link

@noam-honig I made a simple example based on the React starter project: https://github.com/groszdaniel/remult-update-bug

One entity, two integer fields, each button is supposed to increment one without touching the other. Instead, when one is incremented, the other becomes 0. In the network request, you can see that a 0 is sent for the unspecified field. (Not in the example, but strings are set to an empty string, for instance.) The allowNull option doesn't actually matter.

If you fix this, I suggest also updating the documentation to make it clear that it's allowed to only specify some fields. Currently, all examples specify unchanged fields like this:
repo.update(id, { ...foo, field: value })
That's why it wasn't clear to me if it was a bug that other fields are zeroed, or intended behavior. The ...foo is currently necessary, but it should be superfluous if the bug is fixed.

@noam-honig
Copy link
Collaborator

Thanks. I'm out of town, will look at it tomorrow

@noam-honig
Copy link
Collaborator

Sorry about that, this issue was already fixed in version:
https://github.com/remult/remult/releases/tag/v0.22.4

My bad for not updating the starter kit - will do that now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants