Reactive Entity Store
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.
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
; ;
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.
; books.getAll.subscribeconsole.logbooks;// logs [] immediately, starts with an empty store // there are several other "getters"books.getOne'id1'; // Observable<Book>books.getOneDynamicof'id1'; // Observable<Book>books.getEntities; // Observable<{[id: string]: Book}>books.getIds; // Observable<string[]>
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.
; books.add;// [{id: 'tcc', title: 'Clean Coder', author: 'Bob'}]books.add;// [{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', author: 'Rob'}]books.add;// [{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'}]
If you don't provide an id
property it will autogenerate a string of 20 characters [0-9a-zA-Z] as the id.
Get in touch
Hi, I am Felix, Software developer and NgRX contributor. If you have questions, don't hesitate to reach out.
If you like this library, think about giving it a star or follow me on twitter or github or check out my personal the website.