Skip to content
Advertisement

How do I change an object’s value in an IndexedDB index?

Is it possible to update an object’s value within an IndexedDB index without cloning, deleting, or putting a new entry? Theoretically something like the following snippet would do the trick, though it probably would not delete until the put was confirmed. But it looks like overkill to me. It looks like it would be a nightmare to do any error handling on.

const objectStore = db.transaction([objectStoreName], 'readwrite')
  .objectStore(objectStoreName);

const requestGet = objectStore.get(index);
requestGet.onsuccess = (event: any) => {
  const value = event.target.result.value // Store old value
  const requestDelete = objectStore.delete(index);
  requestDelete.onsuccess = (event: any) => {
    const requestPut = objectStore
      .put({index: 'New Index Value', value: value}); // Put back using new index         
  };
};

Advertisement

Answer

You cannot directly change values in an object store’s index. You can change the values of an object in an object store, and IndexedDB will propagate your changes to related indices. Indices are essentially read-only.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement