Skip to content
Snippets Groups Projects
Commit cc7673e8 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

[ci skip]Add streams to download job

parent a590d58d
No related branches found
No related tags found
1 merge request!116Release v1.0.0
Pipeline #
......@@ -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);
......
......@@ -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);
......
......@@ -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;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment