@@ -11,6 +11,8 @@ var exports = {},
11
11
json2csv = require ( 'json2csv' ) ,
12
12
json2xls = require ( 'json2xls' ) ;
13
13
14
+ //const log = require('./../../utils/log.js')('core:export');
15
+
14
16
//npm install node-xlsx-stream !!!!!
15
17
var xlsx = require ( "node-xlsx-stream" ) ;
16
18
const Transform = require ( 'stream' ) . Transform ;
@@ -316,7 +318,7 @@ function getValues(values, valuesMap, paramList, doc, options) {
316
318
/**
317
319
* Stream data as response
318
320
* @param {params } params - params object
319
- * @param {Stream } stream - cursor stream
321
+ * @param {Stream } stream - cursor object. Need to call stream on it.
320
322
* @param {string } options - options object
321
323
options.filename - name of the file to output to browser
322
324
options.type - type to be used in content type
@@ -354,7 +356,7 @@ exports.stream = function(params, stream, options) {
354
356
params . res . write ( head . join ( ',' ) + '\r\n' ) ;
355
357
}
356
358
357
- stream . on ( 'data' , function ( doc ) {
359
+ stream . stream ( options . streamOptions ) . on ( 'data' , function ( doc ) {
358
360
var values = [ ] ;
359
361
var valuesMap = { } ;
360
362
getValues ( values , valuesMap , paramList , doc , { mapper : mapper , collectProp : listAtEnd } ) ; // if we have list at end - then we don'thave projection
@@ -382,7 +384,7 @@ exports.stream = function(params, stream, options) {
382
384
if ( listAtEnd === false ) {
383
385
sheet . write ( paramList ) ;
384
386
}
385
- stream . on ( 'data' , function ( doc ) {
387
+ stream . stream ( options . streamOptions ) . on ( 'data' , function ( doc ) {
386
388
var values = [ ] ;
387
389
var valuesMap = { } ;
388
390
getValues ( values , valuesMap , paramList , doc , { mapper : mapper , collectProp : listAtEnd } ) ;
@@ -400,7 +402,7 @@ exports.stream = function(params, stream, options) {
400
402
else {
401
403
params . res . write ( "[" ) ;
402
404
var first = false ;
403
- stream . on ( 'data' , function ( doc ) {
405
+ stream . stream ( options . streamOptions ) . on ( 'data' , function ( doc ) {
404
406
if ( ! first ) {
405
407
first = true ;
406
408
params . res . write ( doc ) ;
@@ -478,30 +480,24 @@ exports.fromDatabase = function(options) {
478
480
if ( options . sort ) {
479
481
cursor . sort ( options . sort ) ;
480
482
}
481
- if ( options . limit ) {
482
- cursor . limit ( parseInt ( options . limit ) ) ;
483
- }
483
+
484
484
if ( options . skip ) {
485
485
cursor . skip ( parseInt ( options . skip ) ) ;
486
486
}
487
-
487
+ if ( options . limit ) {
488
+ cursor . limit ( parseInt ( options . limit ) ) ;
489
+ }
490
+ options . streamOptions = { } ;
488
491
if ( options . type === "stream" || options . type === "json" ) {
489
- options . output = options . output || function ( stream ) {
490
- exports . stream ( options . params , stream , options ) ;
492
+ options . streamOptions . transform = function ( doc ) {
493
+ doc = transformValuesInObject ( doc , options . mapper ) ;
494
+ return JSON . stringify ( doc ) ;
491
495
} ;
492
- cursor . stream ( {
493
- transform : function ( doc ) {
494
- doc = transformValuesInObject ( doc , options . mapper ) ;
495
- return JSON . stringify ( doc ) ;
496
- }
497
- } ) ;
498
- options . output ( cursor ) ;
499
496
}
500
- else if ( options . type === "xls" || options . type === "xlsx" || options . type === "csv" ) {
497
+ if ( options . type === "stream" || options . type === "json" || options . type === "xls" || options . type === "xlsx" || options . type === "csv" ) {
501
498
options . output = options . output || function ( stream ) {
502
499
exports . stream ( options . params , stream , options ) ;
503
500
} ;
504
- cursor . stream ( ) ;
505
501
options . output ( cursor ) ;
506
502
}
507
503
else {
@@ -593,19 +589,15 @@ exports.fromRequestQuery = function(options) {
593
589
done ( null , data ) ;
594
590
}
595
591
} ) ;
592
+ options . streamOptions = { } ;
596
593
if ( options . type === "stream" || options . type === "json" ) {
597
- cursor . stream ( {
598
- transform : function ( doc ) {
599
- doc = transformValuesInObject ( doc , options . mapper ) ;
600
- return JSON . stringify ( doc ) ;
601
- }
602
- } ) ;
603
- exports . stream ( { res : outputStream } , cursor , options ) ;
604
- }
605
- else if ( options . type === "xls" || options . type === "xlsx" || options . type === "csv" ) {
606
- cursor . stream ( ) ;
607
- exports . stream ( { res : outputStream } , cursor , options ) ;
594
+ options . streamOptions . transform = function ( doc ) {
595
+ doc = transformValuesInObject ( doc , options . mapper ) ;
596
+ return JSON . stringify ( doc ) ;
597
+ } ;
608
598
}
599
+ exports . stream ( { res : outputStream } , cursor , options ) ;
600
+
609
601
options . output ( outputStream ) ;
610
602
}
611
603
}
0 commit comments