Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replicate from CouchDB to PouchDB?

I've set up a local CouchDB database and I'd like to replicate it to a PouchDB database, using JavaScript in a web page running on localhost.

With the code below I get this error:

Origin http://localhost is not allowed by Access-Control-Allow-Origin.

With http:// removed from REMOTE, I don't get an error, but no docs are shown as replicated.

Looking at IndexedDB databases from Chrome DevTools, I can see the database has been created (but doesn't appear to have documents).

Running in Chrome 29.0.1535.2 canary.

Can I do this locally, or do I need to set up a remote CouchDB database and enable CORS (as per the CouchDB docs)?

var REMOTE = 'http://127.0.0.1:5984/foo';
var LOCAL = 'idb://foo';

Pouch(LOCAL, function(error, pouchdb){
  if (error) {
    console.log("Error: ", error);
  } else {
    var db = pouchdb;
    Pouch.replicate(REMOTE, LOCAL, function (error, changes) {
      if (error) {
        console.log('Error: ', error);
      }
      else {
        console.log('Changes: ', changes);
        db.allDocs({include_docs: true}, function(error, docs) {
          console.log('Rows: ', docs.rows);
        });
    }});
  }
});
like image 981
Sam Dutton Avatar asked Jun 11 '13 15:06

Sam Dutton


People also ask

How do you replicate a CouchDB database?

Simple Replication with the Admin InterfaceStart CouchDB and open your browser to http://127.0.0.1:5984/_utils/ . On the righthand side, you will see a list of things to visit in Futon. Click on “Replication.” Futon will show you an interface to start replication.

Why would you use PouchDB along with CouchDB?

CouchDB and PouchDB are build around the idea of syncing your data. But not only live sync, but losing the connection, and continuing accessing and changing your data. And once your back online, sync it. With PouchDB on the clients browser and CouchDB on the backend your web app can become offline first capable.


1 Answers

You can do it locally, but CORS has to be enabled.

When you remove "http://" from the remote URL, Pouch is going to replicate your DB into a new IndexedDB-backed Pouchdb named "localhost" (or actually "_pouch_localhost" or something like that - it adds a prefix).

Unless you're serving up this page from CouchDB itself (on the same host & port), you will need to enable CORS to get replication to CouchDB working.

like image 114
jches Avatar answered Oct 10 '22 16:10

jches