@clickup/pg-microsharding
Version:
Microshards support for PostgreSQL
53 lines • 2.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseMatchingShards = parseMatchingShards;
const difference_1 = __importDefault(require("lodash/difference"));
const sortBy_1 = __importDefault(require("lodash/sortBy"));
const discoverShards_1 = require("../../internal/discoverShards");
const names_1 = require("../../internal/names");
const normalizeDsns_1 = require("../../internal/normalizeDsns");
const actionList_1 = require("../actionList");
/**
* Extracts DSNs and the list of microshard numbers from the args.
*/
async function parseMatchingShards(args) {
var _a;
const dsns = await (0, normalizeDsns_1.normalizeDsns)(args["dsns"] || args["dsn"]);
if (dsns.length === 0) {
throw "Please provide --dsn or --dsns, DB DSN(s)";
}
let shardNos;
if ((_a = (args["shard"] || args["shards"])) === null || _a === void 0 ? void 0 : _a.match(/^(\d+(?:,\d+)*)$/)) {
shardNos = RegExp.$1.split(",").map(Number);
}
else if (args["shards"]) {
const matchingDsns = dsns.filter((dsn) => dsn.startsWith(args["shards"]) ||
(0, names_1.dsnShort)(dsn).startsWith(args["shards"]));
if (matchingDsns.length === 0) {
await (0, actionList_1.actionList)(args).catch(() => { });
throw "If you provide --shards, it must be in format: N,M,... or DSN-PREFIX";
}
shardNos = (await (0, discoverShards_1.discoverShards)({ dsns: matchingDsns })).map(({ shard }) => shard);
}
else {
await (0, actionList_1.actionList)(args).catch(() => { });
throw "Please provide --shard=N or --shards=N,M..., microshard numbers";
}
const allShards = await (0, discoverShards_1.discoverShards)({ dsns });
const matchingShards = (0, sortBy_1.default)(allShards.filter(({ shard }) => shardNos.includes(shard)), ({ shard }) => shard);
if (matchingShards.length === 0) {
throw "No microshards found";
}
const missingShards = (0, difference_1.default)(shardNos, allShards.map(({ shard }) => shard));
if (missingShards.length > 0) {
throw `Microshards ${missingShards.join(", ")} not found in any of the DSNs`;
}
return {
dsns,
matchingShards,
};
}
//# sourceMappingURL=parseMatchingShards.js.map