Giter Club home page Giter Club logo

storage's Introduction

Storage Build Status

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:

Installation

$ bower install storage
$ component install ask11/storage
$ npm install ask11-storage --save

Standalone build available as storage.js.

<script src="storage.js"></script>
<script>window.storage('key', fn);</script>

Example

// set
storage('key', 'val', function(err) {});
storage({ key: 'foo', key2: 'val2'}, function(err) {});

// get
storage('key', function(err, val) {});
storage(['key', 'key2'], function(err, all) {}); // all.length == 2

// delete
storage('key', null, function(err) {});
storage(['key1', 'key2', 'key3'], null, function(err) {});

API

storage(key, fn)

Get key value.

storage([key1, key2, ..., keyn], fn)

Get group of values. Callbacks return array of values for each key. If key does not exist, it returns undefined for this position.

storage(key, val, fn)

Set key to val. You can store any kind of data, including blobs.

storage(key, null, fn)

Delete key. Null semantic is inspired by yields/store and component/cookie. Setting a key to null is equivalent to deleting the key via storage.del(key).

storage({ key1: val1, key2: val2, key3: val3 }, fn)

Run a batch operation. Simple way to create, update, remove multiple records.

// assume we have 2 records
storage('foo', 7, fn)
storage('bar', ['one', 'two', 'three'], fn);

storage({
  baz: 'val' // create new val
  foo: 1000, // update `foo` value
  bar: null, // remove `bar`
}, function(err) {});

storage([key1, key2, ..., keyn], null, fn)

Delete a group of keys in one request.

storage.forage

It gives you access to the localForage instance. You can use it to configure backend.

storage.forage.config({ name: 'my-name' });
if (!window.indexedDB) storage.forage.setDriver('localStorageWrapper');

storage.clear

Clear storage.

storage.count

Get the number of records in storage.

.get .set .del

If you prefer more explicit API, you can use exposed functions.

storage.set('key', 'val', function(err) {});
storage.get('key', function(err, val) {}); // val
storage.del('key', function(err) {});

Promises

If you don't like callbacks, you can combine storage with then/promise.

var Promise = require('promise');
var orignal = require('storage');
var storage = Promise.denodeify(orignal);

storage('key').then(function(val) {});

License

Aleksey Kulikov, MIT.

storage's People

Contributors

alekseykulikov avatar adrianoferrari avatar

Watchers

Alvar Laigna avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.