Exploring indexedDB

I’m playing with indexedDB and will post a series of articles on what I find. In this first one, let’s look at indexedDB’s structure.

Overview of indexedDB


IndexedDB stores your data in a database. Databases are protected: an application from one website cannot access a database from another’s. How does the browser differentiate between them? If all elements of the application’s origin match, access is granted; otherwise it is denied. The origin is the combination of protocol, hostname and port, for example:


is a different origin from


and both are different from either of these:



A database has a name and a version number. The name must be unique within that origin. The version number enables you to enhance your application to use an updated data model, and to roll this new update out across existing clients that have been using an older model, thus upgrading their database when they next use your application.

Object Store

A database contains at least one object store. An object store holds records as key-value pairs. Keys, which are unique within an object store, can be arrays, strings, dates or numbers, and can be auto-generated.

The object store is organised by key in ascending order: numbers first, then dates, then strings, and finally arrays.

Values can be any JavaScript object.

Asynchronous Programming Model

We’ll focus on IndexedDB’s asynchronous API. Here’s an analogy: imagine that you’ve taken your car to be repaired. Your car goes into a queue, and the receptionist gives you a pre-pay mobile phone that she says she will call you back on when your car is ready or if there is a problem. Further, she also tells you that the phone comes with an app that you can instruct yourself: you can instruct it to do this or that depending on whether the car was fixed (‘onsuccess’) or not (‘onerror’). If there was a problem, you can find out what the problem was.

The IndexedDB Asynchronous API works like this:

IndexedDB's Asynchronous API

  1. You call IndexedDB; every call returns immediately, without blocking the caller’s thread.
  2. Your call returns a request object (the mobile phone).
  3. You set up event handlers (‘onsuccess’ and ‘onerror’) on the request object.
  4. Once information is available, IndexedDB will fire events on the request object, resulting in calls to your handlers.

That’s all for now. More to come shortly.



Simple SQL date insertions using bash

Here’s a quick solution for populating a database with a series of records having increasing date values. For example, 2012-04-04, 2012-05-05, and so on. This is so I can prime a database with upcoming data that changes daily:

mysql> select * from mytable;
| date       | ref  |
| 2012-04-05 |    1 |
| 2012-04-06 |    2 |
| 2012-04-07 |    3 |
| 2012-04-08 |    4 |
| 2012-04-09 |    5 |
| 2012-04-10 |    6 |
| 2012-04-11 |    7 |
7 rows in set (0.00 sec)


I could have used a language like Perl, Python or Groovy, but in this case I chose a simple shell script.

The basic idea: call the ‘mysql’ command with a sequence of INSERT
statements. I’ve simplified this example to use just two columns – a date and an id. The inserting-the-data part is easy:

for id in 1 2 3 4 5 6 7
echo INSERT INTO blah \(date,ref\) VALUES \(...\);
done | mysql -u dbuser ...

The ‘for’ loop generates SQL statements on its standard output, which ‘mysql’ then executes line-by-line as it reads from the pipe.

The Date Calculations

To increment the date values, we can use a couple of features of the standard ‘date’ command. First, the format:

$ date +%Y-%m-%d
$ start=$(date +%Y-%m-%d)
$ echo $start

So that gets us nicely into ISO 8601 format, MySQL’s default. Now for adding days:

$ date --date "now + 1 day" +%Y-%m-%d

So our shell script becomes:

for id in 1 2 3 4 5 6 7
  newdate=$(date --date "now + $id days" +%Y-%m-%d)
  echo INSERT INTO mytable \(date,ref\) VALUES \(\"$newdate\",$id\)\;
done | mysql -u dbuser -p -h mydb

Bash Arithmetic

In my case I actually wanted to subtract days, so I could create a rolling ‘history’ of records, with the dates starting two days from today. (Also in my case, the ids were foreign keys.)

mysql> select * from mytable;
| date       | ref  |
| 2012-04-06 |   18 |
| 2012-04-05 |  351 |
| 2012-04-04 |  296 |
| 2012-04-03 |  550 |
| 2012-04-02 |  328 |
| 2012-04-01 |   19 |
| 2012-03-31 |   17 |
| 2012-03-30 |  549 |
| 2012-03-29 |  142 |
| 2012-03-28 |  551 |
10 rows in set (0.00 sec)


This can be done using bash’s Arithmetic Expansion construct, $((…)). I introduced a loop counter, i, incremented each time round the loop. The $((-i+2)) construct subtracts 2 from i and uses this value in the date argument:


for id in 18 351 296 550 328 19 17 549 142 551
  newdate=$(date --date "now + $((-i+2)) days" +%Y-%m-%d)
  echo INSERT INTO mytable \(date,ref\) VALUES \(\"$newdate\",$id\)\;
  let i++
done | mysql -u dbuser -p -h mydb

That’s it. Let me know if you found it helpful.