Skip to content

Dexie.exists()

David Fahlander edited this page Jun 13, 2016 · 10 revisions

Since v1.2

Syntax

Dexie.exists(dbName)

Return Value

Promise<boolean>

Description

Checks whether a database with the given name exists or not. Returns a Promise that resolves with true or false.

IMPORTANT NOTICE You normally won't need this method! Dexie will automatically check the following on db.open():

  • If database does not exists, Dexie will create it automatically.
  • If database exists but on a previous version, Dexie will upgrade it for you.
  • If database exists on the declared version, Dexie will just open it for you.

So, don't do stuff like:

Dexie.exists('my-database').then(function (exists) {
    if (!exists) {
        var db = new Dexie('my-database');
        db.version(1).stores(...);
        db.open();
    }
}

It's simply NOT needed. Instead, just do:

var db = new Dexie('my-database');
db.version(1).stores(...);
db.open().catch(function (err) {
    console.error (err.stack || err);
});

...and it will create the db when needed but just open it if not needing to create it.

Sample

Dexie.exists("myDatabase")
.then(function(exists) {
    if (exists)
        console.log("Database exists");
    else
        console.log("Database doesnt exist");
}).catch(function (error) {
    console.error("Oops, an error occurred when trying to check database existance");
});
Clone this wiki locally