Reputation: 738
When I try to call Dexie on a database on which another call has been done by IndexedDB, there rises an error that the connection is already made to the database.
Can we pass an existing connection to Indexedb to Dexie? This can be helpful when we want to use same connection in a Dexie object and another object and this happens to me when I try to add Dexie to my project. I don't want to rewrite the existing function.
Example:
function initDataBase(callback){
if(window.indexedDB){
var requeteBDD = window.indexedDB.open("databasename",1);
requeteBDD.onsuccess = function(){
if(typeof callback == "function")
callback(requeteBDD.result);
};
}
}
So can we do,for instance
initDataBase(function(db){
var dex = new Dexie(db);
});
I would like to use same connection as the first one. Is it possible?
Upvotes: 0
Views: 544
Reputation: 5691
It's not possible as of current version to pass an instance of IDBDatabase into Dexie constructor. However, this would definitely be an easy pull request to do in the source, as Dexie already has the ability to open existing database by name and adapt to it's existing schema.
However, you should not get an error if you instanciate multiple IDBDatabases to same database name unless one of them tries to upgrade it using another version.
Dexie can open an existing database without creating the schema (even though you can only pass a name and not the db instance), as shown in the following fiddle: https://jsfiddle.net/dfahlander/b8Levamm/
new Dexie('MyDatabase').open().then(function (db) {
log ("Found database: " + db.name);
log ("Database version: " + db.verno);
db.tables.forEach(function (table) {
log ("Found table: " + table.name);
log ("Table Schema: " +
JSON.stringify(table.schema, null, 4));
});
}).catch('NoSuchDatabaseError', function(e) {
// Database with that name did not exist
log ("Database not found");
}).catch(function (e) {
log ("Oh uh: " + e);
});
(which fails because the given DB is not there. But if you create it on jsfiddle and run it again, you'll see it open).
Upvotes: 1