indexeddb-js
Welcome to the indexeddb-js
javascript/node-js module: a
pure-javascript stop-gap implementation of the IndexedDB (aka. Indexed
Database) API. Being "stop-gap", it currently does not implement all
IndexedDB API features and can only use
sqlite3 as a data storage
back-end.
Just To Be Clear
This module is not intended to provide a "production" level implementation: it is a "stop-gap" measure while we (the community) wait for a more robust/native implementation for node-js. As such, it was created to allow unit-testing of other projects (such as syncml-js and others), which require that you provide access to an indexedDB API, but need to be unit-tested in any environment, including non-browser environments.
If time permits, and/or others are willing to contribute, it may one day graduate to a less "stop-gap" measure.
What Isn't Implemented
There are many IndexedDB API items that are not implemented, which include but are not limited to:
-
True transaction support, i.e. transaction isolation, aborting (rollback), and transaction events (oncomplete).
-
Proper meta-information upgrade management.
-
Many of the dynamic public API properties of various object types.
-
Compliance with any of the DOMException errors.
-
Many non-critical APIs, including:
- indexedDB.deleteDatabase()
- indexedDB.cmp()
- Transaction.abort()
- Store.deleteIndex()
- Cursor.update()
- Cursor.advance()
- Cursor.delete()
-
Performance and efficiency (it was implemented one Saturday afternoon).
-
Non-sqlite3 data stores.
Installation
This is the easy part, provided you have npm
installed:
npm install indexeddb-js sqlite3
Usage
A quick example of how to use indexeddb-js
:
// assuming modules 'sqlite3' and 'indexeddb-js' have been loaded// in your environment-specific way, e.g. with `define` or `require`.// of course, if you are being nice to the community, you would wrap the// following in a call to "define()" and would share your code as a// non-environment-specific javascript module. see// http://manuel.kiessling.net/2012/03/30/true-universal-javascript-modules-with-write-once-run-anywhere-jasmine-specs/// for details... ;-)var engine = ':memory:';var scope = indexeddbjs;var request = scopeindexedDB;var db = null;request {console;};request {db = eventtargetresult;var store = db;store;store;};request {db = eventtargetresult;request;};request {// register an error handler for any error on the current dbdb {console;};// fetch the record with id "1" in store "data"var store = db;store {var obj = eventtargetresult;console;// now delete itstore {console;// and now add a couple new records (overwriting it if the key// already exists) with the same 'value' (so we can play with cursors)store {store {console;// we're getting pretty deeply nested here... let's pop out// and use the index;};};};};var {var index = dbindex'value';var range = scopeIDBKeyRange;console;index {var cursor = eventtargetresult;if ! cursorreturn;console;cursor;};};};
The output on the console from the above script should be:
record: {"id":1,"value":"my-first-item"}
deleted the record
added two more records
all objects with the "value" field set to "another object":
- {"id":"2","value":"another object"}
- {"id":3,"value":"another object"}
Note that indexeddb-js
implements the Indexed Database API as
accurately as possible, so just
google for the specification and
many good tutorials. My favorite:
Tests
indexeddb-js
uses jasmine for
the testing infrastructure; in the indexeddb-js directory:
npm install jasmine-node
make tests