18

私は IndexedDB を使用しています。私は 2 つのオブジェクトストアを持っます 、そのパーツが含まれる機器を表すフィールドtagNoを持つ)。

Equipmentのレコードを削除する場合、 equipPartstagNoを含むすべてのレコードを削除したいと思います(「 whereequipParts.tagNo =equip.tagNo」のように)。

私のコードからの抜粋:

var tx = db.transaction(["equip", "equipParts"],"readwrite");
var estore = tx.objectStore("equip");
var pstore = tx.objectStore("equipParts");
var tagIndex = pstore.index("by_tagNo");
var pdestroy = tagIndex.openCursor(IDBKeyRange.only(tagno)); //opens all records bearing the selected tag number
pdestroy.onsuccess = function() {
    var cursor = pdestroy.result;
    if (cursor) {
        if (cursor.value.tagNo == tagno) {
            pstore.delete(cursor.value.seqNo); //I guess I'm wrong here
        }
        cursor.continue;
    }
}
pdestroy.onerror = function() {
    alert("Deletion attempt NG");
}
var ereq = estore.delete(tagno);
ereq.onsuccess = function(e) {
    alert("Form deletion OK");
    window.location = "index.html";
}
ereq.onerror = function(e) {
    alert("Form deletion NG");
    window.location = "index.html";
}
db.close();

問題は、equipのレコードのみが削除されることです。equipPartsのレコードはそこに残ります。一意ではないインデックス (親オブジェクト ストアの主キーになる可能性があります) に基づいて、IndexedDB オブジェクト ストア内の複数のレコードを削除する方法はありますか?

4

3 に答える 3

19

レコードを削除するには、主キーを取得する必要があります。

var pdestroy = tagIndex.openKeyCursor(IDBKeyRange.only(tagno)); 
pdestroy.onsuccess = function() {
  var cursor = pdestroy.result;
  if (cursor) {
      pstore.delete(cursor.primaryKey);
      cursor.continue();
  }
}

代わりに、しかし効率的ではありません

var pdestroy = tagIndex.openCursor(IDBKeyRange.only(tagno)); 
pdestroy.onsuccess = function() {
  var cursor = pdestroy.result;
  if (cursor) {
      cursor.delete();
      cursor.continue();
  }
}
于 2013-09-05T01:51:25.843 に答える