Skip to content

Instantly share code, notes, and snippets.

@hliyan
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hliyan/bb3bf9b0dee9e48a99cb to your computer and use it in GitHub Desktop.
Save hliyan/bb3bf9b0dee9e48a99cb to your computer and use it in GitHub Desktop.
IndexedDB
// connectivity: ITERATION 1
var database = new Database('companydb');
database.connect(function(db) {
if (db.error) {
// handle error
}
// continue db ops
});
// connectivity: ITERATION 2
var database = new Database(configs);
database.connect(function(db) {...});
"configs: ITERATION 1":
{
"database": "companydb",
"version": 3,
"stores": {
"departments": {
"keyPath": "deptid",
"indexes": {
"departments-deptid": { "keyPath": "deptid", "unique": "true" }
}
},
"employees": {
"keyPath": "empid",
"indexes": {
"employees-empid": { "keyPath": "empid", "unique": "true" },
"employees-deptid": { "keyPath": "deptid" },
"employees-name": { "keyPath": "name" }
}
}
}
}
"configs: ITERATION 2":
{
"database": "companydb",
"versions": {
"1": {
"stores": {
"departments": {
"keyPath": "deptid",
"indexes": {
"departments-deptid": {
"keyPath": "deptid",
"unique": "true"
}
}
}
}
},
"2": {
"stores": {
"departments": {
...
},
"employees": {
...
}
}
}
}
}
"configs: ITERATION 3":
{
"database": "companydb",
"version": 3,
"stores": {
"departments": {
"keyPath": "deptid",
"indexes": {
"departments-deptid": { "keyPath": "deptid", "unique": "true" }
}
},
"employees": {
"keyPath": "empid",
"indexes": {
"employees-empid": { "keyPath": "empid", "unique": "true" },
"employees-deptid": { "keyPath": "deptid" },
"employees-name": { "keyPath": "name" }
}
}
},
"upgrades": {
"1": "schema/upgrades/version1.js",
"2": "schema/upgrades/version2.js",
"3": "schema/upgrades/version3.js"
}
}
// connectivity: ITERATION 3
var database = new Database(JSON.parse(dbconfigs)); // assuming we're loading it directly from file
database.connect(...);
// referece: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
var dbname = "companydb";
var dbversion = 1;
var db = null;
// open the database, do first time initializations if necessary
function openidb(callback) {
var request = window.indexedDB.open(dbname, dbversion);
request.onsuccess = function(e) {
console.log('database opened successfully');
db = request.result;
callback(); // all db ops have to happen after onsuccess
};
request.onerror = function(e) {
console.error('failed to open database: ' + e.target.error.message);
};
request.onupgradeneeded = function(e) {
console.info('previous version of database schema detected. upgrading...');
db = e.target.result;
var employees = db.createObjectStore('employees', { keyPath: 'empid' });
employees.createIndex('employee id', 'empid', { unique: true });
employees.createIndex('department', 'deptid');
employees.createIndex('full name', 'name');
var departments = db.createObjectStore('departments', { keyPath: 'deptid' });
departments.createIndex('department id', 'deptid', { unique: true });
departments.transaction.oncomplete = function(e) {
var tx = db.transaction(['departments'], 'readwrite');
tx.objectStore('departments').add({ deptid: 1, name: 'Accounting' });
tx.objectStore('departments').add({ deptid: 2, name: 'Engineering' });
console.info('...database schema upgrade done.');
};
};
}
// write some data
function testidbwrite(callback) {
console.log('writing data...');
var emp1 = { empid: 1, name: 'John Doe', deptid: 1 };
var emp2 = { empid: 2, name: 'Jeremy Doe', deptid: 2 };
var tx = db.transaction('employees', 'readwrite');
tx.oncomplete = function(e) {
console.log('...writing data complete');
callback();
};
tx.onerror = function(e) {
console.error('failed to write data: ' + e.target.error.message);
};
tx.objectStore('employees').add(emp1);
tx.objectStore('employees').add(emp2); // each returns a request to which
// you can attach onerror/onsuccess, but i'm only catching the full
// transaction level errors and completion
}
// read written data
function testidbread() {
console.log('reading data...');
var request = db.transaction('employees').objectStore('employees').get(1);
request.onsuccess = function(e) {
console.log('... and the data: ');
console.log(request.result);
};
request.onerror = function(e) {
console.error('failed to read data: ' + e.target.error.message);
};
}
// since this is a test, deleting any older versions of the database
// and starting froms scratch
var request = window.indexedDB.deleteDatabase(dbname);
request.onsuccess = test;
request.onerror = test;
function test() {
openidb(function() {
testidbwrite(function() {
testidbread();
});
});
}
// writing: ITERATION 1
database.connect(onconnect);
function onconnect(db) {
if (db.error) {
// handle
return;
}
var data = { empid: 3, name: 'William Tell', deptid: 1 };
var store = database.getStore('employees');
store.add(data, function(tx) {
if (tx.error) {
// handle
return;
}
});
}
// writing: ITERATION 2
var data = { empid: 3, name: 'William Tell', deptid: 1 };
database.add('employees', data, function(tx) {
if (tx.error) ...
});
// transactions: ITERATION 1
var tx = database.transaction();
for (var i in hires)
tx.add('employees', hires[i]);
for (var i in fires)
tx.remove('employees', fires[i].empid);
tx.execute(function(tx) {
if (tx.error) {
}
});
// transactions: ITERATION 2
database.transaction([
database.add('departments', marketing),
database.add('employees', mktVP),
database.add('employees', mktManager),
database.add('employees', mktExecutive),
]).execute(function(tx) {
if (tx.error) {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment