Storage is a functional wrapper around localForage. That means it's an asynchronous browser storage with multiple back-ends (IndexedDB, WebSQL, localStorage), which is built for a better offline experience.
The main differences with localForage:
- batch get/set support
- callbacks or promises
- browserify friendly
- simple API inspired by yields/store
- development mode
$ npm install asyncstorage --save
$ bower install storage
$ component install alekseykulikov/storage
Standalone build available as ./dist/storage.js.
<script src="storage.js"></script>
<script>window.storage('key', fn);</script>
// set
storage({ key: 'val', key2: 'val2'}, function(err) {});
// get
storage('key', function(err, val) {});
storage(['key', 'key2'], function(err, all) {}); // all.length == 2
// count
storage(function(err, count) {}); // count == 2
// delete
storage('key', null, function(err) {});
storage(['key', 'key2'], null, function(err) {});
Each method returns promise, and accepts optional callback.
Main function is facade to get/set/del/count methods. It's inspired by yields/store.
Setting a key to null
is equivalent to deleting the key via storage.del(key)
.
Get key
value.
Get group of values. Callbacks return array of values for each key.
If key does not exist, it returns null
on this position.
Set key
to val
.
You can store any kind of data, including blobs.
Run a batch operation.
Simple way to create, update, remove multiple records.
Use null
to remove record.
// assume we have 2 records
storage.set('foo', 7, fn)
storage.set('bar', ['one', 'two', 'three'], fn);
storage.set({
baz: 'val' // create new val
foo: 1000, // update `foo` value
bar: null, // remove `bar`
}, function(err) {});
Delete key
.
Delete a group of keys in one request.
Clear storage.
Count records.
Work with async code console can be unpleasant.
Setup development flag and storage will console.log() results of get
or count
.
storage.development = true;
storage.set({ foo: 1, bar: 2 });
storage.get(['foo', 'bar']);
// => [1 ,2]
storage.del('bar');
storage.count();
// => 1
// shortcut to: storage.count().then(console.log.bind(console));
It gives you access to the localForage instance.
You can use it to configure backend or for advanced methods as keys
or iterate
.
storage.forage.config({ name: 'my-name' });
if (!window.indexedDB) storage.forage.setDriver(storage.forage.LOCALSTORAGE);
storage.forage.keys().then(function(keys) {
console.log(keys);
});