@@ -248,11 +248,27 @@ module.exports = function(RED) {
248248
249249 if ( node . search === "_id_" ) {
250250 var id = getDocumentId ( msg . payload ) ;
251+ var attachmentName = getAttachementName ( msg . payload ) ;
252+ var attachmentType = getAttachementType ( msg . payload ) ;
251253 node . inputId = id ;
252-
253- db . get ( id , function ( err , body ) {
254- sendDocumentOnPayload ( err , body , msg ) ;
255- } ) ;
254+ if ( attachmentName ) {
255+ if ( attachmentType ) {
256+ db . attachment . get ( id , attachmentName , function ( err , body ) {
257+ sendAttachementOnPayload ( err , body , msg , attachmentType ) ;
258+ } ) ;
259+ } else {
260+ db . get ( id , function ( err , body ) {
261+ attachmentType = body . _attachments [ attachmentName ] [ "content_type" ] ;
262+ db . attachment . get ( id , attachmentName , function ( err , body ) {
263+ sendAttachementOnPayload ( err , body , msg , attachmentType ) ;
264+ } ) ;
265+ } ) ;
266+ }
267+ } else {
268+ db . get ( id , function ( err , body ) {
269+ sendDocumentOnPayload ( err , body , msg ) ;
270+ } ) ;
271+ }
256272 }
257273 else if ( node . search === "_idx_" ) {
258274 options . query = options . query || options . q || formatSearchQuery ( msg . payload ) ;
@@ -283,7 +299,22 @@ module.exports = function(RED) {
283299
284300 return payload ;
285301 }
286-
302+ function getAttachementName ( payload ) {
303+ if ( typeof payload === "object" ) {
304+ if ( "attachmentName" in payload ) {
305+ return payload . attachmentName ;
306+ }
307+ }
308+ return null ;
309+ }
310+ function getAttachementType ( payload ) {
311+ if ( typeof payload === "object" ) {
312+ if ( "attachmentType" in payload ) {
313+ return payload . attachmentType ;
314+ }
315+ }
316+ return null ;
317+ }
287318 function formatSearchQuery ( query ) {
288319 if ( typeof query === "object" ) {
289320 // useful when passing the query on HTTP params
@@ -298,7 +329,20 @@ module.exports = function(RED) {
298329 }
299330 return query ;
300331 }
301-
332+ function sendAttachementOnPayload ( err , body , msg , attachmentType ) {
333+ if ( ! err ) {
334+ msg . cloudant = body ;
335+ msg . payload = body ;
336+ msg . headers = {
337+ "Content-Type" : attachmentType
338+ }
339+ }
340+ else {
341+ msg . payload = null ;
342+ node . error ( err . description , err ) ;
343+ }
344+ node . send ( msg ) ;
345+ }
302346 function sendDocumentOnPayload ( err , body , msg ) {
303347 if ( ! err ) {
304348 msg . cloudant = body ;
0 commit comments