Created
May 27, 2010 01:27
-
-
Save hallettj/415315 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* From the W3C working draft | |
* http://www.w3.org/TR/IndexedDB | |
*/ | |
// synchronously set properties on a database and retrieve a record | |
var db = indexedDB.open('books', 'Book store', false); | |
if (db.version !== '1.0') { | |
var olddb = indexedDB.open('books', 'Book store'); | |
olddb.createObjectStore('books', 'isbn'); | |
olddb.createIndex('BookAuthor', 'books', 'author', false); | |
olddb.setVersion("1.0"); | |
} | |
// db.version === "1.0"; | |
var index = db.openIndex('BookAuthor'); | |
var matching = index.get('fred'); | |
if (matching) | |
report(matching.isbn, matching.name, matching.author); | |
else | |
report(null); | |
// asynchronously set properties on a database and retrieve a record | |
function findFred() { | |
db.request.onsuccess = function() { | |
var index = db.request.result; | |
index.request.onsuccess = function() { | |
var matching = index.request.result; | |
if (matching) | |
report(matching.isbn, matching.name, matching.author); | |
else | |
report(null); | |
} | |
index.get('fred'); | |
} | |
db.openIndex('BookAuthor'); | |
} | |
indexedDB.request.onsuccess = function() { | |
var db = indexedDB.request.result; | |
if (db.version !== '1.0') { | |
indexedDB.request.onsuccess = function() { | |
var olddb = indexedDB.request.result; | |
olddb.request.onsuccess = function() { | |
olddb.request.onsuccess = function() { | |
olddb.request.onsuccess = function() { | |
findFred(db); | |
} | |
olddb.setVersion("1.0"); | |
} | |
olddb.createIndex('BookAuthor', 'books', 'author', false); | |
} | |
olddb.createObjectStore('books', 'isbn'); | |
} | |
indexedDB.open('books', 'Book store'); | |
} | |
findFred(db); | |
}; | |
indexedDB.open('books', 'Book store', false); | |
/* Working with Object Stores */ | |
var db = indexedDB.open('AddressBook', 'Address Book'); | |
if (db.version !== '1') { | |
var olddb = indexedDB.open('AddressBook', 'Address Book'); | |
olddb.createObjectStore('Contact', 'id', true); | |
olddb.setVersion('1'); | |
} | |
// storing a record | |
var store = db.openObjectStore('Contact'); | |
var lincoln = {name: 'Lincoln', number: '7012'}; | |
var contact = store.put(lincoln); | |
// contact.id === 1 | |
// retrieving a record | |
var contact = store.<>get(1); | |
// contact.name === 'Lincoln' | |
// updating a record using an in-line key | |
var abraham = {id: 1, name: 'Abraham', number: '2107'}; | |
store.put(abraham); | |
// iterating through records using a key range and a cursor | |
var range = new KeyRange.bound(2, 4); | |
var cursor = store.openCursor(range); | |
// each value is a contact and each key is the id for that | |
// contact whose id is between 2 and 4, both inclusive | |
cursor.continue(); | |
/* Working with Indexes */ | |
// creating an index | |
var db = indexedDB.open('AddressBook', 'Address Book'); | |
if (db.version === '1') { | |
var olddb = indexedDB.open('AddressBook', 'Address Book'); | |
olddb.createObjectStore('Contact', 'id', true); | |
olddb.createIndex('ContactName', 'Contact', 'name', false); | |
olddb.setVersion('2'); | |
} | |
// retrieving a record using an index | |
var index = db.openIndex('ContactName'); | |
var id = index.get('Lincoln'); | |
// id === 1 | |
// iterating through records in an index is similar to | |
// iterating through records in an object store | |
var range = new KeyRange.bound('L', 'M'); | |
var cursor = index.openCursor(range); | |
// each value is a contact and each key is the name for that | |
// contact whose name's first letter is either L or M | |
cursor.continue(); | |
/* Walking through records with a Cursor */ | |
var objects = ... | |
var cursor = objects.openCursor(); | |
// act on each object and continue the cursor to its end | |
cursor.continue(); | |
// iterating through records in reverse order | |
var objects = ... | |
var count = objects.openCursor(Cursor.PREV); | |
// act on each object and continue the cursor to its end | |
cursor.continue(); | |
// single bound, and skipping duplicates | |
var objects = ... | |
var range = KeyRange.leftBound(key); | |
objects.openCursor(range, Cursor.NEXT_NO_DUPLICATE); | |
// act on each object and continue the cursor to its end | |
cursor.continue(); | |
// left and right bounds | |
var objects = ... | |
var range = KeyRange.bound(start, end); | |
objects.openCursor(range); | |
// act on each object and continue the cursor to its end | |
cursor.continue(); | |
// working with an object from a cursor | |
console.log('current object', cursor.key, cursor.value); | |
console.log('there are '+ cursor.count +' objects with the same key'); | |
cursor.value += 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment