W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2011

Re: Help converting a WebSQL example to IndexDB

From: Jonas Sicking <jonas@sicking.cc>
Date: Tue, 21 Jun 2011 20:19:26 -0700
Message-ID: <BANLkTikqSY527RO0teQZF9ea=1sTJ+5ONw@mail.gmail.com>
To: Ian Hickson <ian@hixie.ch>
Cc: public-webapps@w3.org
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 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:45 GMT