UNPKG

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

47 lines (46 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.streamSql = streamSql; const child_process_1 = require("child_process"); const pg_env_1 = require("pg-env"); const stream_1 = require("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 stream_1.Readable({ read() { this.push(text); this.push(null); } }); return stream; } async function streamSql(config, sql) { const args = setArgs(config); return new Promise((resolve, reject) => { const sqlStream = stringToStream(sql); const proc = (0, child_process_1.spawn)('psql', args, { env: (0, pg_env_1.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())); }); }); }