Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PouchDB check if local database exists

the question is simple but even through an exhaustive search through the internet and the pouchdb source I could not find a function to check if a local database exists.

The use case for this would be to check if a local database is already existing and then make a successfull login optional.

Best regards

like image 542
Mythli Avatar asked Jul 23 '16 12:07

Mythli


3 Answers

There is indeed a skip_setup option available.

As stated in the documentation, by default, PouchDB will check if the database exists and try to create it if it does not exist yet. You can set this option to true to skip this setup.

With this option enabled, you'll get an error if the database does not exist when you query the database informations for example:

const db = new PouchDb('DB_URL_OR_NAME', { skip_setup: true });

db.info()
  .then(() => {
    // The database exists.
    // Do something...
  })
  .catch(e => {
    // No database found and it was not created.
    // Do something else...
  });
like image 185
HiDeo Avatar answered Nov 15 '22 07:11

HiDeo


I needed to confirm a database was wiped off of my client's browsers. The problem is you need to create the database just to delete it...

The default way that Pouch stores the data, at least on Chrome, is inside an indexedDB with the prefix _pouch_. So I simply just delete the indexedDB database.

Here my code. It returns a promise to match the PouchDB syntax.

function deletePouchDatabase(dbName) {
  return new Promise((resolve, reject) => {
    var req = indexedDB.deleteDatabase('_pouch_' + dbName);
    req.onsuccess = function () {
      resolve(null)
    };
    req.onerror = function () {
      reject(new Error("There was an error"))
    };
    req.onblocked = function () {
      reject(new Error("The operation was blocked"))
    };
  });
}

So you can use a similar approach to check if it exists. The promise returns true if is exists and false if it does not.

function pouchDatadatabaseExists(dbname) {
  return new Promise((resolve, reject) => {
    const req = indexedDB.open(`_pouch_${dbname}`);
    req.onsuccess = function () {
      resolve(true)
    }
    req.onupgradeneeded = function () {
      resolve(false)
    }
  });
}
like image 32
Steven Spungin Avatar answered Nov 15 '22 06:11

Steven Spungin


As akofman pointed out, skip_setup does not work with local databases. So the only way I know to do this is a workaround - checking to see if the new database is empty and then immediately deleting it. Of course this does not help if the database in question exists but is empty...

const testdb = new PouchDB('testdb_name');

testdb.info().then(function (details) {
if (details.doc_count == 0 && details.update_seq == 0) {
    alert ('database does not exist');
    testdb.destroy().then (function() {console.log('test db removed');});
}
else alert ('database exists');
})
.catch(function (err) {
   console.log('error: ' + err);
return;
});
like image 7
IanC Avatar answered Nov 15 '22 07:11

IanC