Socket
Socket
Sign inDemoInstall

@ngxs-labs/entity-state

Package Overview
Dependencies
5
Maintainers
8
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngxs-labs/entity-state


Version published
Maintainers
8
Weekly downloads
315
increased by2.61%

Weekly downloads

Readme

Source


Easy CRUD actions for your ngxs state

Build Status NPM License

This package is an entity adapter and simplifies CRUD behaviour with just a few lines of setup per state class!

Setup

npm i @ngxs-labs/entity-state

You do not have import any module, just extend your state class, make a super call and you are good to go! The first super parameter is always the state class itself. The second parameter is the key to identify your entities with. The third is an implementation of an IdGenerator (see below).

Example state
export interface ToDo {
  title: string;
  description: string;
  done: boolean;
}

@State<EntityStateModel<ToDo>>({
  name: 'todo',
  defaults: defaultEntityState()
})
export class TodoState extends EntityState<ToDo> {
  constructor() {
    super(TodoState, 'title', IdStrategy.EntityIdGenerator);
  }
}

Example in the integration app!

Actions

There are ready to use Actions for entity-states. Just pass in the targeted state class as the first parameter and then your action's payload.

this.store.dispatch(new SetLoading(TodoState, this.loading));
this.store.dispatch(new UpdateActive(TodoState, { done: true }));

Example in the integration app

ActionShort description
AddAdds a new entity and cannot replace existing entities
CreateOrReplaceGets the entity's ID and will replace entities with same id or else add a new one
UpdateUpdates one or more entities by partial value or function
UpdateAllUpdate all entities by partial value or function
RemoveRemoves entities from the state
RemoveAllRemoves all entities from the state
--------------------
SetActiveTakes an ID and sets it as active
ClearActiveClears the active ID
UpdateActiveUpdates the currently active entity
RemoveActiveRemoves the active entity and clears the ID
--------------------
SetErrorSets the error (Error instance or undefined)
SetLoadingSets the loading state (true or false)
ResetResets the state to default
--------------------
GoToPageGoes to specified page, via index, stepwise or first/last
SetPageSizeSets the page size

Actions that change the entities will update the internal timestamp lastUpdated. You can use one of the existing selectors to see the age of your data.

Selectors

Use predefined Selectors just like you would normally!

@Select(TodoState.entities) toDos$: Observable<ToDo[]>;
@Select(TodoState.active) active$: Observable<ToDo>;

Example in the integration app

SelectorShort description
entitiesAll entities in an array
keysAll entity keys in an array
entitiesMapAll entities in a map
sizeEntity count
active the active entity
activeIdthe active ID
paginatedEntitiesEntities in an array defined by pagination values
nthEntitythe nthEntity by insertion order
latestIdthe ID of the latest entity
latestthe latest entity
loadingthe loading state
errorthe current error
lastUpdatedthe lastUpdated timestamp as Date
agedifference between Date.now() and lastUpdated in ms

IdStrategy

There are 3 different strategies in the IdStrategy namespace available:

  • IncrementingIdGenerator -> uses auto-incremeting IDs based on present entities
  • UUIDGenerator -> generates UUIDs for new entities
  • EntityIdGenerator -> takes the id from the provided entity

The latter will cause errors if you try to add an entity with the same ID. The former two will always generate a new ID.

You can also implement your own strategy by extending IdGenerator and then provide it in the super call.

EntitySelector

The EntitySelector type is used in Actions such as Update or Remove.

export type EntitySelector<T> = string | string[] | ((entity: T) => boolean);
  • string -> one ID; selects one entity
  • string[] -> array of IDs; selects matching entities
  • (entity: T) => boolean -> predicate; selects entities that return true when applied to this function

Keywords

FAQs

Last updated on 21 Sep 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc