Code Monkey home page Code Monkey logo

indexeddb-js's Introduction

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.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    = new sqlite3.Database(':memory:');
var scope     = indexeddbjs.makeScope('sqlite3', engine);
var request   = scope.indexedDB.open('MyDatabase');
var db        = null;

request.onerror = function(event) {
  console.log('ERROR: could not open database: ' + event.target.error);
};

request.onupgradeneeded = function(event) {
  db = event.target.result;
  var store = db.createObjectStore('data', {keyPath: 'id'});
  store.createIndex('value', 'value', {unique: false});
  store.add({id: 1, value: 'my-first-item'});
};

request.onsuccess = function(event) {
  db = event.target.result;
  request.run();
};

request.run = function() {

  // register an error handler for any error on the current db
  db.onerror = function(event) {
    console.log('DATABASE ERROR: ' + event.target.error);
  };

  // fetch the record with id "1" in store "data"
  var store = db.transaction(null, 'readwrite').objectStore('data');
  store.get('1').onsuccess = function(event) {
    var obj = event.target.result;
    console.log('record: ' + JSON.stringify(obj));

    // now delete it
    store.delete('1').onsuccess = function(event) {
      console.log('deleted the record');

      // 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.put({id: '2', value: 'another object'}).onsuccess = function(event) {
        store.put({id: 3, value: 'another object'}).onsuccess = function(event) {
          console.log('added two more records');

          // we're getting pretty deeply nested here... let's pop out
          // and use the index
          play_with_the_index_and_cursors();

        };
      };
    };
  };

  var play_with_the_index_and_cursors = function() {

    var index = db.transaction(null, 'readwrite').objectStore('data').index('value');
    var range = scope.IDBKeyRange.only('another object');

    console.log('all objects with the "value" field set to "another object":');

    index.openCursor(range).onsuccess = function(event) {
      var cursor = event.target.result;
      if ( ! cursor )
        return;
      console.log('  - ' + JSON.stringify(cursor.value));
      cursor.continue();
    };

  };

};

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

indexeddb-js's People

Contributors

metagriffin avatar olafura avatar brettz9 avatar nilclass avatar

Watchers

James Cloos avatar masaki.kusuhata avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.