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

64 lines
1.7 KiB
JavaScript
Raw Normal View History

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;
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) {
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-08 22:58:32 +08:00
pgstream
.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
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) {
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 => {
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
}
setConnectionClosedByClient(connectionClosedByClient) {
this.connectionClosedByClient = connectionClosedByClient;
}
2018-05-26 00:50:56 +08:00
};