CartoDB-SQL-API/app/services/stream_copy.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-05-25 23:42:30 +08:00
const zlib = require('zlib');
const PSQL = require('cartodb-psql');
const copyTo = require('pg-copy-streams').to;
const copyFrom = require('pg-copy-streams').from;
2018-05-26 00:47:41 +08:00
const StreamCopyMetrics = require('./stream_copy_metrics');
const { Client } = require('pg');
2018-06-08 17:15:23 +08:00
const Logger = require('./logger');
2018-05-25 23:42:30 +08:00
2018-06-08 22:50:12 +08:00
module.exports = {
2018-06-08 22:58:32 +08:00
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)
});
2018-06-08 22:50:12 +08:00
},
2018-06-08 17:09:51 +08:00
2018-06-08 22:50:12 +08:00
from(sql, userDbParams, cb, next) {
const pg = new PSQL(userDbParams);
pg.connect(function (err, client, done) {
if (err) {
cb(err);
}
let copyFromStream = copyFrom(sql);
const pgstream = client.query(copyFromStream);
pgstream
.on('error', err => {
done();
cb(err, pgstream);
})
.on('end', function () {
done();
next(null, copyFromStream.rowCount);
});
cb(null, pgstream, client, done)
});
2018-05-25 23:42:30 +08:00
}
2018-05-26 00:50:56 +08:00
};