2013-08-31 3 views
2

couchDB JSON 문서를 클라이언트에로드 한 다음 다른 couchdb 데이터베이스에 게시하여 하나의 데이터베이스에서 다른 데이터베이스로 복사해야하는 것처럼 보입니까? 아니면 그렇게 할 수있는 서버 측 방법이 있습니까?하나의 데이터베이스에서 다른 데이터베이스로 문서를 복사하는 방법 (couchDB)

참조 http://docs.couchdb.org/en/latest/api/documents.html#copy-db-doc 복사 명령은 HTTP가 아닌 표준이며 내에서 하나는 dB입니다.

답변

2

예, COPY ING는 단일 데이터베이스 내에서만 가능하지만, 대신 하나 또는 여러 개의 문서를 복제 할 수 있습니다 : 당신이 COPY 때와 수처럼

curl -X POST http://localhost:5984/_replicate -H "Content-Type: application/json" -d '{"source": "db_a", "target":"db_b", "doc_ids": ["foo"]}' 

그러나이 경우 문서 ID를 변경할 수 없습니다. 필요하면 COPY 문서를 먼저 복사하고 필요한 경우 소스에서 제거하십시오. 서버 측 메서드 만 사용하고 클라이언트에 문서 내용을로드하지 않기위한 3 가지 HTTP API 호출 - 클라이언트에서 복사 논리를 사용하는 대신이를 사용하기로 결정한 것입니다.

0

node.jsrequest 모듈을 사용하십시오.

Pre : 대상 문서가 DB에 있습니다. 원본 첨부물이 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);   
})); 
}); 
관련 문제