Last active
September 16, 2018 10:22
-
-
Save IamManchanda/1d25c23564a7c46e2eab389c53acdefe 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
import idb from 'idb'; | |
var dbPromise = idb.open('test-db', 4, function(upgradeDb) { | |
switch(upgradeDb.oldVersion) { | |
case 0: | |
var keyValStore = upgradeDb.createObjectStore('keyval'); | |
keyValStore.put("world", "hello"); | |
case 1: | |
upgradeDb.createObjectStore('people', { keyPath: 'name' }); | |
case 2: | |
var peopleStore = upgradeDb.transaction.objectStore('people'); | |
peopleStore.createIndex('animal', 'favoriteAnimal'); | |
case 3: | |
peopleStore = upgradeDb.transaction.objectStore('people'); | |
peopleStore.createIndex('age', 'age'); | |
} | |
}); | |
// read "hello" in "keyval" | |
dbPromise.then(function(db) { | |
var tx = db.transaction('keyval'); | |
var keyValStore = tx.objectStore('keyval'); | |
return keyValStore.get('hello'); | |
}).then(function(val) { | |
console.log('The value of "hello" is:', val); | |
}); | |
// set "foo" to be "bar" in "keyval" | |
dbPromise.then(function(db) { | |
var tx = db.transaction('keyval', 'readwrite'); | |
var keyValStore = tx.objectStore('keyval'); | |
keyValStore.put('bar', 'foo'); | |
return tx.complete; | |
}).then(function() { | |
console.log('Added foo:bar to keyval'); | |
}); | |
dbPromise.then(function(db) { | |
var tx = db.transaction('keyval', 'readwrite'); | |
var keyValStore = tx.objectStore('keyval'); | |
keyValStore.put('cat', 'favoriteAnimal'); | |
return tx.complete; | |
}).then(function() { | |
console.log('Added favoriteAnimal:cat to keyval'); | |
}); | |
// add people to "people" | |
dbPromise.then(function(db) { | |
var tx = db.transaction('people', 'readwrite'); | |
var peopleStore = tx.objectStore('people'); | |
peopleStore.put({ | |
name: 'Sam Munoz', | |
age: 25, | |
favoriteAnimal: 'dog' | |
}); | |
peopleStore.put({ | |
name: 'Susan Keller', | |
age: 34, | |
favoriteAnimal: 'cat' | |
}); | |
peopleStore.put({ | |
name: 'Lillie Wolfe', | |
age: 28, | |
favoriteAnimal: 'dog' | |
}); | |
peopleStore.put({ | |
name: 'Marc Stone', | |
age: 39, | |
favoriteAnimal: 'cat' | |
}); | |
return tx.complete; | |
}).then(function() { | |
console.log('People added'); | |
}); | |
// list all cat people | |
dbPromise.then(function(db) { | |
var tx = db.transaction('people'); | |
var peopleStore = tx.objectStore('people'); | |
var animalIndex = peopleStore.index('animal'); | |
return animalIndex.getAll('cat'); | |
}).then(function(people) { | |
console.log('Cat people:', people); | |
}); | |
// people by age | |
dbPromise.then(function(db) { | |
var tx = db.transaction('people'); | |
var peopleStore = tx.objectStore('people'); | |
var ageIndex = peopleStore.index('age'); | |
return ageIndex.getAll(); | |
}).then(function(people) { | |
console.log('People by age:', people); | |
}); | |
// Using cursors | |
dbPromise.then(function(db) { | |
var tx = db.transaction('people'); | |
var peopleStore = tx.objectStore('people'); | |
var ageIndex = peopleStore.index('age'); | |
return ageIndex.openCursor(); | |
}).then(function(cursor) { | |
if (!cursor) return; | |
return cursor.advance(2); | |
}).then(function logPerson(cursor) { | |
if (!cursor) return; | |
console.log("Cursored at:", cursor.value.name); | |
// I could also do things like: | |
// cursor.update(newValue) to change the value, or | |
// cursor.delete() to delete this entry | |
return cursor.continue().then(logPerson); | |
}).then(function() { | |
console.log('Done cursoring'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment