Reputation: 9483
I am trying to use @ngrx/data to create a simple service in Angular, and am receiving the error EntityDefinition for entity type "Position".
when the component runs.
My EntityMetadataMap is this:
const entityMetaData: EntityMetadataMap = {
Employee: {},
Position: {}
};
const pluralNames = { Employee: 'Employees', Position: 'Positions' };
export const entityConfig = {
entityMetaData,
pluralNames,
};
and my store module is this:
@NgModule({
declarations: [],
imports: [
StoreModule.forRoot({}),
EffectsModule.forRoot([]),
StoreDevtoolsModule.instrument(),
EntityDataModule.forRoot(entityConfig),
],
providers: [
{provide: DefaultDataServiceConfig, useValue: defaultDataServiceConfig},
PositionsDataService
]
})
export class ItwStoreModule {
constructor() {
console.log(entityConfig);
}
}
and my data service for position is this:
@Injectable({ providedIn: 'root' })
export class PositionsDataService extends EntityCollectionServiceBase<Position> {
constructor(serviceFactory: EntityCollectionServiceElementsFactory) {
super('Position', serviceFactory);
}
}
I can put a break point in the module setup and in EntityDataModule and I can see that the entity definition exists and is getting set correctly (as part of EntityDataModule.forRoot(entityConfig)
.
But when the constructor of the service fires and calls super(...)
, in the debugger, I can see that there are no EntityDefinitions in the EntityCollectionServiceElementsFactory
that is passed in (note the "definitions" are empty):
What am I doing wrong here?
Obviously, between when the store module is initiated and when the dependency injection injects the EntityCollectionServiceElementsFactory
into the service, something is going wrong.
Upvotes: 3
Views: 4022
Reputation: 26266
While providing the config, be sure to use property name entityConfig
. For example:
const mySampleEntityMetadata : EntityMetadataMap = {
Hero: {},
Villain: {}
};
...
export const entityConfig = {
entityMetadata: mySampleEntityMetadata,
pluralNames
};
Upvotes: 1
Reputation: 6811
To work easily with entities, here is an example
In your store.states.ts
export interface State {
store_A: STATE_A;
}
export interface STATE_A extends EntityState<ClassA> {}
export const adapter: EntityAdapter<ClassA> = createEntityAdapter<ClassA>({
selectId: (params: ClassA) => params.id
});
export const initialState: State = {
store_A: aAdapter.getInitialState({})
};
In your store.selector.ts
export const getMainState = createFeatureSelector<State>('root-feature');
export const getStateA = createSelector(getMainState , (state) => state.stateA);
export const getAllStateA = createSelector(getStateA , (state) => adapter.getSelectors().selectAll);
In your service or component:
this.store.select(getAllStateA) // Observable of all item A
In your reducer:
...
on(
MyAction,
(state, { itemAs }) => {
return adapter.addAll(itemAs , { ...state });
}
),
...
Upvotes: 2