Eldon Lesley
Eldon Lesley

Reputation: 935

Updating/changing keys for objects in indexedDB

I have a Web-App that is using JavaScript and IndexedDB, I use it to store a blob/uint(in Chrome), with the indexed-key(unique), I can easily update the blob by just implementing:

var blob1=this.request.result; //the blob file(e.g. image)
var blob2=null;
var FileStorage = [
  { id: "10012", filedata:blob1 },
  { id: "10013", filedata:blob2 }
];
var objectStore = transaction.objectStore("storage");
for (var i in FileStorage) {
  var request = objectStore.put(FileStorage[1]);
  request.onsuccess = function(event) {
    console.log("success");
  };
}

I implemented .put(), instead of .add() to update a record, in this case i updated the filedata with the id of 10013,

But now, I'm facing problem to RENAME the id of existing record, for example, I want to change the first record(id 10012) to become id:10019 without changing/modifying the filedata at all, but I had a hard time to find out how. Again, the id is unique

Upvotes: 1

Views: 2440

Answers (2)

Steve Jorgensen
Steve Jorgensen

Reputation: 12371

Technically, you can't change the ID value of an existing record, but if you read the record, delete it, and then re-write it with the desired ID, then the result is the same as if you had changed the ID.

Upvotes: 3

Kyaw Tun
Kyaw Tun

Reputation: 13151

Indexeddb is a simple document store with no partial update. You read full object, update it and write them back in full. If updating such meta data is frequent, perhaps meta data may store in separately.

Upvotes: 1

Related Questions