UNPKG

@clickup/ent-framework

Version:

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

75 lines 3.31 kB
import type { Client } from "../abstract/Client"; import type { Cluster } from "../abstract/Cluster"; import type { Schema } from "../abstract/Schema"; import type { FieldOfIDTypeRequired, Table } from "../types"; import type { ShardAffinity } from "./ShardAffinity"; import type { VC } from "./VC"; /** * 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). */ export declare class Inverse<TClient extends Client, TTable extends Table> { private cluster; private shardAffinity; private name; private inverseSchema; readonly id2Field: FieldOfIDTypeRequired<TTable>; readonly type: string; constructor({ cluster, shardAffinity, id2Schema, id2Field, name, type, }: { cluster: Cluster<TClient>; shardAffinity: ShardAffinity<string>; id2Schema: Schema<TTable>; id2Field: FieldOfIDTypeRequired<TTable>; name: string; type: string; }); /** * 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. */ beforeInsert(vc: VC, id1: string | null, id2: string): Promise<boolean>; /** * Runs after a row was updated in the main schema. */ afterUpdate(vc: VC, id1: string | null, id2: string, oldID1: string | null): Promise<void>; /** * Runs after a row was deleted in the main schema. */ afterDelete(vc: VC, id1: string | null, id2: string): Promise<void>; /** * Returns all id2s by a particular (id1, type) pair. The number of resulting * rows is limited to not overload the database. */ id2s(vc: VC, id1: string | null): Promise<string[]>; /** * 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). */ private static buildInverseSchema; /** * 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). */ private id2ShardIsInferrableFromShardAffinity; /** * A shortcut to run a query on the Shard of id1. */ private run; /** * Returns a target Shard for an id. */ private shard; } //# sourceMappingURL=Inverse.d.ts.map