async-storage
v0.1.0
Published
A Jetpack module for using IndexedDB, based on the localForage API
Downloads
1,557
Maintainers
Readme
Async Storage for Jetpack
This is a module for Mozilla's Addon-SDK that simplifies using IndexedDB in Firefox extensions, based on the localForage library.
Installation
The Add-on SDK supports loading modules from a node_modules directory via the jpm tool, so if you're using jpm all you need to do is this:
npm install --save jp-async-storage
If you're using cfx instead ( upgrade! upgrade! ) you can just to this:
cd <extension folder>/lib
wget https://raw.githubusercontent.com/canuckistani/jp-async-storage/master/lib/async-storage.js
Usage
let { AsyncStorage } = require("async-storage");
let config = {
name: 'my-database',
version: 1
};
AsyncStorage.open(config, function(e, r) {
if (e) throw e;
let item = {_id: 1, string: "Hello world"};
AsyncStorage.setItem('key-'+item._id, item, function() {
if (e) throw e;
// if you got this far, you probably saved data!
});
}):
Supported API
The localForage api is supported:
- setItem
- getItem
- removeItem
- getItems
- keys
- key
- length
- clear
There are two main differences:
- Promises are not supported, only callbacks
- callback arguments are node-style: the first argument is an error, and the second is hopefully your data.
Tests
First install jpm
with npm install jpm -g
, then run jpm test -b nightly -v
with Firefox Nightly installed.
TODO
- look into using Promises
- consider using a Promise-based initialization method like localForage does with a bit more magic.