/simple_localstorage_api

Simple LocalStorage API

Primary LanguageJavaScriptMIT LicenseMIT

Simple LocalStorage API

Better API to for storage data in localStorage, sessionStorage or Memory

Support for an expire date

Support for data collection

Installation

npm install --save simple_localstorage_api

API

import { Store } from 'simple_localstorage_api';
const store = new Store(); // default is window.localStorage

store.set('key', {data,...})

###Use store as session Storage

const store = new Store({type: 'session'}) // use window.sessionStorage

###Use store as Memory Storage

const store = new Store({type: 'memory'})

###Insert a record by key

const store = new Store()
store.insert('mykey', {id:1,name:'test'});

###Insert a record with an expire date

const store = new Store()

store.insert('mykey', {id:1, name:'test', expireAfter: 2}) // Exipre in 2 Miniutes 

store.insert('mykey', {id:1,name:'test', expireAfter: '2.minutes'}); // Expire in 2 miniutes
store.insert('mykey', {id:1,name:'test', expireAfter: '2.hours'}); // Expire in 2 hours
/*
supported key base on moment.js = 
years
quarters
months
weeks
days
hours
minutes
seconds	
milliseconds
*/

###Find a record by key

store.find('mykey') ;
//return json object

###Insert a collection by key

const store = new Store();
store.set('mycollection', {items: [{id:1}, {id:2}], expireAfter: 5});

###Add item into a collection

const store = new Store();
store.addItem('mycollection', {id: 3});

###Find item from a collection

const store = new Store();
store.findItem('mycollection', item => item.id === 1);

###Update item into a collection

const store = new Store();
store.updateItem('mycollection', {name: 'test'}, item => item.id === 1);

###Remove item form a collection

const store = new Store();
store.removeItem('mycollection', item => item.id === 1);

###Remove a record by key

store.clear('mykey');

###Remove all the records from store

store.clear();

License

MIT, see LICENSE.md for more information.