0

in my project I'm using indexedDb through the wrapper Dexie.js. The goal is to fetch the data directly from the indexedDb instead of calling api all the time.

Since I might change/delete some data, from time to time, I use a version number to keep track of what is present in the user cache. So when the application starts I receive the version of the db, if that matches the version in the cache I fetch the indexedDb, otherwise I will delete the indexedDb and fetch from the server the new data.

The problem is that I can't find a way to update the version value without adding another entry. Right now this is what I'm doing:

       db.version(1).stores({
              buildings: "id, name, address, lat, long, purchased, [lat+long]",
              versions: "id, version",
          });

          db.open().then((el) => {
              console.log(el);
              this.eventBus.$on("refresh-map", () => {
                  this.fetchHexagons();
              });
          }).catch(Dexie.MissingAPIError, (e) => {
              console.log("Couldn't find indexedDB API");
              console.log(e);
              rollback = true;
          }).catch((error) => {
              console.log(`error: ${error}`);
          });

          db.versions.toArray((res) => {
              if (res.length > 0 && res[0].version != version.body.model_version) {
                  db.buildings.clear().then(() => {
                      console.log("clearing buildings");
                  });
              }

              db.versions.update(1, { version: "2"}).then((update) => {
                  if (!update) {
                      db.versions.put({ version: version.body.model_version });
                  }
              });

The problem is that I get the following error:

dexie.es.js?f8d5:1384 Unhandled rejection: Error: Failed to execute 'put' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value.

I just want to make sure that whenever I add/put the version field I only substitute that value without adding anything else: that way the db in the "versions" table will always have one element.

How can I solve this?

1 Answer 1

0

This line:

db.versions.put({ version: version.body.model_version });

...needs to provide an id:

db.versions.put({ id: someUniqueId, version: version.body.model_version });

Another option is to use an auto-incremented primary key. If so, you would need to recreate the DB (primary keys may not be changed on existing db):

db.version(1).stores({
    buildings: "id, name, address, lat, long, purchased, [lat+long]",
    versions: "++id, version",
});

To recreate the db:

db.delete().then(()=>db.open())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.