IndexedDB之光標(biāo)查詢
Querying with Cursors
Transactions can be used directly to retrieve a single item with a known key. When you want to retrieve multiple items, you need to create a cursor within the transaction. A cursor is a pointer into a result set. Unlike traditional database queries, a cursor doesn’t gather all of the result set up front. Instead, a cursor points to the fi rst result and doesn’t try to find the next until instructed to do so.
翻譯:
事務(wù)能直接取出已知主鍵的單一條目。當(dāng)你想要取出多個(gè)條目是,你需要在事務(wù)里面創(chuàng)建一個(gè)光標(biāo)。一個(gè)光標(biāo)就是結(jié)果集中的一個(gè)指針。
Cursors are created using the openCursor() method on an object store. As with other operations with IndexedDB, the return value of openCursor() is a request, so you must assign onsuccess and onerror event handlers. For example:
翻譯:
使用對象存儲上的openCursor()來創(chuàng)建光標(biāo)。和IndexDB上的其他操作一樣,方法openCursor()的返回值是一個(gè)請求,所以你必須實(shí)現(xiàn)onsuccess和onerror時(shí)間。如下:
var store = db.transaction(“users”).objectStore(“users”), request = store.openCursor();
request.onsuccess = function(event){
//handle success };
request.onfailure = function(event){
//handle failure };
When the onsuccess event handler is called, the next item in the object store is accessible via event.target.result, which holds an instance of IDBCursor when there is a next item or null when there are no further items. The IDBCursor instance has several properties:
翻譯:
當(dāng)成功事件被調(diào)用的時(shí)候,通過event.target.result你可以訪問對象存儲中的條目。如果還有下一條,event.target.result返回IDBCursor的實(shí)例;如果沒有下一條,則返回null;IDBCursor實(shí)例有一些屬性:
direction — A numeric value indicating the direction the cursor should travel in. The default is IDBCursor.NEXT (0) for next. Other values include IDBCursor.NEXT_ NO_DUPLICATE (1) for next without duplicates, IDBCursor.PREV (2) for previous, and IDBCursor.PREV_NO_DUPLICATE (3) for previous without duplicates.
key — The key for the object.
value — The actual object.
primaryKey — The key being used by the cursor. Could be the object key or an index key (discussed later).
翻譯:
direction — 一個(gè)數(shù)字,指示光標(biāo)行進(jìn)的方向。默認(rèn)值是IDBCursor.NEXT (0),向前進(jìn)。其他的值包含IDBCursor.NEXT_ NO_DUPLICATE (1) 向前進(jìn)排除重復(fù)值。IDBCursor.PREV (2),向后退;IDBCursor.PREV_NO_DUPLICATE (3)向后退排除重復(fù)的。
key — 對象的主鍵。
value — 對象本身。’
primaryKey — 光標(biāo)使用的主鍵。可以是對象的主鍵或者一個(gè)索引鍵(稍后討論)。
You can retrieve information about a single result using the following:
翻譯:
使用如下代碼,你能獲得一個(gè)單一結(jié)果的信息:
request.onsuccess = function(event){
var cursor = event.target.result;
if (cursor){ //always check
console.log(“Key: “ + cursor.key + “, Value: “ + JSON.stringify(cursor.value));
} };
Keep in mind that cursor.value in this example is an object, which is why it is JSON encoded before being displayed.
翻譯:
注意此示例中cursor.value是一個(gè)對象,所以顯示前需要JSON編碼。
A cursor can be used to update an individual record. The update() method updates the current cursor value with the specified object. As with other such operations, the call to update() creates a new request, so you need to assign onsuccess and onerror if you want to know the result:
翻譯:
一個(gè)光標(biāo)能被用來更新某條記錄。 方法update()更新指定對象的光標(biāo)值。和其他操作一樣,方法update()的調(diào)用創(chuàng)建了一個(gè)新的請求,因此如果你想知道結(jié)果,你需要給onsuccess和onerror事件柄賦值:
request.onsuccess = function(event){
var cursor = event.target.result, value, updateRequest;
if (cursor){ //always check
if (cursor.key == “foo”){
value = cursor.value; //get current value
value.password = “magic!”; //update the password
updateRequest = cursor.update(value); //request the update be saved
updateRequest.onsuccess = function(){ //handle success; };
updateRequest.onfailure = function(){ //handle failure }; } } };
You can also delete the item at that position by calling delete(). As with update(), this also creates a request:
翻譯:
通過調(diào)用方法delete(),你也能刪除那個(gè)位置的條目。和update()一樣,這樣也會創(chuàng)建一個(gè)請求:
request.onsuccess = function(event){
var cursor = event.target.result, value, deleteRequest;
if (cursor){ //always check
if (cursor.key == “foo”){
deleteRequest = cursor.delete(); //request the value be deleted
deleteRequest.onsuccess = function(){ //handle success; };
deleteRequest.onfailure = function(){ //handle failure }; } } };
Both update() and delete() will throw errors if the transaction doesn’t have permission to modify the object store.
翻譯:
假如事務(wù)沒有權(quán)限修改對象,udpate()和delete()都將拋出異常。
Each cursor makes only one request by default. To make another request, you must call one of the following methods:
每個(gè)光標(biāo)默認(rèn)只產(chǎn)生一次請求。為了產(chǎn)生其他的請求,你必須調(diào)用下列方法之一:
continue(key) — Moves to the next item in the result set. The argument key is optional. When not specified, the cursor just moves to the next item; when provided, the cursor will move to the specified key.
advance(count) — Moves the cursor ahead by count number of items.
翻譯:
continue(key) — 移動到結(jié)果集的下個(gè)條目。參數(shù)可選。如果不指定,光標(biāo)就移往下個(gè)條目;如果提供,光標(biāo)則移往指定主鍵。
advance(count) — 往前移動光標(biāo)跳過指定數(shù)量的條目。
Each of these methods causes the cursor to reuse the same request, so the same onsuccess and onfailure event handlers are reused until no longer needed. For example, the following iterates over all items in an object store:
上述方法都使得光標(biāo)重用相同的請求。因此,相同的onsuccess和onfailure事件被重用直到不再需要。例如,下面代碼遍歷了對象存儲中的所有條目。
request.onsuccess = function(event){
var cursor = event.target.result;
if (cursor){ //always check
console.log(“Key: “ + cursor.key + “, Value: “ + JSON.stringify(cursor.value));
cursor.continue(); //go to the next one
} else {
console.log(“Done!”); } };
The call to continue() triggers another request and onsuccess is called again. When there are no more items to iterate over, onsuccess is called one last time with event.target.result equal to null.
翻譯:
continue()的調(diào)用觸發(fā)了另一個(gè)請求,然后onsuccess被再次調(diào)用。當(dāng)遍歷結(jié)束之后,onsuccess被最后一次調(diào)用,此時(shí),event.target.result為空。