briebug / ngrx-auto-entity

NgRx Auto-Entity: Simplifying Reactive State
https://briebug.gitbook.io/ngrx-auto-entity/
Other
66 stars 12 forks source link

Multiple stores (.forFeature()) with ngrx-auto-entity #191

Closed Inexad closed 2 years ago

Inexad commented 3 years ago

Is it possible to use multiple stores in ngrx-auto-entity ? I currently have 2 modules and different stores and states but i get following error:

[NGRX-AE] ! State for model CheckoutRow could not be found! Make sure you add your entity state to the parent state with a property named exactly 'checkoutRow'. Example app state:

export interface AppState {
  // ... other states ...
  checkoutRow: IEntityState<CheckoutRow>,
  // ... other states ...
}

I just want to make sure it is possible to use .forFeature() and multiple stores with this package?

Also thanks again for a great package !

UPDATE: I get these entity state errors, but data still loads into stores.

image

Inexad commented 3 years ago

Sorry for such a short description, but getting a bit crazy as i got 50+ different models, facades, effects etc and now wanting to divide them into multiple stores, this causes some briebug errors such as above

jrista commented 3 years ago

Hi @Inexad. This definitely should be possible. We put in effort to explicitly support feature modules, but it does require some specific setup.

Quick question...are you using buildState or buildFeatureState for your feature entities? If you are just using buildState that might be the problem. The buildFeatureState function lets you specify the feature name for your NgRx Feature state, and will accept the feature selector created with createFeatureSelector to ensure it can find the entity state in the right place.

Inexad commented 3 years ago

No I currently use buildState().

Do you have any example when using buildFeatureState and createFeatureSelector ?

Regards.

jrista commented 3 years ago

This should get you going:

https://briebug.gitbook.io/ngrx-auto-entity/advanced/usage/building-state/buildfeaturestate-func

Inexad commented 3 years ago

Thanks! I've changed to "buildFeatureState" and also creatingFeatureSelector. I now recieve a "Uncaught ReferenceError: Cannot access 'USER_FEATURE' before initialization". Tried to google this without success.

checkout-row.state.ts

export const { initialState, facade: CheckoutRowFacadeBase } = buildFeatureState(CheckoutRow, USER_FEATURE, userFeatureState);

// A "stub" reducer is required to support AOT
export function checkoutRowReducer(
  state = initialState,
): IEntityState<CheckoutRow> {
  return state;
}

user.state.ts

export interface IFeatureUserState {
  checkoutRow: IEntityState<CheckoutRow>;
...
};

export const USER_FEATURE = 'users';
export const userFeatureState = createFeatureSelector<IFeatureUserState>(
  USER_FEATURE
);

export const userReducers: ActionReducerMap<IFeatureUserState> = {
  checkoutRow: checkoutRowReducer,
...
};

user-store.module.ts

@NgModule({
  imports: [
    StoreModule.forFeature(USER_FEATURE, userReducers),
    EffectsModule.forFeature([
         CheckoutRowEffects,
...
   NgrxAutoEntityModule.forFeature();

  providers: [
    { provide: CheckoutRow, useClass: EntityService },
...
]);

user-layout.module.ts <- Lazy loaded module

@NgModule({
  imports: [
    UserStoreModule
...
]

app.module.ts


@NgModule({
    StoreModule.forRoot({}, {
      metaReducers: [clearState],
      runtimeChecks: {
        strictStateImmutability: true,
        strictActionImmutability: true,
      },
    }),
    AuthStoreModule, // Another feature with same setup as USER_FEATURE but with authentication models.
    EffectsModule.forRoot(),
    NgrxAutoEntityModule.forRoot(),
...
});

image

What i'm missing? Is it possible to have direct contact with you ? Maybe consulting you ?

Regards.

jrista commented 3 years ago

Hi @Inexad. I'll need a bit of time to digest your issue here, and I'll get back to you.

Can I ask for the versions of angular and ngrx that you are using?

jrista commented 3 years ago

@Inexad Could I also ask you to create a reproduction? StackBlitz or a public repo?

I suspect something with regards to feature state setup has changed in a more resent version of NgRx. I'll need to be able to test out the scenario and do some debugging to figure out a solution.

jrista commented 3 years ago

@Inexad Were you ever able to resolve this?

Inexad commented 3 years ago

Jon, sorry for late reply.

No I didn't and we had to move on to different parts of our projects due to time.

Sorry for not replying back for long time.

As I cannot provide example at the moment we can close this.

Regards

On Mon, 1 Nov 2021, 18:23 Jon, @.***> wrote:

@Inexad https://github.com/Inexad Were you ever able to resolve this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/briebug/ngrx-auto-entity/issues/191#issuecomment-956428906, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACBW7TGTP5QBMZPJTNK2DP3UJ3EJPANCNFSM5CJB4PMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

jrista commented 3 years ago

Ok, thanks for the update @Inexad. We don't have tons of issues, so I'll leave this open until you are able to return to it.

You should be able to break things up into feature stores. We will just need to figure out why its giving you a premature usage error and hopefully you'll be golden.

jrista commented 2 years ago

@Inexad Just wanted to give you an update. I just fixed a bug in our optional loading functionality. When trying to test that, I ran into the same issue you did, same error.

Turned out to be a circular reference issue. I had my FEATURE_NAME constant and featureState selector in a feature.state.ts file, which was importing each of my entity states. The entity states, then, were using buildFeatureState and needed FEATURE_NAME and featureState so they were importing from the feature state file. Even though the error was a circular reference issue, it was not reported as one, and took me a while to figure out my mistake.

I pulled out my FEATURE_NAME and featureSelector code into their own file, re-imported everything properly, and the issue went away. Hopefully this will help you resolve the issue in your own code base.