pgsql-test
Version:
pgsql-test offers isolated, role-aware, and rollback-friendly PostgreSQL environments for integration tests — giving developers realistic test coverage without external state pollution
44 lines (43 loc) • 1.17 kB
JavaScript
import { spawn } from 'child_process';
import { getSpawnEnvWithPg } from 'pg-env';
import { Readable } from 'stream';
function setArgs(config) {
const args = [
'-U', config.user,
'-h', config.host,
'-d', config.database
];
if (config.port) {
args.push('-p', String(config.port));
}
return args;
}
// Converts a string to a readable stream (replaces streamify-string)
function stringToStream(text) {
const stream = new Readable({
read() {
this.push(text);
this.push(null);
}
});
return stream;
}
export async function streamSql(config, sql) {
const args = setArgs(config);
return new Promise((resolve, reject) => {
const sqlStream = stringToStream(sql);
const proc = spawn('psql', args, {
env: getSpawnEnvWithPg(config)
});
sqlStream.pipe(proc.stdin);
proc.on('close', (code) => {
resolve();
});
proc.on('error', (error) => {
reject(error);
});
proc.stderr.on('data', (data) => {
reject(new Error(data.toString()));
});
});
}