Difference between revisions of "User:Abel/experimental.js"
Jump to navigation
Jump to search
m |
|||
Line 1: | Line 1: | ||
+ | console.log("~"); | ||
console.log("~"); | console.log("~"); | ||
Line 21: | Line 22: | ||
// Add some data | // Add some data | ||
− | var John = {id: 12345, name: {first: "John", last: "Doe"}, age: 42, phone: '+33123456789'}; | + | // var John = {id: 12345, name: {first: "John", last: "Doe"}, age: 42, phone: '+33123456789'}; |
− | var Bob = {id: 67890, name: {first: "Bob", last: "Smith"}, age: 35} | + | // var Bob = {id: 67890, name: {first: "Bob", last: "Smith"}, age: 35} |
− | store.put(John); | + | // store.put(John); |
− | store.put(Bob); | + | // store.put(Bob); |
Line 30: | Line 31: | ||
var getJohn = store.get(12345); | var getJohn = store.get(12345); | ||
var getBob = index.get(["Smith", "Bob"]); | var getBob = index.get(["Smith", "Bob"]); | ||
− | + | console.log(getJohn); | |
getJohn.onsuccess = function() { | getJohn.onsuccess = function() { | ||
− | console.log(getJohn | + | console.log(getJohn); // => "John" |
}; | }; | ||
getBob.onsuccess = function() { | getBob.onsuccess = function() { | ||
− | console.log(getBob | + | console.log(getBob); // => "Bob" |
}; | }; | ||
Revision as of 02:48, 25 April 2019
console.log("~");
console.log("~");
var pddb = window.indexedDB.open("pdio", 1);
pddb.addEventListener('error', (event) => {
console.log('Request error:', pddb.error);
}, false);
pddb.onupgradeneeded = function() {
var db = pddb.result;
var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
};
pddb.onsuccess = function() {
// Start a new transaction
var db = pddb.result;
var tx = db.transaction("MyObjectStore", "readwrite");
var store = tx.objectStore("MyObjectStore");
var index = store.index("NameIndex");
// Add some data
// var John = {id: 12345, name: {first: "John", last: "Doe"}, age: 42, phone: '+33123456789'};
// var Bob = {id: 67890, name: {first: "Bob", last: "Smith"}, age: 35}
// store.put(John);
// store.put(Bob);
// Query the data
var getJohn = store.get(12345);
var getBob = index.get(["Smith", "Bob"]);
console.log(getJohn);
getJohn.onsuccess = function() {
console.log(getJohn); // => "John"
};
getBob.onsuccess = function() {
console.log(getBob); // => "Bob"
};
// Close the db when the transaction is done
tx.oncomplete = function() {
db.close();
};
}