 |
ทำไมเวลาจะแก้ไข้หรือจัดการดาต้าเบส Indexed DB ต้องประกาศเป็นเวอร์ชั่นใหม่ถึงจะจัดการดาต้าเบสได้ |
|
 |
|
|
 |
 |
|
const request = indexedDB.open("DbName" , VersionNO);
ตั้งแต่ใช้ทำดาต้าเบสมาหลายๆแบบ ไม่มีรูปแบบไหนต้องใส่เลขเวอร์ชั่นเลย แล้วต้องใส่เลขให้มากกว่าเวอร์ชั่นเก่าเพื่ออะไร แล้วจะรู้ได้ไงว่าถึงเวอร์ชั่นไหนแล้ว ถ้าเป็นดาต้าเบสรูปแบบอื่นแค่ใส่ path หรือ ชื่อ แล้วออพชั่นอื่นอย่างพวก read and write, password ก็เปิดได้แล้ว
Code (Java)
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.
};
Tag : HTML, HTML5, JavaScript
|
|
 |
 |
 |
 |
Date :
2021-10-21 16:41:36 |
By :
nk4ever |
View :
954 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|