Mythli Mythli - 18 days ago 5
Javascript Question

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

Answer

There is indeed a skip_setup option available.

As stated in the documentation, by default, PouchDB will checks 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...
  });
Comments