Search code examples
javascriptdexie

Updating table 'primary key' using dexie.js


I am using an dexie.js to interact with an IndexedDB table on a users local system when offline. This table will sync back to the server MariaDB table when the user is online. I am having an issue after the data has been pushed to the server, there is a possibility that another user with have pushed up another value with the same ID value (used as a primary key). I therefore return the servers ID value back to the local system, in JSON format, to update the IndexedDB value, but when I try this I run into a problem, and can not seem to update the ID value.

If I update only the added value to be empty the update command works fine.

//SETTING UP THE INITAL TABLE
database.version(1).stores({
  table:          '++id,text,added'
});

//POPULATE TABLE WITH SOME DATA
database.on("populate", function() {
  database.table.add({id: 1, text: "Test Value", added: 1});
});

//UPDATE DATA
database.table.update(1, {id: 4, added: ''});  // DOESN'T WORK
database.table.update(1, {added: ''});  // WORKS

Is the only option to remove the current record and re-insert it with the new values? This would be a problem and require a significant rewrite of my API so I am trying to avoid this.


Solution

  • IndexedDB does not support changing the primary key of an entry but in [email protected] it is supported by removing/adding items when using table.update(). You can install it using npm install dexie@next.