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

50 lines (49 loc) 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.csv = csv; exports.copyCsvIntoTable = copyCsvIntoTable; exports.exportTableToCsv = exportTableToCsv; const promises_1 = require("node:stream/promises"); const logger_1 = require("@launchql/logger"); const fs_1 = require("fs"); const pg_copy_streams_1 = require("pg-copy-streams"); const log = new logger_1.Logger('csv'); function csv(tables) { return { async seed(ctx) { for (const [table, filePath] of Object.entries(tables)) { if (!(0, fs_1.existsSync)(filePath)) { throw new Error(`CSV file not found: ${filePath}`); } log.info(`📥 Seeding "${table}" from ${filePath}`); await copyCsvIntoTable(ctx.pg, table, filePath); } } }; } async function copyCsvIntoTable(pg, table, filePath) { const client = pg.client; const stream = client.query((0, pg_copy_streams_1.from)(`COPY ${table} FROM STDIN WITH CSV HEADER`)); const source = (0, fs_1.createReadStream)(filePath); try { await (0, promises_1.pipeline)(source, stream); log.success(`✅ Successfully seeded "${table}"`); } catch (err) { log.error(`❌ COPY failed for "${table}": ${err.message}`); throw err; } } async function exportTableToCsv(pg, table, filePath) { const client = pg.client; const stream = client.query((0, pg_copy_streams_1.to)(`COPY ${table} TO STDOUT WITH CSV HEADER`)); const target = (0, fs_1.createWriteStream)(filePath); try { await (0, promises_1.pipeline)(stream, target); log.success(`✅ Exported "${table}" to ${filePath}`); } catch (err) { log.error(`❌ Failed to export "${table}": ${err.message}`); throw err; } }