Rounin
Rounin

Reputation: 29463

How can I `put` new values in an already existing objectStore in an already existing indexedDB?

I am tying myself up in knots trying to update a series of four entries in an objectStore in an indexedDB.

This is what I want to achieve (in pseudo-code):

let myDatabase = indexedDB('myDatabase', 1);
let myObjectStore = myDatabase.myObjectStore;
myObjectStore.entry1 = 'newValue1';
myObjectStore.entry2 = 'newValue2';
myObjectStore.entry3 = 'newValue3';
myObjectStore.entry4 = 'newValue4';

But of course, it isn't anything like that straightforward.

I understand I need to use put. But, despite numerous attempted approaches, I can't get further than that.

I have got as far as successfully setting up and populating the objectStore in the first place when the indexedDB is first created:

// SET UP VALUES OBJECT

let valuesObject = {

  entry1 : 'a',
  entry2 : 'b',
  entry3 : 'c',
  entry4 : 'd'
};


// SET UP INDEXED DATABASE

const setUpIndexedDatabase = (valuesObject) => {

  let database
  const databaseVersion = 1; 
  const databaseName = \'myDatabase\';
  const databaseOpenRequest = indexedDB.open(databaseName, databaseVersion);

  databaseOpenRequest.onupgradeneeded = () => {

    database = databaseOpenRequest.result;

    let myObjectStore = database.createObjectStore('myObjectStore');

    myObjectStore.transaction.oncomplete = () => {

      let objectStoreValues = database.transaction('Values', 'readwrite').objectStore('Values');

      const valuesEntries = Object.entries(valuesObject);

      for (let i = 0; i < valuesEntries.length; i++) {

        objectStoreValues.add(valuesEntries[i][1], valuesEntries[i][0]);
      }
    }
  }


  databaseOpenRequest.onsuccess = () => {

    database = databaseOpenRequest.result;

    // >>> THIS IS THE BIT THAT I NEED TO WRITE <<<

    database.close();
  }
}

setUpIndexedDatabase(valuesObject);

So far, so good. The code above fires the onupgradeneeded event if no database exists yet, which creates myObjectStore and populates it with four key-value pairs.

But if the database does exist and already contains myObjectStore, then every variation of code I have written using put fails to update the values for the keys and returns various errors - and quite often no errors at all.

All I want to do is update values in the database.

I think the problem is that I don't know how to use put properly when the Database Version remains unchanged and onupgradeneeded doesn't fire.

Upvotes: 2

Views: 229

Answers (1)

mgarcia
mgarcia

Reputation: 6325

If you want to update an already existing value in the database, you can do so with the following code (as example, I am updating the entry1 entry):

databaseOpenRequest.onsuccess = function(event) {
    db = event.target.result;

    const objectStore = db.transaction('myObjectStore', 'readwrite').objectStore('myObjectStore');
    const request = objectStore.put('e', 'entry1');
    request.onerror = function(event) {
        // There was an error while updating.
    };
    request.onsuccess = function(event) {
        // The update was successful.
    };
}

Upvotes: 3

Related Questions