PouchDB check if local database exists
Asked Answered
R

3

15

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

Rufina answered 23/7, 2016 at 12:37 Comment(1)
Still no solution?Lacedaemon
M
10

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...
  });
Mum answered 23/7, 2016 at 12:51 Comment(3)
Interesting. I tried info() without skip_setup which created the database. Thanks for pointing out the combination. Works like a charm.Rufina
In the documentation, it is stated that this option is only available for remote databases and this question is about a local database. Unfortunately I tried this option with a local database and it doesn't work.Congress
as @Congress said, the question is about the local database and the 'skip_setup' does not apply here.Tallula
L
8

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;
});
Lozada answered 16/11, 2016 at 20:25 Comment(0)
M
2

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)
    }
  });
}
Macedo answered 25/7, 2018 at 2:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.