Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting multiple records in IndexedDB based on index

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)?

like image 483
user2727708 Avatar asked Sep 04 '13 01:09

user2727708


People also ask

How do I delete a record in IndexedDB?

First, a request to delete an object is created using the delete(key) method, and finally the events are handled as required.

What is keyPath in IndexedDB?

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.

What is IDBKeyRange?

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.


2 Answers

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();
  }
}
like image 75
Kyaw Tun Avatar answered Oct 28 '22 12:10

Kyaw Tun


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();
        }
    })
like image 22
Ehsan Chavoshi Avatar answered Oct 28 '22 12:10

Ehsan Chavoshi