Skip to content

[pg-query-stream] Allow 'destroy' option for QueryStream #3546

@dmurvihill

Description

@dmurvihill

Since I'm using QueryStream for a one-off task, I'd like to wrap it with a function that manages the connection for me.

export function streamAllRows(
  config: ClientConfig,
  query: string,
): stream.Readable;

However, client code needs to wait not just for the QueryStream to close, but also for the managed connection and client to be fully closed and ended.

My solution was to use the destroy argument to stream.PassThrough:

export async function streamAllRows(
  config: ClientConfig,
  query: string,
): Promise<stream.Readable> {
  const client = new pg.Client(config);
  await client.connect();
  return client.query(new QueryStream(query))
    .pipe(new stream.PassThrough({
      objectMode: true,
      destroy: (e: Error | null, callback) => {
        void client.end().then(() => { callback(); }).catch(callback);
      },
    }));
}

This works well enough, but creates an extra object and seems unnecessarily circuitous. It would be more expressive to accept the construct and destroy arguments and forward them to super().

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions