I am using IndexedDB and I have two object stores: equip (represents different equipment, primary key tagNo) and equipParts (represents the parts of a piece of equipment and has an index which is based on the tag number/serial number, primary key seqNo, with a field tagNo which represents the equipment that part is part of).
If I delete a record in equip, I want to delete all records in equipParts bearing the tagNo of equip (just like "where equipParts.tagNo = equip.tagNo").
Excerpt from my 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 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();
The problem is that only the record in equip is deleted; the records in equipParts stay there. Is there a way to delete multiple records in an IndexedDB object store based on a non-unique index (which can be the primary key of the parent object store)?
You have to get primary keys to delete the records.
var pdestroy = tagIndex.openKeyCursor(IDBKeyRange.only(tagno));
pdestroy.onsuccess = function() {
var cursor = pdestroy.result;
if (cursor) {
pstore.delete(cursor.primaryKey);
cursor.continue();
}
}
Alternatively, but not efficient
var pdestroy = tagIndex.openCursor(IDBKeyRange.only(tagno));
pdestroy.onsuccess = function() {
var cursor = pdestroy.result;
if (cursor) {
cursor.delete();
cursor.continue();
}
}