brianc / node-pg-copy-streams

COPY FROM / COPY TO for node-postgres. Stream from one database to another, and stuff.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.to returns a writable stream

kilianc opened this issue · comments

const copyTo = require('pg-copy-streams').to

const csvStream = pg.query(exports.copyTo(dedent`
  COPY (
    SELECT *
    FROM some_table
  ) TO STDOUT
  WITH CSV DELIMITER ','
  HEADER;
`))

console.log(csvStream.writable, csvStream.readable) // true, true

Most of the libraries out there rely on those flags, I have to manually set it to
csvStream.writable = false to make it work!

Hello @killianC thanks for the report.
Indeed under the hood the copyTo object is currently a Transform stream. It is writable because the pg connection is piped into it.

What do you mean by "Most of the libraries out there rely on those flags" ?

Reading the node.js doc on https://nodejs.org/api/stream.html#stream_writable_writable I would not think that forcing csvStream.writable = false is a safe thing to do.

feel free to reopen if needed