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)?
First, a request to delete an object is created using the delete(key) method, and finally the events are handled as required.
In short, the indexName is how we want to the "field" or "column" (index) will be named in our "table" (Object Store) and the keyPath is the property of the stored object that contains the value of the field.
only() The only() method of the IDBKeyRange interface creates a new key range containing a single value. Note: This feature is available in Web Workers.
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();
}
}
I deleted multiple records belonging to an index by this way using idb :
var tx = idb.transaction("MyObjectStore", 'readwrite');
var index = tx.store.index('my_relid_idx');
var pdestroy = index.openCursor(RelID);
pdestroy.then(async cursor => {
while (cursor) {
cursor.delete();
cursor = await cursor.continue();
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With