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 Adapter #56

Closed
amcdnl opened this issue Mar 9, 2018 · 11 comments
Closed

Entity Adapter #56

amcdnl opened this issue Mar 9, 2018 · 11 comments

Comments

@amcdnl
Copy link
Member

amcdnl commented Mar 9, 2018

A while back I prototyped something like John Papas NGRX-DATA, https://gist.github.com/amcdnl/cafd37bc9a99e2cd7653ebc023e06ffe#file-readme-md

Something we might want to think about one day.

@eugene-s
Copy link

Yes, it would be nice. Entity adapter gives more comfortable things to manipulate with storage.

@wesleygrimes
Copy link

I am switching over from NGRX where I heavily used the NGRX Entity modules. Would love to see something like this implemented for NGXS.

@amcdnl amcdnl added the future label May 8, 2018
@amcdnl
Copy link
Member Author

amcdnl commented May 8, 2018

Closing and marking for future.

@amcdnl amcdnl closed this as completed May 8, 2018
@filipjnc
Copy link

How far in the future is the entity adapter planned for? This is the only reason why I’m hesitating to switch. The feeling sucks because I can’t wait to switch.

@abalad
Copy link

abalad commented Aug 13, 2018

Is there a provision for such an appeal? I'd love to see this in NGXS

@dfulgham
Copy link

dfulgham commented Sep 10, 2018

When patching your state instead of just using the array of values (data) use:

store.patchState({ entities: Object.assign(
            {},
            ...data.map(item => ({ [item["id"]]: item }))
          )})

In ngrx-data they also have an array of id's, so you could add that as well.

store.patchState({ 
    entities: Object.assign(
            {},
            ...data.map(item => ({ [item["id"]]: item })),
    ids: ...data.map(item => item["id"] })
})

@abalad
Copy link

abalad commented Sep 10, 2018

I have created a LIB to be used as follows. I'm using it on mine.
projects:

How to use:

export class UserStateModel extends NgxsEntityStateModel<UserModel> {}

@State<UserStateModel>({
  name: 'user',
  defaults: UserStateModel.InitialState()
})
export class UserState implements NgxsOnInit {}

Methods Available:
NgxsEntityAdapter.addAll( payload, ctx );
NgxsEntityAdapter.addOne( payload, ctx );
NgxsEntityAdapter.updateOne( payload, ctx );
NgxsEntityAdapter.removeOne( payload, ctx );

NgxsEntityStateModel Class:

export class NgxsEntityStateModel<T> {
  public ids: string[];
  public entities: { [id: string]: T };
  public selected: T | null;
  public isLoading: boolean;

  static InitialState() {
    return {
      ids: [],
      entities: {},
      selected: null,
      isLoading: false
    };
  }
}

#541 (comment)

@markwhitfeld
Copy link
Member

@abalad I would love to see what this lib looks like!

@markwhitfeld markwhitfeld reopened this Sep 10, 2018
@abalad
Copy link

abalad commented Sep 11, 2018

Is only a class with static methods for now, but I want to do better integration within NGXS:

Example: https://gist.github.com/abalad/d9c82af8bcdce904bb173a7e1fe3edaa

Lib:

@juracy
Copy link
Contributor

juracy commented Sep 25, 2018

A proposal for entity through decorator: #580
I would like to discuss the idea!

@splincode
Copy link
Member

splincode commented Nov 28, 2018

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

9 participants