Lab 9

Today we are going to look at IndexedDB and Google Drive API

But first, download this repo:

https://github.com/shovon/iat381-lab9.git

Databases

  • IndexedDB is a way to store structured data, in a reliable manner
  • why use IndexedDB?
    • same reason why iTunes, Web Browsers (for managing browsing history), instant messaging services (for caching chat history)
    • reliability, and a nice API and paradigm to query data
    • ACID
      • A for Atomicity: have all operations on data happen all at once, or non at all
      • C for Consistency: all operations must ensure that the resulting state is valid, otherwise, do nothing (such as maintain uniqueness)
      • I for Isolation: an incomplete transaction remains intact before any other transaction can occur (with the help of locks, for example)
      • D for Durability: when crap hits the fan with the underlying hardware, things stay intact
    • they all use SQLite, except Google Chrome, where they use Google's own LevelDB
      • IndexedDB works on top of the browser's database engine (SQLite on Firefox, LevelDB on Chrome)
  • Although, IndexedDB is the only Database API for the web, let's still ask, why IndexedDB over anything else?
    • One big advantage: database version management. An event is triggered the moment an application requests that the database's schema has changed, and you can act on that event to update the schema

sklad

  • We're going to use sklad. Working directly with IndexedDB is way too much work. sklad's API lays everything out really nicely.
      an elegant API for creating/opening a database, managing versions/performing necessary migrations, opening connection to the database, all in one single command
  • Although, sklad spoon feeds you a lot of stuff, one thing you are responsible for: maintaining the schema, and performing the necessary migrations
    • but fortunately, with sklad, there's fewer paperwork involved than just raw IndexedDB

sklad continued


var dbName = 'todo_list_store';
 
sklad.open(dbName, {
  version: 1,
  migration: {
    '1': function (database) {
      var objStore = database.createObjectStore('todos', {autoIncrement: true});
      objStore.createIndex('description_search', 'description', {unique: true});
    }
  }
}, function (err, conn) {
  if (err) { throw err; }
  $(function () {
    var $description = $('#description');
    var $add         = $('#add');
    var $list        = $('#list')
 
    function updateRows(conn) {
      conn
        .get({
          todos: {description: sklad.DESC, index: 'description_search'}
        }, function (err, data) {
          if (err) { return console.error(err); }
          
          // TODO: do stuff here.
        });
    }
 
    updateRows(conn);
 
    $add.click(function () {
      if (!$description.val().trim()) {
        return;
      }
      conn.insert({
        todos: [
          { description: $description.val() }
        ]
      }, function (err, insertedKeys) {
        $description.val('');
        if (err) { return console.error(err); }
        updateRows(conn);
      })
    });
  });
});