IDBOpenDBRequest: upgradeneeded イベント
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.
upgradeneeded
イベントは、現在のバージョンより高いバージョン番号でデータベースを開こうとしたとき発火します。
このイベントはキャンセルできず、バブリングしません。
構文
addEventListener()
のようなメソッドでイベント名を使うか、イベントハンドラープロパティを設定します。
js
addEventListener("upgradeneeded", (event) => {});
onupgradeneeded = (event) => {};
イベント型
IDBVersionChangeEvent
です。Event
を継承します。
イベントプロパティ
親の Event
インターフェイスからもプロパティを継承します。
IDBVersionChangeEvent.oldVersion
読取専用-
データベースの古いバージョンを返します。
IDBVersionChangeEvent.newVersion
読取専用-
データベースの新しいバージョンを返します。
例
この例ではデータベースを開き、upgradeneeded
イベントの処理としてオブジェクトストアに必要な更新を行います。
js
// データベースを開きます
const dBOpenRequest = window.indexedDB.open("toDoList", 4);
dBOpenRequest.addEventListener("upgradeneeded", (event) => {
const db = event.target.result;
console.log(`バージョン ${db.version} に更新します`);
// このデータベース用の objectStore を作ります
const objectStore = db.createObjectStore("toDoList", {
keyPath: "taskTitle",
});
// objectStore に保存するデータアイテムを定義します
objectStore.createIndex("hours", "hours", { unique: false });
objectStore.createIndex("minutes", "minutes", { unique: false });
objectStore.createIndex("day", "day", { unique: false });
objectStore.createIndex("month", "month", { unique: false });
objectStore.createIndex("year", "year", { unique: false });
});
これは同じ例ですが、onupgradeneeded
イベントハンドラープロパティを用います。
js
// データベースを開きます
const dBOpenRequest = window.indexedDB.open("toDoList", 4);
dBOpenRequest.onupgradeneeded = (event) => {
const db = event.target.result;
console.log(`バージョン ${db.version} に更新します`);
// このデータベース用の objectStore を作ります
const objectStore = db.createObjectStore("toDoList", {
keyPath: "taskTitle",
});
// objectStore に保存するデータアイテムを定義します
objectStore.createIndex("hours", "hours", { unique: false });
objectStore.createIndex("minutes", "minutes", { unique: false });
objectStore.createIndex("day", "day", { unique: false });
objectStore.createIndex("month", "month", { unique: false });
objectStore.createIndex("year", "year", { unique: false });
};
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
upgradeneeded event |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.