-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Open
Labels
Description
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().