Question

I have created a DB with YDN :

var db = new ydn.db.Storage('our_db', our_schema);

The second time, I test if this DB exists. In case of success I would like to call this DB to apply db.put() and other stuffs...

How do I retrieve this DB into the variable "db" in this case ?

My test code :

if (window.indexedDB){ // if browser supports IndexedDB, create IndexedDB DB
    var request = window.indexedDB.open("our_db", 1);
    request.onerror = function(event) {
    };

    request.onsuccess = function(event) {
        var db = ?????? ;
    };

    request.onupgradeneeded = function(event) {
        // create the datastore
        var db = new ydn.db.Storage('our_db', our_schema);
        console.log("New storage !");
    };
}

In case if you have better ideas in mind... Thanks for advices !

Was it helpful?

Solution

Since ydn-db will do these boilerplate for opening database (IndexedDB and WebSQL), you can just do

var db = new ydn.db.Storage('our_db', our_schema);
db.put('store name', data).done(function(key) {
  console.log('put with key ' + key);
});

It is also not necessary to test support for IndexedDB. If supported, it will be used as first priority. Otherwise it will use other storage mechanisms.

To know the database already exists in the client, listen to ready event:

db.addEventListener('ready', function(e) {
  var ver = e.getOldVersion();
  if (ver > 0) {
     console.log('existing database version: ' + ver);
   } else {
     console.log('new database created');
   }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top