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.
};
Optional. The version to open the database with. If the version is not provided and the database exists, then a connection to the database will be opened without changing its version. If the version is not provided and the database does not exist, then it will be created with version 1.