-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
(entity) Upsert Add overrides the id property #817
Comments
I worked on fixing this, however this one is tightly coupled to #818 . Look at this test case (from the actual unit tests):
It uses However, in my opinion, the upsert methods should take a complete entity as argument, as proposed in #818 and described in the docs. The upsert methods should:
What do you think? @MikeRyanDev @brandonroberts @miniplus [1] https://github.com/ngrx/platform/blob/master/modules/entity/src/sorted_state_adapter.ts#L125 |
there was a long discussion about this: #421 |
Thanks for this reference! However, I dont really see a discussion about this topic there. Do you see a way to fix this issue here without revolutionizing the whole concept again? |
agree. I love the add signature for upsert too. Hope |
Decisions were made and I think, this has been discussed thoroughly. There seem to be reasons and I'm sure they're valid. What remains is that
This is the thing @dinvlad talked about in #421 (comment) |
In NgRx 6 will introduce a breaking change that updates the upsertOne<T>(upsert: { id: string | number, entity: T }, state: EntityState<T>);
upsertMany<T>(upserts: { id: string | number, entity: T ][], state: EntityState<T>); |
This is great news! How can we fix the id property issue until then? |
I'll provide a PR for the 6.x change. @fmalcher I think that this is unsolvable right now (with my limited knowledge). The only way of a 'kinda fix' that I can think of is the following - but this would require having the
If you need the fix right now, I believe the only option would be to use a fork (with the fix) or to create an own adapter. |
@MikeRyanDev appreciate the heads up on the breaking change. I'd previously been using the pattern of AddOne from the example app from the post request (which means using (result: Model) instead of (result: {model: Update<Model>}). Is there a template for making the added elements play well together in effects? This is my current best guess for updating a photo object...anything you'd suggest to clean it up or make it future proof (v6)? (It looks like I would rename 'changes:' to 'entity:' )
|
Fixed via a0f45ff |
I'm submitting a...
What is the current behavior?
The
upsert
methods of theEntityStateAdapter
override theid
property, even if this is not the primary entity key. This only happens when adding with upsert.Expected behavior:
ngrx/entity should not make any changes to the added entity. It should leave the
id
property as is OR should change the property we assigned with theselectId
function.Explanation / reproduction
DEMO: https://stackblitz.com/edit/ngrx-upsert-bug-r96kbx
(thanks to @miniplus for the StackBlitz skeleton)
Imagine an entity like a book whose "primary key" is not named
id
butisbn
.However, this entity also has an
id
property which is not the primary key.(We might discuss why there is an
id
property which is not the ID, but that's not the point here at all 😉 )To make ngrx/entity use the
isbn
property as key, we set theselectId
selector function accordingly:Now, in the reducer, we're using the new
upsertOne
/upsertMany
methods from the StateAdapter to add a new entity which is not yet in the entity list:(Note: The
Update
object seems to be necessary here, but shouldn't, see #818 )This leads to the following entities object in the state:
...where it actually should be
Notice, when doing an upsert as update (i.e. for an entity that already exists), the
id
property is set correctly.Where's the error?
The issue might be in this line:
https://github.com/ngrx/platform/blob/master/modules/entity/src/sorted_state_adapter.ts#L125
This sets/overrides the
id
property, regardless of whether this is the actual ID or not.Possible solution
Remove the line completely.
update.changes
contains the complete object to be added and there is no need to set the ID.If we want to make sure that
update.id
and the ID from the entity are the same we should check this here using theselectId()
function.I'd like to submit a PR but wanted to discuss this first.
Thanks!
Affected versions
@ngrx/entity 5.1.0
The text was updated successfully, but these errors were encountered: