From 014f0a1491b1de8e7cb9414a25c520458efc1723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Fri, 8 Jun 2018 16:58:32 +0200 Subject: [PATCH] refactor of copy to PoC --- app/controllers/copy_controller.js | 12 +++--------- app/services/stream_copy.js | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/controllers/copy_controller.js b/app/controllers/copy_controller.js index 33a5b032..97659819 100644 --- a/app/controllers/copy_controller.js +++ b/app/controllers/copy_controller.js @@ -75,8 +75,7 @@ function handleCopyTo () { res.header("Content-Disposition", `attachment; filename=${encodeURIComponent(filename)}`); res.header("Content-Type", "application/octet-stream"); - const pg = new PSQL(userDbParams); - pg.connect(function (err, client, done) { + streamCopy.to(sql, userDbParams, function (err, pgstream, client, done) { if (err) { return next(err); } @@ -84,8 +83,6 @@ function handleCopyTo () { let responseEnded = false; let connectionClosedByClient = false; - const copyToStream = copyTo(sql); - const pgstream = client.query(copyToStream); res .on('error', err => { @@ -123,12 +120,9 @@ function handleCopyTo () { } }) .on('data', data => metrics.addSize(data.length)) - .on('end', () => { - metrics.end(copyToStream.rowCount); - done(); - return next(null, metrics); - }) .pipe(res); + }, function (err, rows) { + metrics.end(rows); }); }; } diff --git a/app/services/stream_copy.js b/app/services/stream_copy.js index 9e96332b..df42bb07 100644 --- a/app/services/stream_copy.js +++ b/app/services/stream_copy.js @@ -7,8 +7,24 @@ const { Client } = require('pg'); const Logger = require('./logger'); module.exports = { - to() { - + to(sql, userDbParams, cb, next) { + const pg = new PSQL(userDbParams); + pg.connect(function (err, client, done) { + if (err) { + cb(err); + } + + const copyToStream = copyTo(sql); + const pgstream = client.query(copyToStream); + + pgstream + .on('end', () => { + done(); + next(null, copyToStream.rowCount); + }) + + cb(null, pgstream, client, done) + }); }, from(sql, userDbParams, cb, next) {