@clickup/ent-framework
Version:
A PostgreSQL graph-database-alike library with microsharding and row-level security
116 lines • 5.53 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigMixin = ConfigMixin;
const compact_1 = __importDefault(require("lodash/compact"));
const misc_1 = require("../../internal/misc");
const types_1 = require("../../types");
const Configuration_1 = require("../Configuration");
const Inverse_1 = require("../Inverse");
const ShardAffinity_1 = require("../ShardAffinity");
const ShardLocator_1 = require("../ShardLocator");
const Triggers_1 = require("../Triggers");
const Validation_1 = require("../Validation");
/**
* Modifies the passed class adding support for Ent configuration (such as:
* Cluster, table schema, privacy rules, triggers etc.).
*/
function ConfigMixin(Base, cluster, schema) {
class ConfigMixin extends Base {
static get SHARD_AFFINITY() {
Object.defineProperty(this, "SHARD_AFFINITY", {
value: this.configure().shardAffinity,
writable: false,
});
return this.SHARD_AFFINITY;
}
static get SHARD_LOCATOR() {
Object.defineProperty(this, "SHARD_LOCATOR", {
value: new ShardLocator_1.ShardLocator({
cluster,
entName: this.name,
shardAffinity: this.SHARD_AFFINITY,
uniqueKey: schema.uniqueKey,
inverses: this.INVERSES,
}),
writable: false,
});
return this.SHARD_LOCATOR;
}
static get VALIDATION() {
const cfg = this.configure();
Object.defineProperty(this, "VALIDATION", {
value: new Validation_1.Validation(this.name, {
tenantPrincipalField: cfg.privacyTenantPrincipalField,
inferPrincipal: async (vc, row) => {
const res = typeof cfg.privacyInferPrincipal === "function"
? await cfg.privacyInferPrincipal(vc, row)
: cfg.privacyInferPrincipal;
const lowerVC = typeof res === "string"
? vc.toLowerInternal(res)
: res === null
? vc.toGuest()
: res.vc;
if (lowerVC.isOmni()) {
throw Error(`It is prohibited to return an omni VC "${lowerVC.toString()}" from ${this.name} privacyInferPrincipal callback. Loading VC was: "${vc.toString()}".`);
}
else {
return lowerVC;
}
},
load: cfg.privacyLoad,
insert: cfg.privacyInsert,
update: cfg.privacyUpdate,
delete: cfg.privacyDelete,
validate: cfg.validators,
}),
writable: false,
});
return this.VALIDATION;
}
static get TRIGGERS() {
const cfg = this.configure();
Object.defineProperty(this, "TRIGGERS", {
value: new Triggers_1.Triggers(cfg.beforeInsert ?? [], (cfg.beforeUpdate ?? []).map((trigger) => trigger instanceof Array ? trigger : [null, trigger]), cfg.beforeDelete ?? [], (cfg.beforeMutation ?? []).map((trigger) => trigger instanceof Array ? trigger : [null, trigger]), cfg.afterInsert ?? [], (cfg.afterUpdate ?? []).map((trigger) => trigger instanceof Array ? trigger : [null, trigger]), cfg.afterDelete ?? [], (cfg.afterMutation ?? []).map((trigger) => trigger instanceof Array ? trigger : [null, trigger])),
writable: false,
});
return this.TRIGGERS;
}
static get INVERSES() {
const cfg = this.configure();
Object.defineProperty(this, "INVERSES", {
value: (0, compact_1.default)((0, misc_1.entries)(cfg.inverses ?? {}).map(([field, { name, type }]) => {
if (this.SHARD_AFFINITY === ShardAffinity_1.GLOBAL_SHARD) {
throw Error(`It's useless to define a ${field} inverse for GLOBAL_SHARD schemas; use just a DB index`);
}
const spec = schema.table[field];
if (schema.table[field].type !== types_1.ID ||
spec.autoInsert ||
spec.autoUpdate) {
throw Error(`To have inverse specified, the '${field}' must be of type ${types_1.ID} and have no autoInsert/autoUpdate`);
}
return new Inverse_1.Inverse({
cluster,
shardAffinity: this.SHARD_AFFINITY,
id2Schema: schema,
id2Field: field,
name,
type,
});
})),
writable: false,
});
return this.INVERSES;
}
static configure() {
throw Error(`Please define ${this.name}.configure() method`);
}
}
ConfigMixin.Configuration = Configuration_1.Configuration;
ConfigMixin.CLUSTER = cluster;
ConfigMixin.SCHEMA = schema;
return ConfigMixin;
}
//# sourceMappingURL=ConfigMixin.js.map
;