Skip to content

Commit 6bd5ad5

Browse files
docs(signals): add API docs for Entities plugin (#5071)
1 parent b62bb91 commit 6bd5ad5

17 files changed

+282
-0
lines changed

modules/signals/entities/src/entity-config.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ export function entityConfig<Entity, Collection extends string>(config: {
1414
collection: Collection;
1515
}): typeof config;
1616
export function entityConfig<Entity>(config: { entity: Entity }): typeof config;
17+
/**
18+
* @description
19+
*
20+
* Creates a custom entity configuration and ensures strong typing.
21+
* Allows defining named entity collections and a custom id selector.
22+
*
23+
* @usageNotes
24+
*
25+
* ```ts
26+
* import { signalStore, type, withMethods } from '@ngrx/signals';
27+
* import { addEntity, entityConfig, withEntities } from '@ngrx/signals/entities';
28+
*
29+
* type Todo = { key: number; text: string };
30+
*
31+
* const todoConfig = entityConfig({
32+
* entity: type<Todo>(),
33+
* collection: 'todo',
34+
* selectId: (todo) => todo.key,
35+
* });
36+
*
37+
* export const TodosStore = signalStore(
38+
* // 👇 Adds `todoEntityMap`, `todoIds`, and `todoEntities` signals to the store.
39+
* withEntities(todoConfig),
40+
* withMethods((store) => ({
41+
* addTodo(todo: Todo): void {
42+
* patchState(store, addEntity(todo, todoConfig));
43+
* },
44+
* }))
45+
* );
46+
* ```
47+
*/
1748
export function entityConfig<Entity>(config: {
1849
entity: Entity;
1950
collection?: string;

modules/signals/entities/src/updaters/add-entities.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ export function addEntities<Entity>(
3131
entities: Entity[],
3232
config: { selectId: SelectEntityId<NoInfer<Entity>> }
3333
): PartialStateUpdater<EntityState<Entity>>;
34+
/**
35+
* @description
36+
*
37+
* Adds multiple entities to the collection.
38+
* Does not override existing entities with same IDs.
39+
*
40+
* @usageNotes
41+
*
42+
* ```ts
43+
* import { patchState } from '@ngrx/signals';
44+
* import { addEntities } from '@ngrx/signals/entities';
45+
*
46+
* patchState(store, addEntities([todo1, todo2]));
47+
* ```
48+
*/
3449
export function addEntities(
3550
entities: any[],
3651
config?: { collection?: string; selectId?: SelectEntityId<any> }

modules/signals/entities/src/updaters/add-entity.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ export function addEntity<Entity>(
3131
entity: Entity,
3232
config: { selectId: SelectEntityId<NoInfer<Entity>> }
3333
): PartialStateUpdater<EntityState<Entity>>;
34+
/**
35+
* @description
36+
*
37+
* Adds an entity to the collection.
38+
* Does not override if entity with same ID exists.
39+
*
40+
* @usageNotes
41+
*
42+
* ```ts
43+
* import { patchState } from '@ngrx/signals';
44+
* import { addEntity } from '@ngrx/signals/entities';
45+
*
46+
* patchState(store, addEntity(todo));
47+
* ```
48+
*/
3449
export function addEntity(
3550
entity: any,
3651
config?: { collection?: string; selectId?: SelectEntityId<any> }

modules/signals/entities/src/updaters/prepend-entities.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ export function prependEntities<Entity>(
3131
entities: Entity[],
3232
config: { selectId: SelectEntityId<NoInfer<Entity>> }
3333
): PartialStateUpdater<EntityState<Entity>>;
34+
/**
35+
* @description
36+
*
37+
* Adds multiple entities to the beginning of the collection.
38+
* Does not add existing entities with same IDs.
39+
*
40+
* @usageNotes
41+
*
42+
* ```ts
43+
* import { patchState } from '@ngrx/signals';
44+
* import { prependEntities } from '@ngrx/signals/entities';
45+
*
46+
* patchState(store, prependEntities([todo1, todo2]));
47+
* ```
48+
*/
3449
export function prependEntities(
3550
entities: any[],
3651
config?: { collection?: string; selectId?: SelectEntityId<any> }

modules/signals/entities/src/updaters/prepend-entity.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ export function prependEntity<Entity>(
3131
entity: Entity,
3232
config: { selectId: SelectEntityId<NoInfer<Entity>> }
3333
): PartialStateUpdater<EntityState<Entity>>;
34+
/**
35+
* @description
36+
*
37+
* Adds an entity to the beginning of the collection.
38+
* Does not add if entity with same ID exists.
39+
*
40+
* @usageNotes
41+
*
42+
* ```ts
43+
* import { patchState } from '@ngrx/signals';
44+
* import { prependEntity } from '@ngrx/signals/entities';
45+
*
46+
* patchState(store, prependEntity(todo));
47+
* ```
48+
*/
3449
export function prependEntity(
3550
entity: any,
3651
config?: { collection?: string; selectId?: SelectEntityId<any> }

modules/signals/entities/src/updaters/remove-all-entities.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ export function removeAllEntities(): PartialStateUpdater<EntityState<any>>;
66
export function removeAllEntities<Collection extends string>(config: {
77
collection: Collection;
88
}): PartialStateUpdater<NamedEntityState<any, Collection>>;
9+
/**
10+
* @description
11+
*
12+
* Removes all entities from the collection.
13+
*
14+
* @usageNotes
15+
*
16+
* ```ts
17+
* import { patchState } from '@ngrx/signals';
18+
* import { removeAllEntities } from '@ngrx/signals/entities';
19+
*
20+
* patchState(store, removeAllEntities());
21+
* ```
22+
*/
923
export function removeAllEntities(config?: {
1024
collection?: string;
1125
}): PartialStateUpdater<EntityState<any> | NamedEntityState<any, string>> {

modules/signals/entities/src/updaters/remove-entities.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ export function removeEntities<
3030
predicate: EntityPredicate<Entity>,
3131
config: { collection: Collection }
3232
): PartialStateUpdater<State>;
33+
/**
34+
* @description
35+
*
36+
* Removes multiple entities from the collection by IDs or predicate.
37+
*
38+
* @usageNotes
39+
*
40+
* ```ts
41+
* import { patchState } from '@ngrx/signals';
42+
* import { removeEntities } from '@ngrx/signals/entities';
43+
*
44+
* // Remove by IDs
45+
* patchState(store, removeEntities([1, 2, 3]));
46+
*
47+
* // Remove by predicate
48+
* patchState(store, removeEntities((todo) => todo.completed));
49+
* ```
50+
*/
3351
export function removeEntities(
3452
idsOrPredicate: EntityId[] | EntityPredicate<any>,
3553
config?: { collection?: string }

modules/signals/entities/src/updaters/remove-entity.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ export function removeEntity<Collection extends string>(
1414
id: EntityId,
1515
config: { collection: Collection }
1616
): PartialStateUpdater<NamedEntityState<any, Collection>>;
17+
/**
18+
* @description
19+
*
20+
* Removes an entity from the collection by ID.
21+
*
22+
* @usageNotes
23+
*
24+
* ```ts
25+
* import { patchState } from '@ngrx/signals';
26+
* import { removeEntity } from '@ngrx/signals/entities';
27+
*
28+
* patchState(store, removeEntity(1));
29+
* ```
30+
*/
1731
export function removeEntity(
1832
id: EntityId,
1933
config?: { collection?: string }

modules/signals/entities/src/updaters/set-all-entities.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ export function setAllEntities<Entity>(
2929
entities: Entity[],
3030
config: { selectId: SelectEntityId<NoInfer<Entity>> }
3131
): PartialStateUpdater<EntityState<Entity>>;
32+
/**
33+
* @description
34+
*
35+
* Replaces the entire entity collection with the provided entities.
36+
*
37+
* @usageNotes
38+
*
39+
* ```ts
40+
* import { patchState } from '@ngrx/signals';
41+
* import { setAllEntities } from '@ngrx/signals/entities';
42+
*
43+
* patchState(store, setAllEntities([todo1, todo2, todo3]));
44+
* ```
45+
*/
3246
export function setAllEntities(
3347
entities: any[],
3448
config?: { collection?: string; selectId?: SelectEntityId<any> }

modules/signals/entities/src/updaters/set-entities.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ export function setEntities<Entity>(
3131
entities: Entity[],
3232
config: { selectId: SelectEntityId<NoInfer<Entity>> }
3333
): PartialStateUpdater<EntityState<Entity>>;
34+
/**
35+
* @description
36+
*
37+
* Adds or replaces multiple entities in the collection.
38+
*
39+
* @usageNotes
40+
*
41+
* ```ts
42+
* import { patchState } from '@ngrx/signals';
43+
* import { setEntities } from '@ngrx/signals/entities';
44+
*
45+
* patchState(store, setEntities([todo1, todo2]));
46+
* ```
47+
*/
3448
export function setEntities(
3549
entities: any[],
3650
config?: { collection?: string; selectId?: SelectEntityId<any> }

0 commit comments

Comments
 (0)