How to read current db version number in dexie?
Asked Answered
L

2

5

In Dexie, when you need to upgrade your schemas or table architectures, you use db.version(X) method.

The library will then check whether the user's browser has a previous version cached and do the appropriate upgrade steps.

But I cannot find how to read the current user's db version manually.

So for example if write this purposefully breaking code:

db.version(1).stores(/* my schema */) // remove this code
db.version(2).stores(/* my schema */)

The library will politely throw an error saying that the version the user currently has (obviously, 1) has no schema.

But is there a way to read what version exactly the user has?

It could be useful for debugging!

Lycanthropy answered 19/5, 2017 at 14:11 Comment(0)
D
5

It is possible to skip the whole version() thing if you only want to inspect the database. For instance, I'm able to print the version number of my database by using this code.

new Dexie("myDB").open().then((db) => console.log(db.verno))

Note that (as stated in the documentation), Dexie multiples the version number set through .version() by 10 so if you inspect the database in your browser's debugger you will see a number which is 10 times the number you get from using the verno field.

Domicile answered 6/6, 2017 at 15:40 Comment(0)
P
4

In Dexie version 3+. You can use:

  • Dexie.verno: to get the dexie version. The dexie version is the highest value of the registered versions.
  • Dexie.idbdb.version: to get the native version of the IndexedDB. It's the Dexie.verno multipled by 10. See here for more technical explanation.
const db = new Dexie("AppDB");

db.version(1);
db.version(2);
db.version(5);

db.open();

db.on("ready", () => {
  console.log(db.verno); // 5
  console.log(db.idbdb.version); // 50
});
Pitanga answered 12/2, 2021 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.