Re: Help converting a WebSQL example to IndexDB

On Tue, Jun 21, 2011 at 2:52 PM, Ian Hickson <ian@hixie.ch> wrote:
>
> Could one of the IndexDB people help me convert this example to use the
> IndexDB stuff instead of the WebSQL stuff?
>
>   http://www.whatwg.org/demos/workers/database-updater/worker.js

Assuming the 'key' is intended to be a unique identifier into the
pairs table, something like this:

var database;
indexeddb.open('demobase').onsuccess = function(e) {
  database = e.result;
  var server = new WebSocket('ws://whatwg.org/database');
  server.onmessage = websocketHandler;
}
function websocketHandler(event) {
  // data is in the format "command key value"
  var data = event.data.split(' ');
  switch (data[0]) {
    case '+':
     database.transaction(["pairs"], IDBTransaction.READ_WRITE)
       .objectStore("pairs").put(data[2], data[1]);
    case '-':
     database.transaction(["pairs"], IDBTransaction.READ_WRITE)
       .objectStore("pairs").delete(data[1]);
  }
};

A couple of things to note: Opening a database is an asynchronous
operation since it makes metadata about the database available. All
tables must have a unique key for each row. In this example I assumed
that the 'key' part was that unique identifier.

/ Jonas

Received on Wednesday, 22 June 2011 03:20:24 UTC