featherstore
v2.0.0
Published
Zero-config, 600B, cross-browser storage for JavaScript apps
Downloads
3
Readme
featherstore
Zero-config, 600B, cross-browser storage for JavaScript apps
Installation
npm i featherstore --save
or with yarn
yarn add featherstore
Usage
Creating a store a fairly easy -
const store = featherstore();
Options
You can pass in few options to featherstore
(as an object) to create the store
namespace Just so that you don't pollute the global namespace, the
namespace
you provide is prefixed to all thekeys
you use to save the data to the store. Defaults to__FEATHERSTORE__
.ttl (time to live) That's the time in seconds for which the value will be stored. It defaults to one day from the time you save the value. You can pass any value that you like.
storage The storage that'll be used to save the store data. Defaults to
window.localStorage
. You can pass in'session'
to usewindow.sessionStorage
. Else, you can supply your own storage, considering that has the same API surface aswindow.localStorage
const store = featherstore({
namespace: '__APPNAME__',
ttl: 60 * 60 * 24 * 1000 * 365, // One year
storage: 'session'
});
Sample
// create store
const store = featherstore();
// set a value
store.set('key', 'value');
// set a value with a custom timeout (other than default ttl) by passing ttl as third argument
store.set('key_3_sec', 'value for 3 secs', 3000);
// get a stored value
store.get('key'); // value
// check if the store has a value with some `key`
store.has('key'); // true
store.has('non_existing_key'); // false
// get the list of keys in store
store.keys(); // ['key']
// remove a value with some `key`
store.clear('key');
// remove all values from store i.e clear the store
store.clear();
memstore
You can use an in-memory store (in case you would not like to use localStorage
or sessionStorage
). memstore helps you do that with just 200B of additional weight.
License
Copyright © MIT