2018-06-13 00:39:50 +08:00
|
|
|
const EventEmitter = require('events');
|
2018-05-25 23:42:30 +08:00
|
|
|
const PSQL = require('cartodb-psql');
|
|
|
|
const copyTo = require('pg-copy-streams').to;
|
|
|
|
const copyFrom = require('pg-copy-streams').from;
|
|
|
|
|
2018-06-13 00:39:50 +08:00
|
|
|
module.exports = class StreamCopy extends EventEmitter {
|
2018-06-12 22:56:18 +08:00
|
|
|
constructor (sql, userDbParams) {
|
2018-06-13 00:39:50 +08:00
|
|
|
super();
|
|
|
|
|
2018-06-12 22:56:18 +08:00
|
|
|
this.pg = new PSQL(userDbParams);
|
|
|
|
this.sql = sql;
|
2018-06-12 23:04:44 +08:00
|
|
|
this.connectionClosedByClient = false;
|
2018-06-12 22:56:18 +08:00
|
|
|
}
|
|
|
|
|
2018-06-13 00:39:50 +08:00
|
|
|
to(cb) {
|
2018-06-12 22:56:18 +08:00
|
|
|
this.pg.connect((err, client, done) => {
|
2018-06-08 22:58:32 +08:00
|
|
|
if (err) {
|
2018-06-14 00:26:58 +08:00
|
|
|
return cb(err);
|
2018-06-08 22:58:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:56:18 +08:00
|
|
|
const copyToStream = copyTo(this.sql);
|
2018-06-08 22:58:32 +08:00
|
|
|
const pgstream = client.query(copyToStream);
|
2018-06-14 00:26:58 +08:00
|
|
|
|
2018-06-08 22:58:32 +08:00
|
|
|
pgstream
|
2018-06-12 23:04:44 +08:00
|
|
|
.on('error', err => {
|
|
|
|
if (!this.connectionClosedByClient) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
})
|
2018-06-08 22:58:32 +08:00
|
|
|
.on('end', () => {
|
|
|
|
done();
|
2018-06-08 23:04:28 +08:00
|
|
|
});
|
2018-06-08 22:58:32 +08:00
|
|
|
|
2018-06-14 02:06:35 +08:00
|
|
|
cb(null, pgstream, copyToStream, client, done);
|
2018-06-08 22:58:32 +08:00
|
|
|
});
|
2018-06-12 22:56:18 +08:00
|
|
|
}
|
2018-06-08 17:09:51 +08:00
|
|
|
|
2018-06-13 00:39:50 +08:00
|
|
|
from(cb) {
|
2018-06-12 22:56:18 +08:00
|
|
|
this.pg.connect((err, client, done) => {
|
2018-06-08 22:50:12 +08:00
|
|
|
if (err) {
|
2018-06-14 00:26:58 +08:00
|
|
|
return cb(err);
|
2018-06-08 22:50:12 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:56:18 +08:00
|
|
|
const copyFromStream = copyFrom(this.sql);
|
2018-06-08 22:50:12 +08:00
|
|
|
const pgstream = client.query(copyFromStream);
|
|
|
|
|
|
|
|
pgstream
|
|
|
|
.on('error', err => {
|
2018-06-14 01:56:56 +08:00
|
|
|
done(err);
|
2018-06-08 22:50:12 +08:00
|
|
|
})
|
2018-06-13 00:39:50 +08:00
|
|
|
.on('end', () => {
|
2018-06-08 22:50:12 +08:00
|
|
|
done();
|
2018-06-13 00:39:50 +08:00
|
|
|
this.emit('copy-from-end', copyFromStream.rowCount);
|
2018-06-08 22:50:12 +08:00
|
|
|
});
|
|
|
|
|
2018-06-08 23:04:28 +08:00
|
|
|
cb(null, pgstream, client, done);
|
2018-06-08 22:50:12 +08:00
|
|
|
});
|
2018-05-25 23:42:30 +08:00
|
|
|
}
|
2018-06-12 23:04:44 +08:00
|
|
|
|
|
|
|
setConnectionClosedByClient(connectionClosedByClient) {
|
|
|
|
this.connectionClosedByClient = connectionClosedByClient;
|
|
|
|
}
|
2018-05-26 00:50:56 +08:00
|
|
|
};
|