ObjectDB is a transactional database library that lets you treat tables as JSON structures of arbitrary size and depth. Elements in the JSON structure are addressable with URI-encoded paths, and written using simple put
and delete
calls, as well as append
and insert
for array elements.
ObjectDB is backed by indexedDB, the standard transactional noSQL database available in modern browsers. Its API makes indexedDB much easier to work with, but no less powerful.
The jsonv editor UI below allows you to modify the JSON structure stored in a database on your browser. To the right are the commands executed on your behalf.
Error: Your browser does not appear to support indexedDB. Data below will not be persisted. See Compatibility.
Below are examples of objectDB's common uses. For detailed interface specs, see the documentation.
var db = objectDB.open('demo');
var db = objectDB.open('demo', {object: {key: 'value'}, array: ['elem1', 'elem2']});
The JSON data specified in the second argument gets populated to any new objectStore involved in the first transaction on this database if an indexedDB upgrade event is triggered.
var db = objectDB.open('demo', function(upgrade) { upgrade.createObjectStore('newData', {key: 'value'}); }, 2, function(error, blocked) { if (blocked) alert('Database is already open in another window. Please close to continue.'); else console.error(error); });
If the second argument is a function, it should handle an upgrade event using the provided UpgradeTransaction
object. If the third (positive integer) argument is specified and greater than the current database version (1 at database creation), an upgrade event is triggered. An optional fourth argument is both the error handler and blocked operation handler for the database.
The database object returned by open
immediately supports basic reads and writes to the default objectStore.
db.get().then(function(data) { console.log(data); });
{object: {key: 'value'}, array: ['elem1', 'elem2']}
db.get('array').then(function(data) { console.log(data); });
['elem1', 'elem2']
db.get('object').get('array').then(function(obj, arr) { console.log(obj, arr); });
{key: 'value'}, ['elem1', 'elem2']
db.put('path', 'data').then(function(error) { console.error(error); });
db.get('', false, 'immediates').then(function(data) { console.log(data); });
{object: {}, array: []}
db.get('array', function(path, array) { return function(key) { return key < 1; }; }).then(function(data) { console.log(data); });
['elem1']
db.get('number', true).then(function(num) { this.put('number', num+1); // transactional increment });
db.transaction(true, ['sessions', 'users']) .delete('sessions', 'kganser') .delete('users', 'kganser');
ObjectDB requires indexedDB support, including array keys. Check the compatibility table and feature tests for your browser.