ngrx/platform

Problem with custom id entities in @ngrx/signals/entities

Closed this issue · 5 comments

Which @ngrx/* package(s) are the source of the bug?

signals

Minimal reproduction of the bug/regression with instructions

I have defaultParmasIds and DefaultParamsEntitymap are populated, but not DefaultParams.

interface State {
  defaultParams: DefaultParams[];
}

const initialState: State = {
  defaultParams: [],
};

const selectDefaultParamsId: SelectEntityId<DefaultParams> = param => param.Id;

export const Store = signalStore(
  { providedIn: 'root' },
withState(initialState),
  withEntities({
    entity: type<DefaultParams>(),
    collection: 'defaultParams',
  }),
withMethods(state => {
    const defaultParametersService = inject(DefaultParametersService);

    return {
      loadDefaultParams: async () => {
        const defaultParams = await lastValueFrom(
          defaultParametersService.listDefaultParameters()
        );

        patchState(
          state,
          setAllEntities(defaultParams, {
            collection: 'defaultParams',
            selectId: selectDefaultParamsId,
          })
        );

        return defaultParams;
      },
}))

Expected behavior

Have DefaultParams populated

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

Angular CLI: 18.1.0
Node: 22.5.1
Package Manager: npm 10.8.2
OS: linux x64

Angular: 18.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.1801.0
@angular-devkit/build-angular 18.1.0
@angular-devkit/core 18.1.0
@angular-devkit/schematics 18.1.0
@angular/cdk 18.1.1
@schematics/angular 18.1.0
rxjs 7.8.1
typescript 5.4.5
zone.js 0.14.7
"@ngrx/signals": "^18.0.0-rc.2",

Other information

Workaround, add
patchState(state, { defaultParams });

I would be willing to submit a PR to fix this issue

  • Yes
  • No

initialState is never used in the code snippet you shared, but anyway, I think I understand what you're trying to achieve.

If you're trying to manage the array named defaultParams by using withEntities with named collection defaultParams:

withState({ defaultParams: [] }),
withEntities({
  entity: type<DefaultParams>(),
  collection: 'defaultParams',
}),

This is not how the entities plugin works.

Learn more about the @ngrx/signals/entities plugin including named entity collections in the official entity management guide: https://ngrx.io/guide/signals/signal-store/entity-management

For further questions, feel free to join our Discord server: https://discord.gg/ngrx

Yes sorry i forgot the withState part :
withState(initialState),

This is not how the entities plugin works.

I think you might not fully understand my issue @markostanimirovic :) I am using an entity with an ID that has the name 'Id' instead of 'id' (unfortunately, that's how the API is designed).

Can you use Stackblitz and create a reproduction of the issue: https://stackblitz.com/~/github.com/rainerhahnekamp/ngrx-signal-store-starter?file=package.json

When reporting a bug, it's necessary to create a reproduction via Stackblitz or Github repo.

@alcaidio I just rechecked the description. defaultParams will never be populated because withEntities doesn't control external state properties. Instead, withEntities exposes a signal computed from entityMap and ids that contains an array of all entities.

By default, the name of this computed signal is entities. In your case (collection name defaultParams), the signal that contains the array of all entities is named defaultParamsEntities. So you can use it and remove the defaultParams array from the state.

Check the Entity Management guide, especially the Named Entity Collections section for more info.

Ho my bad thanks to get time to correct me :)