Closed juanpmarin closed 6 years ago
Hello @juanpmarin Out of the box, ngrx-normalizr does not provide a selector for denormalizing a set of id's (Would you like to add this feature by a Pull Request?)
However, it should be possible to achieve selecting a set of id's with the exported selectors. Let's assume you have a state property which holds your selected id's:
const entityState = {
...
selectedIds: string[]
}
Then we need a selector for the state property:
const getSelectedIds = createSelector(
someStateSelector,
(state: State) => state.selectedIds
);
We might then create a selector for denormalizing entities for the selected id's with a schema selector:
const schemaSelectors = createSchemaSelectors<Entity>(entitySchema);
const getSelectedEntities = createSelector(
// get all denormalized entities of the schema
schemaSelectors.getEntities,
// get the selected id's from the feature state
getSelectedIds,
(entities, ids) => entities.filter(e => ids.indexOf(e.id) > -1)
);
Or maybe as a selector creator function:
function getSelectedEntitiesSelector(ids: string[]) {
return createSelector(
schemaSelectors.getEntities,
entities => entities.filter(e => ids.indexOf(e.id) > -1)
);
}
The selection could be more efficient if ngrx-normalizr would accept an array of id's for the entitiesProjector, because with the approach above, all entities of the schema bound selector will be denormalized. Good point for a feature!
Hi ! thanks for answering I made a PR, let me know if it fit your expects.
Thank you for your PR @juanpmarin This issue will be closed when 2.1.0 is published.
Hi @michaelkrone Is it possible to release this feature ? I'm gonna need this on my project and I don't want to use the workaround and do the refactor later Thanks !
2.1.0 is published, thank you @juanpmarin
@michaelkrone Great, thanks !
Hi, thanks for this lib ! There is a way to select entites by a list of ids ?