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

Entity upsert wrongly documented + typing issue #818

Closed
miniplus opened this issue Feb 14, 2018 · 7 comments
Closed

Entity upsert wrongly documented + typing issue #818

miniplus opened this issue Feb 14, 2018 · 7 comments

Comments

@miniplus
Copy link

miniplus commented Feb 14, 2018

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request

What is the current behavior?

  • Using upsertOne and upsertMany from @ngrx/entity complains about typing (User vs Update<User>). See reproduction app/user.reducer.ts#14 & app/user.reducer.ts#18.
  • Using upsertOne and upsertMany functions from @ngrx/entity isn't updating the store correctly when entity or entities are passed to upsertOne or upsertMany as documenten in Medium Blogpost or github docs. Instead a type of Update<Entity> is expected (which is in the form of {id: number; changes: Partial<Entity>} (See reproduction example)

Possibly related to #817.

Expected behavior:

  • When entity is not in store yet, it's being added.
  • When entity is already in store, it's being updated
  • Typing in reducer when using adapter.upsertOne / adapter.upsertMany doesn't complain about type User vs Update<User>

Minimal reproduction of the problem with instructions:

https://stackblitz.com/edit/ngrx-upsert-bug

Version of affected browser(s),operating system(s), npm, node and ngrx:

ngrx store / entity 5.1.0

@MikeRyanDev
Copy link
Member

This is an issue with our docs. You are correct that the type signature is Update<Entity>.

@miniplus
Copy link
Author

miniplus commented Feb 15, 2018

Is this by design or by accident?

I figured I would just pass the entity to the API and it would be handled behind the scenes.
As the docs and medium blogpost do this as well, wouldn't it be the more logical thing to do as more people would expect it to function that way?

For now I agree it is a docs issue, however I think the API consumer shouldn't pass an Update<User> but instead a User.

#817 Seems to agree.

(Note: The Update object seems to be necessary here, but shouldn't, see #818 )

@miniplus miniplus changed the title Entity upsert not working correctly + typing issue Entity upsert wrongly documented + typing issue Feb 15, 2018
@fmalcher
Copy link
Contributor

fmalcher commented Feb 15, 2018

#817 Seems to agree.

I like the way the upsert is being described in the docs:

return adapter.upsertOne(myEntity, state);

IMO, upsert is an insert that silenty updates existing entities. The API for this should be closer to "add" than to "update". Using upsert with the current API will probably always look like this:

const update = { id: myEntity.id, changes: myEntity };
return adapter.upsertOne(update, state);

The Update object always has to be created manually, even though it always looks the same.
I'd really like to see the API in the first proposed way (the one that is in the docs).

Thank you!

@sandangel
Copy link
Contributor

I have the same idea too. But there was a long discussion and everyone have agreed that upsert should have the same signature with update so I made PR following that. #421

@miniplus
Copy link
Author

Docs fixed in 1ffb5a9.
I also believe just the Entity should be the arg instead of Update<Entity>, but let #817 carry on that discussion if needed, no use having 2 open issues for somewhat the same thing.

@Mathijs003
Copy link

I'm having an issue with the typing too. When an Object of get's updated in the state, the type is lost and it's just an Object

@Mathijs003
Copy link

@miniplus can you reopen this issue?

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

No branches or pull requests

5 participants