indexing - Deleting multiple records in IndexedDB based on index -
i using indexeddb , have 2 object stores: equip (represents different equipment, primary key tagno) , equipparts (represents parts of piece of equipment , has index based on tag number/serial number, primary key seqno, field tagno represents equipment part part of).
if delete record in equip, want delete records in equipparts bearing tagno of equip (just "where equipparts.tagno = equip.tagno").
excerpt code:
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 records bearing 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();
the problem record in equip deleted; records in equipparts stay there. there way delete multiple records in indexeddb object store based on non-unique index (which can primary key of parent object store)?
you have primary keys delete records.
var pdestroy = tagindex.openkeycursor(idbkeyrange.only(tagno)); pdestroy.onsuccess = function() { var cursor = pdestroy.result; if (cursor) { pstore.delete(cursor.primarykey); cursor.continue; } }
alternatively, not efficient
var pdestroy = tagindex.opencursor(idbkeyrange.only(tagno)); pdestroy.onsuccess = function() { var cursor = pdestroy.result; if (cursor) { cursor.delete(); cursor.continue; } }
Comments
Post a Comment