Reputation: 2236
It seems that I need to load a couchDB JSON document to the client and then post it to another couchdb database in order to copy it from one db to another? Or is there a server side method to do so?
Reference http://docs.couchdb.org/en/latest/api/documents.html#copy-db-doc The copy command is non standard http and is within one db only.
Upvotes: 3
Views: 3729
Reputation: 826
Using node.js and the request module.
Pre: The destination document exists in the DB. The origin attachment exists in the DB
var originAttachment = 'somefile.txt'
var originDocId = '1somecouchdbid';
var origindb = 'http://localhost:5984/db1';
var destinationAttachment = 'somefile.txt'
var destinationDocId = '2somecouchdbid';
var desinationdb = 'http://localhost:5984/db2';
var uridestination = desinationdb + "/" + destinationDocId;
request(uridestination, function(err, res, body){
if(err){
throw err;
}
var doc = JSON.parse(body);
var origin = origindb + '/' + originDocId + '/' + encodeURIComponent(originAttachment);
var optionsOrigin = {
url: origin
};
var uridestination = desinationdb + '/' + destinationDocId + '/' + encodeURIComponent(destinationAttachment) + '?rev=' + doc._rev;
var optionDestination = {
url: uridestination,
method: 'PUT',
headers: {
'Content-Type': false
}
};
request(optionsOrigin)
.pipe(request(optionsDestination, function(err, res, body){
if(err){
throw err;
}
console.log(body);
}));
});
Upvotes: 0
Reputation: 4679
Yes, COPY
ing is only possible within single database, but you may replicate single or multiple documents instead:
curl -X POST http://localhost:5984/_replicate -H "Content-Type: application/json" -d '{"source": "db_a", "target":"db_b", "doc_ids": ["foo"]}'
However, you cannot change document ID in this case like you can with COPY
. If you need this, COPY
document first, replicate it and remove it in source if needed. Three HTTP API calls for using only server side methods and not loading document content to the client - decision to use it instead of having copy-logic on the client is yours.
Upvotes: 6