-
-
Notifications
You must be signed in to change notification settings - Fork 668
Dexie.exists()
David Fahlander edited this page Jun 13, 2016
·
10 revisions
Since v1.2
Dexie.exists(dbName)
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 detect if database creation or upgrade is needed. The following checks are made withing 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.
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");
});
Dexie.js - minimalistic and bullet proof indexedDB library