UNPKG

@clickup/ent-framework

Version:

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

141 lines 6.02 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Inverse = void 0; const fast_typescript_memoize_1 = require("fast-typescript-memoize"); const misc_1 = require("../internal/misc"); const types_1 = require("../types"); const ShardAffinity_1 = require("./ShardAffinity"); /** * No DB unique indexes can include a nullable field and be really unique, so we * simulate id1=NULL via just storing "0" in the Inverse, and Inverse abstracts * this fact from the caller. */ const ZERO_NULL = "0"; /** * Represents an Inverse assoc manager which knows how to modify/query Inverses. * Parameter `name` is the Inverse's schema name (in relational databases, most * likely a table name), and `type` holds both the name of the "parent" entity * and the field name of the child (e.g. "org2users" when a field "org_id" in * EntUser refers an EntOrg row). */ class Inverse { constructor({ cluster, shardAffinity, id2Schema, id2Field, name, type, }) { this.cluster = cluster; this.shardAffinity = shardAffinity; this.inverseSchema = Inverse.buildInverseSchema(id2Schema, name); this.id2Field = id2Field; this.name = name; this.type = type; } /** * Runs before a row with a pre-generated id2 was inserted to the main schema. * Returns true if the Inverse row was actually inserted in the DB. */ async beforeInsert(vc, id1, id2) { if (this.id2ShardIsInferrableFromShardAffinity(id1)) { return false; } const id = await this.run(vc, this.shard(id1), this.inverseSchema.insert({ type: this.type, id1: id1 ?? ZERO_NULL, id2, })); return id !== null; } /** * Runs after a row was updated in the main schema. */ async afterUpdate(vc, id1, id2, oldID1) { if (id1 === oldID1) { return; } await (0, misc_1.join)([ this.afterDelete(vc, oldID1, id2), this.beforeInsert(vc, id1, id2), ]); } /** * Runs after a row was deleted in the main schema. */ async afterDelete(vc, id1, id2) { if (this.id2ShardIsInferrableFromShardAffinity(id1)) { return; } const row = await this.run(vc, this.shard(id1), this.inverseSchema.loadBy({ type: this.type, id1: id1 ?? ZERO_NULL, id2, })); if (row) { await this.run(vc, this.shard(id1), this.inverseSchema.delete(row[types_1.ID])); } } /** * Returns all id2s by a particular (id1, type) pair. The number of resulting * rows is limited to not overload the database. */ async id2s(vc, id1) { const rows = await this.run(vc, this.shard(id1), this.inverseSchema.selectBy({ type: this.type, id1: id1 ?? ZERO_NULL, })); return rows.map((row) => row.id2).sort(); } /** * Creates an Inverse schema which derives its id field's autoInsert from the * passed id2 schema. The returned schema is heavily cached, so batching for * it works efficiently even for different id2 schemas and different Inverse * types (actually, it would work the same way even without `@Memoize` since * Runner batches by schema hash, not by schema object instance, but anyways). */ static buildInverseSchema(id2Schema, name) { return new id2Schema.constructor(name, { id: { type: types_1.ID, autoInsert: id2Schema.table[types_1.ID].autoInsert }, created_at: { type: Date, autoInsert: "now()" }, type: { type: String }, id1: { type: types_1.ID }, id2: { type: types_1.ID }, }, ["type", "id1", "id2"]); } /** * If the field is already mentioned in shardAffinity, and the referred parent * object (id1) exists, we won't need to create an Inverse, because the engine * will be able to infer the target Shard from shardAffinity. This method * would return true in such a case. In fact, we could've still create an * Inverse for this case, but in sake of keeping the database lean, we don't * do it (useful when a field holds a reference to an "optionally sharded" * Ent, like sometimes it point so an Ent which is sharded, and sometimes on * an Ent in the global Shard). */ id2ShardIsInferrableFromShardAffinity(id1) { return (id1 !== null && this.cluster.shard(id1) !== this.cluster.globalShard() && this.shardAffinity !== ShardAffinity_1.GLOBAL_SHARD && this.shardAffinity.includes(this.id2Field)); } /** * A shortcut to run a query on the Shard of id1. */ async run(vc, shard, query) { return shard.run(query, vc.toAnnotation(), vc.timeline(shard, `${this.name}:${this.type}`), vc.freshness); } /** * Returns a target Shard for an id. */ shard(id) { // id1=NULL Inverse is always put to the global Shard. return id ? this.cluster.shard(id) : this.cluster.globalShard(); } } exports.Inverse = Inverse; __decorate([ (0, fast_typescript_memoize_1.Memoize)((id2Schema, name) => id2Schema.table[types_1.ID].autoInsert + name) // eslint-disable-next-line @typescript-eslint/explicit-function-return-type ], Inverse, "buildInverseSchema", null); //# sourceMappingURL=Inverse.js.map