Behavior Subject Entitites is a helper class designed to be used in place of a standard BehaviorSubject(). It includes all CRUD functions to make dealing with adding, removing, and deleting easier.
-
Install the package
npm install behavior-subject-entities --save
- Import it in a TS file
import { EntityClass } from 'behavior-subject-entities';
- Option 1: Extend it on a Class
export class TestService extends EntityClass<T>
- Options 2: add it as a variable to a class
subject = new EntityClass<T>();
EntityClass accepts a few options
| Key | Required | Type | Default Value | Description |
|---|---|---|---|---|
| key | false | IdSelector | (instance: any) => instance.id | Id selector for the Entity. Pass a new function if you do not use id |
| name | false | string | Item | The Single Name for the Entity |
| plural | false | string | Items | The Plural Name for the Entity |
| Method | Parameters | Return |
|---|---|---|
| data$ | N/A | Observable<EntityObjContainer<T>> |
| items$ | N/A | Observable<T[]> |
| activeId$ | N/A | Observable<string> |
| addOne | item: T | void |
| addMany | arr: T[] | void |
| getOne | id: string | Observable<T> |
| getMany | ids: string[] | Observable<T[]> |
| updateOne | item: T | void |
| updateMany | arr: T | void |
| removeOne | id: string | void |
| removeMany | ids: string[] | void |
| snapshot | N/A | EntitySnapshot<T> |
| setActiveId | id: string | void |