Last active
January 23, 2018 15:25
-
-
Save hungryzi/5476449 to your computer and use it in GitHub Desktop.
IndexedDB talk
This file contains hidden or 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
var request = indexedDB.open('beer_czar'); | |
request.onsuccess = function(event){ | |
var db = event.target.result; | |
var transaction = db.transaction(['beer'], 'readwrite'); | |
transaction.oncomplete = function(event){ | |
// handle transaction errors here | |
} | |
var beerObjectStore = transaction.objectStore('beer'); | |
var operation = beerObjectStore.add(beer) | |
operation.onsuccess = function(event){ | |
// handle success here | |
} | |
operation.onerror = function(event){ | |
// handle error here | |
} | |
} |
This file contains hidden or 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
var request = indexedDB.open('beer_czar'); | |
request.onsuccess = function(event){ | |
var db = event.target.result; | |
var transaction = db.transaction(['beer'], 'readonly'); | |
var beerObjectStore = transaction.objectStore('beer'); | |
var results = []; | |
var operation; | |
var style_id = options.style_id; | |
var abv = options.abv; | |
var key = IDBKeyRange.bound([style_id, abv.lower], [style_id, abv.upper]); | |
operation = beerObjectStore.index('style_abv').openCursor(key); | |
operation.onerror = error; | |
operation.onsuccess = function(event) { | |
var cursor = event.target.result; | |
if (cursor) { | |
results.push(cursor.value); | |
cursor.continue(); | |
} else { | |
if (success) { | |
success(results); | |
console.debug('find', results); | |
} | |
} | |
}; | |
} |
This file contains hidden or 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
var request = indexedDB.open('beer_czar', 1); |
This file contains hidden or 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
var request = indexedDB.open('beer_czar', 1); | |
request.onupgradeneeded = function(event){ | |
var db = event.target.result; | |
db.createObjectStore('beer', { keyPath: 'id', autoIncrement: true }); | |
} | |
This file contains hidden or 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
var request = indexedDB.open('beer_czar'); | |
request.onsuccess = function(event){ | |
var db = event.target.result; | |
var transaction = db.transaction(['beer'], 'readonly'); | |
var beerObjectStore = transaction.objectStore('beer'); | |
var operation = beerObjectStore.get(id) | |
operation.onsuccess = success; | |
operation.onerror = error; | |
} |
This file contains hidden or 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
var request = indexedDB.open('beer_czar'); | |
request.onsuccess = function(event){ | |
var db = event.target.result; | |
var transaction = db.transaction(['beer'], 'readonly'); | |
var beerObjectStore = transaction.objectStore('beer'); | |
var results = []; | |
var operation; | |
var abv = options.abv; | |
var key = IDBKeyRange.bound(abv.lower, abv.upper); | |
operation = beerObjectStore.index('abv').openCursor(key); | |
operation.onerror = error; | |
operation.onsuccess = function(event) { | |
var cursor = event.target.result; | |
if (cursor) { | |
results.push(cursor.value); | |
cursor.continue(); | |
} else { | |
if (success) { | |
success(results); | |
} | |
} | |
}; | |
} |
This file contains hidden or 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
var request = indexedDB.open('beer_czar'); | |
request.onsuccess = function(event){ | |
var db = event.target.result; | |
var transaction = db.transaction(['beer'], 'readonly'); | |
var beerObjectStore = transaction.objectStore('beer'); | |
var results = []; | |
var operation; | |
var key = IDBKeyRange.only(options.style_id); | |
operation = beerObjectStore.index('style').openCursor(key); | |
operation.onerror = error; | |
operation.onsuccess = function(event) { | |
var cursor = event.target.result; | |
if (cursor) { | |
results.push(cursor.value); | |
cursor.continue(); | |
} else { | |
if (success) { | |
success(results); | |
console.debug('find', results); | |
} | |
} | |
}; | |
} |
This file contains hidden or 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
var request = indexedDB.open('beer_czar', 2); | |
request.onupgradeneeded = function(event){ | |
var db = event.target.result; | |
if (event.oldVersion < 1){ | |
db.createObjectStore('beer', { keyPath: 'id', autoIncrement: true }); | |
} | |
if (event.oldVersion < 2){ | |
var beerStore = event.target.transaction.objectStore('beer'); | |
beerStore.createIndex('style', 'style_id', { unique: false }); | |
} | |
} |
This file contains hidden or 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
var request = indexedDB.open('beer_czar'); | |
request.onsuccess = function(event){ | |
var db = event.target.result; | |
var transaction = db.transaction(['beer'], 'readonly'); | |
var beerObjectStore = transaction.objectStore('beer'); | |
var results = []; | |
var operation = beerObjectStore.openCursor(); | |
operation.onerror = error; | |
operation.onsuccess = function(event) { | |
var cursor = event.target.result; | |
if (cursor) { | |
results.push(cursor.value); | |
cursor.continue(); | |
} else { | |
if (success) { | |
success(results); | |
console.debug('find', results); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment