UNPKG

@clickup/ent-framework

Version:

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

57 lines 2.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Schema = void 0; const object_hash_1 = __importDefault(require("object-hash")); /** * Schema is like a "table" in some database (sharded, but it's beyond the scope * of Schema). It is also a factory of Query: it knows how to build runnable * Query objects. This 2nd role is database engine specific (e.g. there might be * PgSchema, RedisSchema etc.): such composition simplifies the code and lowers * the number of abstractions. * * The set of supported Queries is opinionated and is crafted carefully to * support the minimal possible list of primitives, but at the same time, be not * too limited in the queries the DB engine can execute. */ class Schema { constructor( /** For relational databases, it's likely a table name. */ name, /** Structure of the table. */ table, /** Fields which the native unique key consists of (if any). */ uniqueKey) { this.name = name; this.table = table; this.uniqueKey = uniqueKey; if (!Object.keys(this.table).length) { throw Error("Must have at least one field"); } for (const field of this.uniqueKey) { if (this.table[field].autoUpdate) { throw Error("All fields in upsert unique key list must be non-auto-updatable"); } } // For perf reasons, simplicity and easier CAS support, we don't // identifier-escape field names in query builder code, so here, we enforce // the field names to be simple and not require any escaping. (Notice that // we DO escape table names though.) for (const field of Object.keys(this.table)) { if (!field.match(/^[_a-z][_a-z0-9]*$/)) { throw Error(`Field name must be a simple identifier, but '${field}' passed`); } } this.hash = this.name + ":" + (0, object_hash_1.default)([this.table, this.uniqueKey], { algorithm: "md5", ignoreUnknown: true, }); } } exports.Schema = Schema; //# sourceMappingURL=Schema.js.map