kganser.com

objectDB

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.

Demo

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.


    

Usage

Below are examples of objectDB's common uses. For detailed interface specs, see the documentation.

Opening the database

var db = objectDB.open('demo');

Supplying initial data

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.

Custom upgrade handler, database version, error handler

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.

Basic reading & writing

The database object returned by open immediately supports basic reads and writes to the default objectStore.

Read all objectStore data

db.get().then(function(data) {
  console.log(data);
});
{object: {key: 'value'}, array: ['elem1', 'elem2']}

Read data at a path

db.get('array').then(function(data) {
  console.log(data);
});
['elem1', 'elem2']

Read data at multiple paths

db.get('object').get('array').then(function(obj, arr) {
  console.log(obj, arr);
});
{key: 'value'}, ['elem1', 'elem2']

Write data

db.put('path', 'data').then(function(error) {
  console.error(error);
});

Reading with a cursor

Get only immediate elements

db.get('', false, 'immediates').then(function(data) {
  console.log(data);
});
{object: {}, array: []}

Filter elements

db.get('array', function(path, array) {
  return function(key) {
    return key < 1;
  };
}).then(function(data) {
  console.log(data);
});
['elem1']

Transactions

Write after read

db.get('number', true).then(function(num) {
  this.put('number', num+1); // transactional increment
});

Transactions on multiple objectStores

db.transaction(true, ['sessions', 'users'])
  .delete('sessions', 'kganser')
  .delete('users', 'kganser');

Compatibility

ObjectDB requires indexedDB support, including array keys. Check the compatibility table and feature tests for your browser.

Documentation