apache / couchdb-nano

Nano: The official Apache CouchDB library for Node.js
https://www.npmjs.com/package/nano
Apache License 2.0
651 stars 165 forks source link

Replication Enable creating incorrect document #322

Closed jkeczan closed 1 year ago

jkeczan commented 1 year ago

We are having issues with creating replications using replication.enable. The server accepts the request but the replication document does not contain the correct replication information therefore it doesn't start

Expected Behavior

When we call replication.enable(source, target, {create_target: false}), we expect the source of the replication document to look like the following:

{
  "_id": "9d2ad6fedf2c919a28463cef8800086c",
  "_rev": "1-1a6c06225aca25efad513b58ee0866bd",
  "user_ctx": {
    "name": "admin",
    "roles": [
      "_admin",
      "_reader",
      "_writer"
    ]
  },
  "source": {
    "url": "https://test.url.com:5984/soloDB-5-63",
    "headers": {
      "Authorization": "Basic JIMBOB=="
    }
  },
  "target": {
    "url": "mainDB",
    "headers": {
      "Authorization": "Basic JOHNSMITH=="
    }
  },
  "create_target": false,
  "continuous": true,
  "owner": "admin"
}

Current Behavior

However, we are currently getting this document instead with the replication failing to launch and the UI showing it does NOT see any credentials.

{
  "_id": "7131260c0b1a10fddb66464ce802eb2d",
  "_rev": "1-7d100014ab0dc2b18548e06d63f17940",
  "continuous": true,
  "create_target": false,
  "source": "https://user:pass@test.url.com:5984/soloDB-5-27",
  "target": "mainDB",
  "owner": "admin"
}

Possible Solution

At this point, I do not have a possible solution other than potentially dropping down to make a custom request. However, I feel like I am doing something incorrect here and wanted to reach out first.

Steps to Reproduce (for bugs)

const url = 'test.url.com';
 const sourceURL = `https://user:pass@${url}/soloDB-5-27`;
        const targetURL = `mainDB`;

return database.db.replication.enable(sourceURL, targetURL, {
          continuous: true,
          create_target: false,
        });

Context

Right now, this is preventing me from writing an automated onboarding tool that creates the database for a user when they are added to our HR databases.

Your Environment

glynnbird commented 1 year ago

Thanks for the reports @jkeczan but I don't think "replication.enable" is doing anything unexpected here. As you can see from the code, its job is to create a document in the _replicator database with a source and target in it.

If I were you I would just write the document yourself and you get complete control:

e.g.

const replicator = nano.db.use('_replicator')
const doc = {
  source: 'https://mysourceurl.com/a',
  target: 'https://myothersourceurl.com/b',
  create_target: true
}
await replicator.insert(doc)
jkeczan commented 1 year ago

I had already figured that one out as a workaround after I posted the issue but thought it was odd that the replicate function didn't create a document that could be replicated. Appreciate the feedback.