IndexedDB之索引(Indexes)
Indexes
For some data sets, you may want to specify more than one key for an object store. For example, if you’re tracking users by both a user ID and a username, you may want to access records using either piece of data. To do so, you would likely consider the user ID as the primary key and create an index on the username.
翻譯:
對于有些數據集來說,你可能想要指定不止一個的鍵值。例如,假如你想即通過用戶ID又通過用戶名來定位用戶,你想要訪問任意的這兩塊數據。為了這樣做,你可能會考慮用戶ID作為主鍵然后再用戶名上創建一個索引。
To create a new index, first retrieve a reference to the object store and then call createIndex(), as in this example:
翻譯:
為了創建一個新的索引,首先引用對象存儲,然后調用方法createIndex(),如下例:
var store = db.transaction(“users”).objectStore(“users”), index = store.createIndex(“username”, “username”, { unique: true });
The first argument to createIndex() is the name of the index, the second is the name of the property to index, and third is an options object containing the key unique. This option should always be specified so as to indicate whether or not the key is unique across all records. Since username may not be duplicated, this index is not unique.
翻譯:
方法createIndex()的第一個參數是索引名,第二個是相對索引的屬性名,第三個是一個選項,限制鍵值的唯一性。這個選項應該總是指定,以便指明所有記錄里面鍵值是否唯一。既然用戶名可能不是復制的,那么索引也不是唯一的。
The returned value from createIndex() is an instance of IDBIndex. You can also retrieve the same instance via the index() method on an object store. For example, to use an already existing index named “username”, the code would be:
翻譯:
方法createIndex()返回是IDBIndex的實例。對象存儲上的方法index()也能取出相同的實例。例如,使用已有的名為"username"的索引,代碼如下:
var store = db.transaction(“users”).objectStore(“users”), index = store.index(“username”);
An index acts a lot like an object store. You can create a new cursor on the index using the openCursor() method, which works exactly the same as openCursor() on an object store except that the result.key property is filled in with the index key instead of the primary key. Here’s an example:
翻譯:
一個索引更像一個對象存儲。你能使用方法openCursor()在索引上創建一個新的光標。該光標和在對象存儲上通過方法openCursor()創建的光標類似,除了一點,結果中的鍵值屬性是索引的鍵值而不是主鍵。如下例:
var store = db.transaction(“users”).objectStore(“users”),
index = store.index(“username”), request = index.openCursor();
request.onsuccess = function(event){
//handle success };
An index can also create a special cursor that returns just the primary key for each record using the openKeyCursor() method, which accepts the same arguments as openCursor(). The big difference is that event.result.key is the index key and event.result.value is the primary key instead of the entire record.
翻譯:
一個索引通過方法openKeyCursor()也能創建返回主鍵的光標,它接受和openCursor()一樣的參數。最大的不同是event.result.key是索引鍵值而event.result.value是主鍵而不是整個記錄。
var store = db.transaction(“users”).objectStore(“users”),
index = store.index(“username”), request = index.openKeyCursor();
request.onsuccess = function(event){
//handle success
//event.result.key is the index key, event.result.value is the primary key };
You can also retrieve a single value from an index by using get() and passing in the index key, which creates a new request:
翻譯:通過方法get()然后傳入索引鍵值,你也能從一個索引中取出一個單一值。他也創建了一個請求:
var store = db.transaction(“users”).objectStore(“users”), index = store.index(“username”), request = index.get(“007”);
request.onsuccess = function(event){ //handle success };
request.onfailure = function(event){ //handle failure };
To retrieve just the primary key for a given index key, use the getKey() method. This also creates a new request but result.value is equal to the primary key value rather than the entire record:
翻譯:
為了取出一個已有索引鍵值的主鍵,可以使用方法getKey()。該方法也創建了一個新的請求,但result.value等于主鍵而不是整個記錄。
var store = db.transaction(“users”).objectStore(“users”), index = store.index(“username”), request = index.getKey(“007”);
request.onsuccess = function(event){
//handle success
//event.result.key is the index key, event.result.value is the primary key };
In the onsuccess event handler in this example, event.result.value would be the user ID.
翻譯:
該示例中的onsuccess事件柄,event.result.value將是用戶的ID。
At any point in time, you can retrieve information about the index by using properties on the IDBIndex object:
翻譯:
在任何時間點,通過IDBIndex對象的屬性,你能獲得索引的信息:
name — The name of the index.
keyPath — The property path that was passed into createIndex().
objectStore — The object store that this index works on.
unique — A Boolean indicating if the index key is unique.
翻譯:
name — 索引名。
keyPath — 被傳入createIndex()的屬性路徑。
objectStore — 索引隸屬于的對象存儲。
unique — 標識索引鍵值是否唯一的布爾值。
The object store itself also tracks the indexes by name in the indexNames property. This makes it easy to figure out which indexes already exist on an object using the following code:
翻譯:
對象存儲本身通過屬性indexNames中的名字也能定位索引。這使得通過下列代碼找到對象中已經存在哪些索引變得簡單:
var store = db.transaction(“users”).objectStore(“users”),
indexNames = store.indexNames, index, i = 0, len = indexNames.length;
while(i < len){
index = store.index(indexNames[i++]);
console.log(“Index name: “ + index.name + “, KeyPath: “ + index.keyPath + “, Unique: “ + index.unique); }
This code iterates over each index and outputs its information to the console.
翻譯:
上述代碼遍歷了所有索引,并且將索引信息輸出到控制臺。
An index can be deleted by calling the deleteIndex() method on an object store and passing in the name of the index:
翻譯:
通過調用對象存儲上的方法deleteIndex()并傳入索引名,可以刪除一個索引。
var store = db.transaction(“users”).objectStore(“users”);
store.deleteIndex(“username”);
Since deleting an index doesn’t touch the data in the object store, the operation happens without any callbacks.
翻譯:
因為刪除索引不影響對象存儲的數據,該操作沒有任何回調。