Reactive Entity Store

Reactive Entity Store

Published on: 4/17/2019

Build Status Coverage Status NPM npm bundle size
This is a side product of nodeplotlib, but it is completely independent. It's a lightweight reactive entity store providing utilities for all commonly used crud operations, without setting up anything except the interface. Test it yourself with this typescript stackblitz demo.

Installation

npm install reactive-entity-store

Usage

At first you have to create an empty entity store. Let's use the name books in this demonstration. At first we create a file called books-store.ts which will contain the following
import { Store } from 'reactive-entity-store'; export interface Book { id?: string; title: string; author: string; } export const books = new Store<Book>();
Listing: Setup the store
It does not matter how you name your files at all, this is just for demonstration purposes. Lets create a reader of the store, lets name it service.ts.
import { books } from './pathto/books-store'; books.getAll().subscribe(books => console.log(books)); // there are several other "getters" books.getOne('id1'); // Observable<Book> books.getOneDynamic(of('id1')); // Observable<Book> books.getEntities(); // Observable<{[id: string]: Book}> books.getIds(); // Observable<string[]>
Listing: Get items from the store
In another file called controller.ts we are going to play around with some of the add and remove logic. The comments are printed due to the console.log in the previous file.
import { books } from './pathto/books-store'; books.add({ id: 'tcc', title: 'Clean Coder', author: 'Bob' }); // [{id: 'tcc', title: 'Clean Coder', author: 'Bob'}] books.add({ id: 'ng', title: 'Angular', author: 'Rob' }); // [{id: 'ng', title: 'Angular', author: 'Rob'}, {id: 'tcc', title: 'Clean Coder', author: 'Bob'}] books.remove('tcc'); // [{id: 'ng', title: 'Angular', author: 'Rob'}] books.update({id: 'ng', title: 'React'}); // [{id: 'ng', title: 'React', author: 'Rob'}] books.add({title: 'Vue', author: 'Evan'}); // [{id: 'someUniqueRandomString', title: 'Vue', author: 'Evan'}, {id: 'ng', title: 'React', author: 'Rob}] books.removeAll(); // [] books.addAll([ { id: 'ng', title: 'Angular', author: 'Rob' }, { id: 'tcc', title: 'Clean Coder', author: 'Bob' } ]); // [{id: 'ng', title: 'Angular', author: 'Rob'}, {id: 'tcc', title: 'Clean Coder', author: 'Bob'}]
Listing: Store interactions
If you don't provide an id property it will autogenerate a string of 20 characters [0-9a-zA-Z] as the id.