@@ -248,11 +248,27 @@ module.exports = function(RED) {
248
248
249
249
if ( node . search === "_id_" ) {
250
250
var id = getDocumentId ( msg . payload ) ;
251
+ var attachmentName = getAttachementName ( msg . payload ) ;
252
+ var attachmentType = getAttachementType ( msg . payload ) ;
251
253
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
+ }
256
272
}
257
273
else if ( node . search === "_idx_" ) {
258
274
options . query = options . query || options . q || formatSearchQuery ( msg . payload ) ;
@@ -283,7 +299,22 @@ module.exports = function(RED) {
283
299
284
300
return payload ;
285
301
}
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
+ }
287
318
function formatSearchQuery ( query ) {
288
319
if ( typeof query === "object" ) {
289
320
// useful when passing the query on HTTP params
@@ -298,7 +329,20 @@ module.exports = function(RED) {
298
329
}
299
330
return query ;
300
331
}
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
+ }
302
346
function sendDocumentOnPayload ( err , body , msg ) {
303
347
if ( ! err ) {
304
348
msg . cloudant = body ;
0 commit comments