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

50 lines
1.3 KiB
JavaScript
Raw Normal View History

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-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);
2018-06-08 23:04:28 +08:00
});
2018-06-08 22:58:32 +08:00
2018-06-08 23:04:28 +08:00
cb(null, pgstream, client, done);
2018-06-08 22:58:32 +08:00
});
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);
});
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-05-26 00:50:56 +08:00
};