From cc7673e8418bbeedb0fde07a55acb677aec2f526 Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Wed, 28 Jun 2017 09:58:44 -0300
Subject: [PATCH] [ci skip]Add streams to download job

---
 src/libs/db/mongoose.js   |  2 +-
 src/libs/jobs/download.js | 26 +++++++++-----------------
 src/libs/routes/region.js | 16 ++++++++--------
 3 files changed, 18 insertions(+), 26 deletions(-)

diff --git a/src/libs/db/mongoose.js b/src/libs/db/mongoose.js
index d8594158..f9d2ed8e 100644
--- a/src/libs/db/mongoose.js
+++ b/src/libs/db/mongoose.js
@@ -11,7 +11,7 @@ mongoose.Promise = global.Promise;
 module.exports = () => {
     // Get mongodb URI (ip and port) in config file
     const mongoUri = process.env.MONGO_URI || config.mongodb.uri;
-    log.debug(`Connecting to MongoDB on URI ${mongoUri}`);
+    log.info(`Connecting to MongoDB on URI ${mongoUri}`);
     // Connection singleton
     const db = mongoose.connect(mongoUri);
 
diff --git a/src/libs/jobs/download.js b/src/libs/jobs/download.js
index bf76afd2..14eb05b5 100644
--- a/src/libs/jobs/download.js
+++ b/src/libs/jobs/download.js
@@ -3,29 +3,21 @@ const log = require(`${libs}/log`)(module);
 const execQuery = require(`${libs}/db/query_exec`);
 const jsonexport = require('jsonexport');
 const fs = require('fs');
+const Readable = require('stream').Readable;
 
 module.exports = function(agenda) {
     agenda.define('download database', (job, done) => {
         let sql = job.attrs.data.sql;
         execQuery(sql.text, sql.values).then((result) => {
             log.debug('Got results');
-            jsonexport(result, (err, csv) => {
-                if(err) {
-                    log.error('Error converting to CSV');
-                    log.error(err);
-                    return done(err);
-                }
-                log.debug('Converted to csv');
-                fs.writeFile('out.csv', csv, (err) => {
-                    if(err) {
-                        log.error('Error writing file');
-                        log.error(err);
-                        return done(err);
-                    }
-                    log.info('File created');
-                    done();
-                });
-            });
+            log.debug(typeof result);
+
+            let reader = new Readable();
+            reader._read = function noop(){};
+            reader.push(result);
+            let writer = fs.createWriteStream('out.csv');
+
+            reader.pipe(jsonexport()).pipe(writer);
         }, (err) => {
             log.error('Error getting data from database!');
             log.error(err);
diff --git a/src/libs/routes/region.js b/src/libs/routes/region.js
index 5afeea77..1d4344ab 100644
--- a/src/libs/routes/region.js
+++ b/src/libs/routes/region.js
@@ -53,13 +53,13 @@ regionApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     next();
 }, query, response('region'));
 
-// const agenda = require(`${libs}/agenda`);
-//
-// regionApp.get('/download', (req, res) => {
-//     req.sql.from('regiao').field('*');
-//     let params = req.sql.toParam();
-//     agenda.now('download database', {sql: params});
-//     res.json({msg: 'Wait for it...'});
-// });
+const agenda = require(`${libs}/agenda`);
+
+regionApp.get('/download', (req, res) => {
+    req.sql.from('regiao').field('*');
+    let params = req.sql.toParam();
+    agenda.now('download database', {sql: params});
+    res.json({msg: 'Wait for it...'});
+});
 
 module.exports = regionApp;
-- 
GitLab