Skip to content

@ngrx/entity add mapOne method #2538

@awonihlgard

Description

@awonihlgard

Similiar to setOne and updateOne, it would be useful with a mapOne for cases where you want to update a single entity but the new value depends on the old. Example:

return adapter.mapOne(id, entity => {
    ...entity,
    counter: entity.counter + 1
}, state);

The naming of map would then be a bit inconsistent, but that might be fine. For full consistency the naming would mapOne and mapAll (mapMany could also be added for completeness).

Describe any alternatives/workarounds you're currently using

The same thing can be accomplished by accessing the old entity with state.entities, but it becomes a bit more verbose if you also want to check for existence:

const entity = state.entities[id];
if (entity) {
    return state.updateOne({
        id,
        changes: {
            counter: entity.counter + 1
        }
    })
} else {
    return state;
}

If accepted, I would be willing to submit a PR for this feature

[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions