A Pen by Juan Cortines on CodePen.
Created
September 3, 2015 07:37
-
-
Save ncortines/bc27b6a13ec5c1f8b476 to your computer and use it in GitHub Desktop.
pjvgJB
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
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; | |
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; | |
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; | |
if (!window.indexedDB) { | |
window.alert('Your browser doesn\'t support a stable version of IndexedDB'); | |
} | |
function log(text) { | |
var div = document.createElement('div'); | |
div.textContent = text; | |
document.body.appendChild(div); | |
} | |
function addData(objectStore) { | |
objectStore.put({ name: 'Paul', id: 45 }); | |
objectStore.put({ name: 'Paul', id: 56 }); | |
objectStore.put({ name: 'Paul', id: 43 }); | |
objectStore.put({ name: 'Paul', id: 36 }); | |
objectStore.put({ name: 'Paul', id: 10 }); | |
objectStore.put({ name: 'Paul', id: 93 }); | |
objectStore.put({ name: 'John', id: 78 }); | |
objectStore.put({ name: 'John', id: 77 }); | |
objectStore.put({ name: 'John', id: 75 }); | |
objectStore.put({ name: 'John', id: 98 }); | |
objectStore.put({ name: 'John', id: 88 }); | |
objectStore.put({ name: 'John', id: 13 }); | |
objectStore.put({ name: 'John', id: 99 }); | |
objectStore.put({ name: 'Patrick', id: 34 }); | |
objectStore.put({ name: 'Patrick', id: 23 }); | |
objectStore.put({ name: 'Patrick', id: 12 }); | |
objectStore.put({ name: 'Patrick', id: 87 }); | |
objectStore.put({ name: 'Patrick', id: 02 }); | |
} | |
var request = window.indexedDB.open('Test', 1); | |
request.onsuccess = function(event) { | |
var db = event.target.result, | |
objectStore = db.transaction('customers').objectStore('customers'), | |
cursor = objectStore.index('nameId').openCursor(window.IDBKeyRange.bound(['John'], ['John' + '0'], true, false)); | |
cursor.onsuccess = function (event) { | |
var cursor = event.target.result; | |
if (cursor) { | |
log(JSON.stringify(cursor.value)); | |
cursor.continue(); | |
} | |
}; | |
}; | |
request.onupgradeneeded = function (event) { | |
var db = event.target.result, | |
objectStore = db.createObjectStore('customers', { keyPath: 'id' }); | |
objectStore.createIndex('nameId', ['name','id'], { unique: true }); | |
objectStore.transaction.oncomplete = function (event) { | |
var transaction = db.transaction('customers', 'readwrite'), | |
objectStore = transaction.objectStore('customers'); | |
addData(objectStore); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment