@ngrx/entity

  • Version 22.0.0
  • Published
  • 276 kB
  • 1 dependency
  • MIT license

Install

npm i @ngrx/entity
yarn add @ngrx/entity
pnpm add @ngrx/entity

Overview

Common utilities for entity reducers

Index

Functions

function createEntityAdapter

createEntityAdapter: {
<T extends { id: string | number }>(options?: {
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T, T extends { id: infer U } ? U : never>;
<T>(options: {
selectId: IdSelectorStr<T>;
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T, string>;
<T>(options: {
selectId: IdSelectorNum<T>;
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T, number>;
<T>(options: {
selectId: IdSelector<T>;
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T, string | number>;
<T>(): EntityAdapter<T, string | number>;
};
  • Creates an entity adapter with methods for managing collections of entities in state.

    The entity adapter provides a set of predefined reducers and selectors for managing normalized entity collections. It includes methods for adding, updating, removing, and selecting entities from state.

    T - The entity type to be managed by the adapter

    Parameter options

    Configuration options for the adapter

    Parameter

    options.selectId - A function that selects the unique identifier from an entity. Defaults to (entity) => entity.id if not provided.

    Parameter

    options.sortComparer - A comparer function for sorting entities in state. Set to false (default) to maintain insertion order, or provide a comparer function to keep entities sorted.

    Returns

    An entity adapter containing methods for managing and selecting entity collections in state.

    Example 1

    // Entity with default 'id' property
    interface User {
    id: number;
    name: string;
    }
    const userAdapter = createEntityAdapter<User>();
    // Entity with custom id field
    interface Book {
    isbn: string;
    title: string;
    }
    const bookAdapter = createEntityAdapter<Book>({
    selectId: (book) => book.isbn,
    });
    // Entity with sorting
    const sortedBookAdapter = createEntityAdapter<Book>({
    sortComparer: (a, b) => a.title.localeCompare(b.title),
    });

Classes

class Dictionary

abstract class Dictionary<T> implements DictionaryNum<T> {}

    Interfaces

    interface DictionaryNum

    interface DictionaryNum<T> {}

      index signature

      [id: number]: T | undefined;

        interface EntityAdapter

        interface EntityAdapter<T, IdType = string | number> extends EntityStateAdapter<T> {}

          property selectId

          selectId: IdType extends string
          ? IdSelectorStr<T>
          : IdType extends number
          ? IdSelectorNum<T>
          : unknown;

            property sortComparer

            sortComparer: false | Comparer<T>;

              method getInitialState

              getInitialState: {
              (): EntityState<T>;
              <S extends EntityState<T>>(state: Omit<S, keyof EntityState<T>>): S;
              };

                method getSelectors

                getSelectors: {
                (): EntitySelectors<T, EntityState<T>>;
                <V>(selectState: (state: V) => EntityState<T>): MemoizedEntitySelectors<
                T,
                V
                >;
                };

                  interface EntityState

                  interface EntityState<T> {}

                    property entities

                    entities: Dictionary<T>;

                      property ids

                      ids: string[] | number[];

                        Type Aliases

                        type Comparer

                        type Comparer<T> = (a: T, b: T) => number;

                          type EntityMap

                          type EntityMap<T> = (entity: T) => T;

                            type EntityMapOne

                            type EntityMapOne<T> = EntityMapOneNum<T> | EntityMapOneStr<T>;

                              type EntitySelectors

                              type EntitySelectors<T, V> = {
                              selectIds: (state: V) => string[] | number[];
                              selectEntities: (state: V) => Dictionary<T>;
                              selectAll: (state: V) => T[];
                              selectTotal: (state: V) => number;
                              };

                                type IdSelector

                                type IdSelector<T> = IdSelectorStr<T> | IdSelectorNum<T>;

                                  type MemoizedEntitySelectors

                                  type MemoizedEntitySelectors<T, V> = {
                                  selectIds: MemoizedSelector<
                                  V,
                                  string[] | number[],
                                  (entityState: EntityState<T>) => string[] | number[]
                                  >;
                                  selectEntities: MemoizedSelector<
                                  V,
                                  Dictionary<T>,
                                  (entityState: EntityState<T>) => Dictionary<T>
                                  >;
                                  selectAll: MemoizedSelector<V, T[], (entityState: EntityState<T>) => T[]>;
                                  selectTotal: MemoizedSelector<
                                  V,
                                  number,
                                  (entityState: EntityState<T>) => number
                                  >;
                                  };

                                    type Predicate

                                    type Predicate<T> = (entity: T) => boolean;

                                      type Update

                                      type Update<T> = UpdateStr<T> | UpdateNum<T>;

                                        Package Files (1)

                                        Dependencies (1)

                                        Dev Dependencies (0)

                                        No dev dependencies.

                                        Peer Dependencies (3)

                                        Badge

                                        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@ngrx/entity.

                                        • Markdown
                                          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@ngrx/entity)
                                        • HTML
                                          <a href="https://www.jsdocs.io/package/@ngrx/entity"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>