0
我想在onupgradeneeded上更新我的indexeddb數據庫的其中一個對象庫。我想檢查一下,如果這個索引存在於這個物品庫中,那麼就不需要做任何改變。但如果不是,我想更新它的索引。檢查indexedDB索引是否已經存在
我想在onupgradeneeded上更新我的indexeddb數據庫的其中一個對象庫。我想檢查一下,如果這個索引存在於這個物品庫中,那麼就不需要做任何改變。但如果不是,我想更新它的索引。檢查indexedDB索引是否已經存在
var request = indexedDB.open(...);
request.onupgradeneeded = function(event) {
// Get the IDBDatabase connection
var db = event.target.result;
// This is the implied IDBTransaction instance available when
// upgrading, it is type versionchange, and is similar to
// readwrite.
var tx = event.target.transaction;
// Get the store from the transaction. This assumes of course that
// you know the store exists, otherwise use
// db.objectStoreNames.contains to check first.
var store = tx.objectStore('myStore');
// Now check if the index exists
if(store.indexNames.contains('myIndex')) {
// The store contains an index with that name
console.log('The index myIndex exists on store myStore');
// You can also access the index and ensure it has the right
// properties you want. If you want to change the properties you
// will need to delete the index then recreate it.
var index = store.index('myIndex');
// For example
if(index.multiEntry) {
console.log('myIndex is flagged as a multi-entry index');
}
if(index.unique) {
console.log('myIndex is flagged with the unique constraint');
}
} else {
// The store does not contain an index with that name
console.log('The index myIndex does not exist on store myStore');
// Can create it here if you want
store.createIndex('myIndex', ...);
}
};
很好的答案,謝謝! – user6333296
「contains」的官方(或非官方)文檔在哪裏?我已經搜遍了所有可能的來源(MDN,w3.org等)和[這個問題](http://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-一個對象的JavaScript),但我似乎無法找到任何提及這種本地方法。我在哪裏可以閱讀有關「包含」? –
https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/indexNames和https://developer.mozilla.org/en-US/docs/Web/API/DOMStringList – Josh