@clickup/ent-framework
Version:
A PostgreSQL graph-database-alike library with microsharding and row-level security
41 lines • 1.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShardNamer = void 0;
/**
* Client-specific logic on how to synchronously convert an ID into Shard number
* (only for the use cases when ID is prefixed with a Shard number), how to
* build Shard names, and how to extract Shard number from a Shard name.
*/
class ShardNamer {
/**
* Initializes an instance of ShardNamer.
*/
constructor(options) {
this.options = options;
const match = this.shardNameByNo(0).match(/(\d+)/);
this.shardNoPadLen = match ? match[1].length : 0;
if (!this.shardNoPadLen) {
throw Error("Invalid nameFormat value");
}
}
/**
* Converts a Shard name to Shard number. Returns null if it's not a correct
* Shard name.
*/
shardNoByName(name) {
const match = name?.match(/(\d+)/);
const no = match ? parseInt(match[1]) : null;
return no !== null && name === this.shardNameByNo(no) ? no : null;
}
/**
* Builds the Shard name (e.g. for PG, "schema name") by Shard number using
* `ShardNamerOptions#nameFormat`.
*
* E.g. nameFormat="sh%04d" generates names like "sh0042".
*/
shardNameByNo(no) {
return this.options.nameFormat.replace(/%(0?)(\d+)[sd]/, (_, zero, d) => no.toString().padStart(zero ? parseInt(d) : 0, "0"));
}
}
exports.ShardNamer = ShardNamer;
//# sourceMappingURL=ShardNamer.js.map
;