refactor of copy to PoC

This commit is contained in:
Simon Martín 2018-06-08 16:58:32 +02:00
parent f7454228c6
commit 014f0a1491
2 changed files with 21 additions and 11 deletions

View File

@ -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);
});
};
}

View File

@ -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) {