Search code examples
javascriptindexeddbdexie

Dexie.js Autoincrement Primary Key - does it ever reset? How to reset it?


In Dexie.js you can create a store with an auto-incrementing key

    let db = new Dexie("nav_api");
    db.version(1).stores({
        jobs: '++id, json'
    });

So to test, I created 14 objects in the db via db.jobs.put({json: '[]'}), and all their keys came out as expected, started at 1 up to 14. Then deleted some of the later ones, db.jobs.where('id').above(6).delete(), and added another one to the db, and its index was 15.

Is there any way to reset the index to 0? I was using it for ordering, and I'm not sure what happens when the value gets incredibly large -- does it eventually wrap back around to 0?

On the other hand, it's also probably not something I need to worry about. Depending on how high the '++id' field can be, and I'm assuming it will be in the billions, it would be many many years (many lifetimes) before it was ever something to worry about. So maybe I should just ignore it.

[edit] I tried clearing the table, db.jobs.clear() and then putting a new row in the table, but again it used the next index after. I can't find a way to fully delete a table, however I can fully delete an entire database but I don't really want to do that.


Solution

  • The IndexedDB spec has more info.

    You probably won't hit the upper limit:

    The current number is always a positive integer less than or equal to 253 (9007199254740992) + 1.

    And there is no way to reset it, other than deleting the object store and recreating it:

    The current number for a key generator never decreases, other than as a result of database operations being reverted. Deleting a record from an object store never affects the object store’s key generator. Even clearing all records from an object store, for example using the clear() method, does not affect the current number of the object store’s key generator.