1

There are strong recommendations to flatten out the state tree for an ngrx store, and I've worked to do so throughout my app. I understand with, and agree with the reasons behind doing so- flat state trees are easier to manage, deep copy, etc.

However, I've run into a scenario that leaves me wondering the most maintainable way of setting up my state tree. I have a feature, Crews which contains a number of users on a Crew. An instance of a crew can have substitute users (e.g.- if someone on that crew is unavailable). A sub simply defines the original user ID, and the replacement user ID. As far as I can tell, there are two ways I can do this. The most straightforward way would be to have a non-flat structure- something that might look like this:

export interface User {
  id: string;
  username: string;
}

export interface Crew {
  id: string;
  members: string[];         //User IDs
  subs: [string, string][];  //[Orig UID, Sub UID]
}

export interface UserState extends EntityState<User> { }
export interface CrewState extends EntityState<Crew> { }

export interface AppState {
    users: UserState;
    crews: CrewState;
}

The other option, is to create a Subs feature, and the subs within Crew would point to an id in subs:

export interface User {
  id: string;
  username: string;
}

export interface Crew {
  id: string;
  members: string[];        //User IDs
  subs: string[];           //Sub ID
}

export interface Sub {
  id: string;
  original: string;         //Original UID
  sub: string;              //Sub UID
}

// Using ngrx entities
export interface UserState extends EntityState<User> { }
export interface CrewState extends EntityState<Crew> { }
export interface SubState extends EntityState<Sub> { }

export interface AppState {
    users: UserState;
    subs: SubState;
    crews: CrewState;
}

But this seems a bit overkill to me- creating an entire new feature, simply to represent two string ID values. Any thought on which direction would be most maintainable/straightforward, or a better way to represent this?

MarkD
  • 4,864
  • 5
  • 36
  • 67

0 Answers0