depot

[![build status](https://secure.travis-ci.org/mkuklis/depot.js.png)](http://travis-ci.org/mkuklis/depot.js)

npm install depot
1 downloads in the last week
7 downloads in the last month

depot.js

build status

depot.js

Description

depot.js is a namespaced localStorage wrapper with a simple API. There are other tools out there but none of them had what I was looking for.

Setup

depot.js should work well with CommonJS and AMD loaders. If loaders are not present depot.js will attach itself to the current context (window) when loaded via <script src="depot.min.js"></script>.

depot.js is also a bower component so you should be able to install it by running:

bower install depot

or if you already have a bower based project you can add depot.js to your dependency list in component.json

 "dependencies": {
    ...
    "depot": "0.x.x"
    ...
  }

Dependencies

depot.js does not depend on any other libraries however if you plan to support older browsers you will need to include ES5-shim.

If you plan to run it on browsers that don't support localStorage you may try to include storage polyfill.

API

  • save(record)

  • updateAll(hash)

  • update(hash)

  • find(hash | function)

  • all()

  • destroy(id | record)

  • destroyAll(none | hash | function)

  • get(id)

  • size()

Usage

Define new store

var todoStore = depot('todos');

Add new records

_id property will be generated and attached to each new record:

todoStore.save({ title: "todo1" });
todoStore.save({ title: "todo2", completed: true });
todoStore.save({ title: "todo3", completed: true });

Update all records

todoStore.updateAll({ completed: false });

Return all records

todoStore.all(); // [{ id: 1, title "todo1" }, {id: 2, title: todo2 }]

Find records

  • find based on given criteria
todoStore.find({ completed: true }); // [{ id: 2, title: "todo2" }, { id: 3, title: "todo3" }]
  • find based on given function
todoStore.find(function (record) {
  return record.completed && record.title == "todo3";
}); // [{ id: 3, title: "todo3" }]

Return single record by id

todoStore.get(1); // { id: 1, title: "todo1" }

Destroy single record

  • by record id
todoStore.destroy(1);
  • by record object
todoStore.destroy(todo);

Destroy all records

  • destroy all
todoStore.destroyAll();
  • destroy by given criteria
todoStore.destroyAll({ completed: true });
  • destroy by given function
todoStore.destroyAll(function (record) {
  return record.completed && record.title == "todo3";
});

Options

You can pass a second parameter to depot.js with additional options.

var todoStore = depot("todos", options);

Available options:

  • idAttribute - used to override record id property (default: _id)
var todoStore = depot("todos", { idAttribute: 'id' });
  • storageAdaptor - used to override storage type (default: localStorage)
var todoStore = depot('todos', { storageAdaptor: sessionStorage });

Contributors:

License:

The MIT License
npm loves you