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

Updating partial data #26

Open
mecsco opened this issue Dec 4, 2017 · 7 comments
Open

Updating partial data #26

mecsco opened this issue Dec 4, 2017 · 7 comments

Comments

@mecsco
Copy link

mecsco commented Dec 4, 2017

If I want to update a certain value (or values) in an object (whether the top-level, a child, or a grandchild), what is the preferred option? If I pass the ID and the name/value of the property and use the Add/Set methods, then any other properties that I'm not passing are removed.

@ngrx/entity (https://github.com/ngrx/platform/blob/master/docs/entity/adapter.md) has update methods which take a Partial<T> to only update the changes - have you considered something similar?

@michaelkrone
Copy link
Owner

I did not thought about that yet, but I see that this is a common use case.
For not breaking the current behaviour of the AddData action, an UpdateData action might be a simple solution.
It can take the entity id, the schema and data to be set. I will give it a try in the develop branch, maybe you can provide some feedback then.

@mecsco
Copy link
Author

mecsco commented Dec 4, 2017

Great thanks! I'd be happy to help

@michaelkrone
Copy link
Owner

A first work in progress is available in the development branch, if you want to give it a try.
You might use the UpdateData or the updateData action creator like this:

// changes is a Partial<Type>
this.store.dispatch(new UpdateData<Type>({id, changes, schema}));
updateData(id, changes);

@mecsco
Copy link
Author

mecsco commented Dec 5, 2017

Wow thanks for looking at this so quickly! I've given it a quick test and it looks like it is mostly working for my scenario of updating a single value in a certain object.

However one oddity was in the Redux extension in Chrome - if I've executed an AddData and view the full raw state, I can see the correct values. Once I execute an UpdateData, the diff doesn't show any changes - but if I click back to the previous AddData entry and again view the full raw state, it has the values post-UpdateData. I'm a bit new to this still but does that suggest the reducer is mutating the existing state?

I did experiment a bit with sending some partial objects with nesting and ran into a few issues. There's every chance I'm doing something wrong, but here's a simplified version of what I have:

interface MyParent { id: string; status: string; children: MyChild[]; }
interface MyChild { id: string; status: string; }

new UpdateData<MyParent>({ 
  id: 'abc', 
  changes: { status: 'online', children: [{ id: 'xyz', status: 'offline' }] }, 
  schema: parentSchema })

This results in the 'children' array under entities/MyParent/abc having 2 'xyz' entries. Under entities/MyChild, there is still just one 'xyz' entry but a separate 'id' value has been added to it

As I said, perhaps I'm doing something wrong here - maybe if I wanted this behaviour (which I don't currently need), then I should just use mergeMap in my @ngrx/effect and dispatch multiple UpdateData actions.

@mecsco mecsco mentioned this issue Dec 5, 2017
@michaelkrone
Copy link
Owner

Thank you @mecsco for your feedback. You usage looks absolutely valid, and you are right, the code seems to mutate the state data and adding childs has still to be done properly. I will refactor the code using non mutating functions, as this is related to #27

@mecsco
Copy link
Author

mecsco commented Dec 20, 2017

Do you have a rough idea of when you'll get a chance to look at this issue, and the related #27?

Thanks in advance

@pierresigwalt
Copy link

Hi,

Do we know when the feature will be pushed to master?

Thank you

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

3 participants