UNPKG

@clickup/pg-mig

Version:

PostgreSQL schema migration tool with microsharding and clustering support

79 lines 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeDsn = normalizeDsn; /** * Accepts DSN in various formats and returns its URL-normalized version, or * throws in case some essential part of the DSN is missing. Tries to use PG* * environment variables for the missing parts. */ function normalizeDsn(dsnOrIslandNo, env = process.env, islandDsns) { var _a, _b, _c, _d; if (!dsnOrIslandNo) { return undefined; } if (islandDsns && dsnOrIslandNo.match(/^\d+$/)) { return normalizeDsn(islandDsns[parseInt(dsnOrIslandNo, 10)], env); } const errorPrefix = `Error parsing "${dsnOrIslandNo}"`; let url; const match = dsnOrIslandNo.match(/^\w+:\/\/(.*)$/); if (match) { // URL class doesn't support username/password fields for custom protocols // like "postgresql", so we pretend it's https. if (!match[1]) { // Allow passing dsn="https://" for all-env defaults. url = new URL(`https://${env["PGHOST"]}`); } else { url = new URL(`https://${match[1]}`); } } else { const parsed = parseDbHostSpec(dsnOrIslandNo); parsed.host || (parsed.host = env["PGHOST"] || ""); if (!parsed.host) { throw `${errorPrefix}: host is missing (pass it or set PGHOST environment variable)`; } url = new URL(`https://${parsed.host}`); url.port = parsed.port ? String(parsed.port) : ""; url.pathname = parsed.database ? `/${parsed.database}` : ""; url.hash = parsed.comment ? `#${parsed.comment}` : ""; } url.username || (url.username = (_a = env["PGUSER"]) !== null && _a !== void 0 ? _a : ""); url.password || (url.password = (_b = env["PGPASSWORD"]) !== null && _b !== void 0 ? _b : ""); url.port || (url.port = (_c = env["PGPORT"]) !== null && _c !== void 0 ? _c : ""); if (url.pathname === "/") { url.pathname = "/" + ((_d = env["PGDATABASE"]) !== null && _d !== void 0 ? _d : ""); } if (env.PGSSLMODE) { const params = new URLSearchParams(url.search); params.set("sslmode", env.PGSSLMODE); url.search = params.toString(); } if (!url.username) { throw `${errorPrefix}: username is missing (pass it or set PGUSER environment variable)`; } else if (!url.password) { throw `${errorPrefix}: password is missing (pass it or set PGPASSWORD environment variable)`; } else if (!url.hostname) { throw `${errorPrefix}: host is missing (pass it or set PGHOST environment variable)`; } else if (!url.pathname || url.pathname === "/") { throw `${errorPrefix}: database name is missing (pass it or set PGDATABASE environment variable)`; } return url.toString().replace(/^\w+:\/\//, "postgresql://"); } /** * Parses "host[:port][/database][#comment]" string. */ function parseDbHostSpec(hostSpec) { const { host, port, database, comment } = hostSpec.match(/^(?<host>[^:/#]+)?(?::(?<port>\d+))?(?:\/(?<database>[^#]+))?(#(?<comment>.+))?$/s).groups; return { host, port: port ? parseInt(port) : undefined, database: database || undefined, comment: comment || undefined, }; } //# sourceMappingURL=normalizeDsn.js.map