UNPKG

@clickup/pg-microsharding

Version:
70 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.chooseDsns = chooseDsns; const names_1 = require("./names"); const normalizeDsn_1 = require("./normalizeDsn"); /** * From the list of existingDsns, choose the ones that match the prefixes. * - When existingDsns is empty, when multiple entries in existingDsns match * some prefix, or when no existingDsns entries match some prefix, then tries * to treat that prefix as DSNs itself (and throws if it cannot). * - If requireMatch=true, each pattern must match exactly one existing DSN. */ function chooseDsns({ patterns, existingDsns, requireMatch, }) { existingDsns = existingDsns.map((dsn) => (0, normalizeDsn_1.normalizeDsn)(dsn)); const dsns = []; const warnings = []; for (const pattern of patterns) { let normalizedPattern = null; try { normalizedPattern = (0, normalizeDsn_1.normalizeDsn)(pattern); } catch (e) { if (typeof e !== "string") { throw e; } } const matched = normalizedPattern && existingDsns.some((dsn) => dsn === normalizedPattern) ? [normalizedPattern] : existingDsns.filter((dsn) => { if (pattern.match(/^\d+$/)) { return (0, names_1.dsnShort)(dsn).match(new RegExp(`(\\D|^)0*${pattern}(\\D|$)`)); } else { return ((0, names_1.dsnShort)(dsn).startsWith(pattern) || (normalizedPattern && (0, names_1.dsnShort)(dsn) === (0, names_1.dsnShort)(normalizedPattern))); } }); if (matched.length === 0) { if (requireMatch) { throw `No DSNs match "${pattern}".`; } try { dsns.push((0, normalizeDsn_1.normalizeDsn)(pattern)); } catch (e) { throw typeof e === "string" ? `No DSNs match "${pattern}". ${e}` : e; } } else if (matched.length === 1) { dsns.push(matched[0]); } else { if (requireMatch) { throw `Multiple DSNs match "${pattern}".`; } warnings.push(`Hint: multiple DSNs match "${pattern}", so treating it as an entire DSN.`); try { dsns.push((0, normalizeDsn_1.normalizeDsn)(pattern)); } catch (e) { throw typeof e === "string" ? `Multiple DSNs match "${pattern}". ${e}` : e; } } } return [dsns, warnings]; } //# sourceMappingURL=chooseDsns.js.map