reactive-entity-store
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

Reactive Entity Store

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.

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>();

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));
// logs [] immediately, starts with an empty store
 
// 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[]>

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'}]

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

twitter github

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.

Package Sidebar

Install

npm i reactive-entity-store

Weekly Downloads

0

Version

0.3.2

License

MIT

Unpacked Size

136 kB

Total Files

36

Last publish

Collaborators

  • ngfelixl