const request = indexedDB.open("library", 3); // Request version 3. let db; request.onupgradeneeded = function(event) { const db = request.result; if (event.oldVersion < 1) { // Version 1 is the first version of the database. const store = db.createObjectStore("books", {keyPath: "isbn"}); const titleIndex = store.createIndex("by_title", "title", {unique: true}); const authorIndex = store.createIndex("by_author", "author"); } if (event.oldVersion < 2) { // Version 2 introduces a new index of books by year. const bookStore = request.transaction.objectStore("books"); const yearIndex = bookStore.createIndex("by_year", "year"); } if (event.oldVersion < 3) { // Version 3 introduces a new object store for magazines with two indexes. const magazines = db.createObjectStore("magazines"); const publisherIndex = magazines.createIndex("by_publisher", "publisher"); const frequencyIndex = magazines.createIndex("by_frequency", "frequency"); } }; request.onsuccess = function() { db = request.result; // db.version will be 3. };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter