simple store creation utils

Usage no npm install needed!

<script type="module">
  import jsStore from 'https://cdn.skypack.dev/js-store';



Build Status

utilities for simple javascript stores


npm install --save js-store


Simple Store

var jsStore = require('js-store');

var myStore = jsStore.createStore({foo: 'bar'});
myStore.setState({baz: 'qux'});
myStore.getState(); // {foo: 'bar', baz: 'qux'});

var cb = function() {
  // pull data from store using getState

myStore.setState({other: 'value'}); // cb will fire
myStore.setState({other: 'another value'}); // cb will not fire

Store Collections

var jsStore = require('js-store');

var TeamsStore = jsStore.createCollectionStore('teams');

TeamsStore.getState(); // returns {teams: []}
TeamsStore.setState({teams: [
  {id: 1, name: 'a'},
  {id: 2, name: 'b'},
  {id: 3, name: 'c'}

TeamsStore.findById(2); // returns {id: 2, name: 'b'}

Store collections have a few helper methods to make it easier to work with the collection:

// returns the object with an id of 2, or undefined
// pass an optional second arg with a true to convert the first arg to an integer)
// where obj.id is a unique identifier, or obj is the value of an id
// returns the index of the object, or -1 if not found
// adds an item to the collection
// replaces existing item (by team.id)
// returns the updated item or undefined if the item.id doesn't match
// add or replaces existing item (by team.id)
// returns the new item
// replace the existing item that has the given id
// returns the id if successfully destroy, or undefined

Stores that sync with localStorage

var jsStore = require('js-store');

var TeamsStore = jsStore.createCollectionStore('teams', true);
// changes to TeamsStore's state will now write to localStorage
// TeamStore's state will also start with state from localstorage

Syncing with local storage is also available for simple stores. To do so pass along a string key to the createStore method.

var jsStore = require('js-store');

var myStore = jsStore.createStore({}, 'myStore');

To make singleton stores for your client side app, simply export a creaetd store from a module

var jsStore = require('js-store');

var UserStore = jsStore.createStore({}, 'user');
module.exports = UserStore;