UNPKG

@clickup/ent-framework

Version:

A PostgreSQL graph-database-alike library with microsharding and row-level security

53 lines 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PgShardNamer = void 0; const ShardError_1 = require("../abstract/ShardError"); const ShardNamer_1 = require("../abstract/ShardNamer"); const misc_1 = require("../internal/misc"); const parseCompositeRow_1 = require("./internal/parseCompositeRow"); /** * ShardNamer implementation for PG. */ class PgShardNamer extends ShardNamer_1.ShardNamer { /** * Synchronously extracts Shard number from an ID. Can also extract from PG * composite rows (to support composite IDs). */ shardNoByID(id) { // Composite ID: `(100008888888,1023499999999)` - try extracting non-zero // Shard from parts (left to right) first, and if there is none, allow shard // zero too. if (typeof id === "string" && id.startsWith("(") && id.endsWith(")")) { let no = NaN; for (const subID of (0, parseCompositeRow_1.parseCompositeRow)(id)) { const tryNo = subID && subID.length >= this.shardNoPadLen + 1 ? parseInt(subID.substring(1, this.shardNoPadLen + 1)) : NaN; if (!isNaN(tryNo)) { if (tryNo > 0) { return tryNo; } else if (isNaN(no)) { no = tryNo; } } } if (isNaN(no)) { const idSafe = (0, misc_1.sanitizeIDForDebugPrinting)(id); throw Error(`Cannot extract shard number from the composite ID ${idSafe}`); } return no; } // Plain ID. const no = typeof id === "string" && id.length >= this.shardNoPadLen + 1 ? parseInt(id.substring(1, this.shardNoPadLen + 1)) : NaN; if (isNaN(no)) { const idSafe = (0, misc_1.sanitizeIDForDebugPrinting)(id); throw new ShardError_1.ShardError(`Cannot parse ID ${idSafe} to detect shard number`); } return no; } } exports.PgShardNamer = PgShardNamer; //# sourceMappingURL=PgShardNamer.js.map