使用Javascript在IndexedDB中为预先存在的ObjectStore添加索引

Jak*_*rew 10 javascript indexeddb

我已经看到createIndex了在创建ObjectStore之后直接定义ObjectStore索引的多个JavaScript示例,如下所示:

var objectStore = ixDb.createObjectStore(osName, { keyPath: pkName, autoIncrement: autoIncrement });

objectStore.createIndex("name", "name", { unique: false }); 
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我如何createIndex在没有调用的情况下使用预先存在的表createObjectStore吗?我想这里真正的问题是如何在不使用的情况下获取对objectStore的引用createObjectStore

我尝试了以下几种变体而没有运气:

var objectStore = window.IDBTransaction.objectStore(ObjectStoreName);
var index = objectStore.createIndex(ixName, fieldName, { unique: unique, multiEntry: multiEntry });
Run Code Online (Sandbox Code Playgroud)

dum*_*ter 4

您可以在 期间执行此操作onupgradeneeded,这应该是您最初创建对象存储的同一位置。在那里,您可以执行升级所需的任何适当操作,例如创建新索引。这是一个例子。