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 = ;
Add new records
_id
property will be generated and attached to each new record:
todoStore;todoStore;todoStore;
Update all records
todoStore;
Return all records
todoStoreall; // [{ id: 1, title "todo1" }, {id: 2, title: todo2 }]
Find records
- find based on given criteria
todoStore; // [{ id: 2, title: "todo2" }, { id: 3, title: "todo3" }]
- find based on given function
todoStore; // [{ id: 3, title: "todo3" }]
Return single record by id
todoStore; // { id: 1, title: "todo1" }
Destroy single record
- by record id
todoStore;
- by record object
todoStore;
Destroy all records
- destroy all
todoStore;
- destroy by given criteria
todoStore;
- destroy by given function
todoStore;
Options
You can pass a second parameter to depot.js with additional options.
var todoStore = ;
Available options:
- idAttribute - used to override record id property (default:
_id
)
var todoStore = ;
- storageAdaptor - used to override storage type (default:
localStorage
)
var todoStore = ;
Contributors:
License:
The MIT License