Search code examples
databasecouchdbpouchdbcloudant

PouchDB relative remote database (with CouchDB / Cloudant)


I am creating a CouchApp and have a question about remote databases and creating a PouchDB object.

  • The CouchApp is available from htps://subdomain.website.com/
  • The CouchDB instance is on htps://username.cloudant.com/database
  • I use a rewrite rule in the database
  • There is a Virtual Host from subdomain.website.com to username.cloudant.com/database/_design/client/_rewrite
  • I am not using replication, only a direct connection

I currently create a new PouchDB object using:

var db = new PouchDB('htps://username.cloudant.com/database');

But I would rather instantiate it as:

var db = new PouchDB('/database');

or

var db = new PouchDB('database');

My guess is that PouchDB sees this as a local database instead of a remote database. Is my thinking correct, and how can I get my desired result? I want to strip the hostname of the URL because I want to keep everything portable and replicate it over other servers.


Solution

  • Since you defined a vhost, you can use the following workaround mabye :

    var url = new URL(window.location.href);
    var host = url.host;
    
    var db = new PouchDB(host+"/database");