Skip to content
NewMulti-tenancy and row-level security (RLS) in TypeScript6 min read

Inheritance

An entity inherits what the class it extends declares, abstract or concrete, so shared columns are declared once:

import { defineEntity, f } from 'uql-orm';
import { v7 as uuidv7 } from 'uuid';
export const BaseEntity = defineEntity({
abstract: true,
name: 'BaseEntity',
fields: {
id: f.string().id().onInsert(uuidv7),
createdAt: f
.column('timestamptz')
.nullable()
.onInsert(() => new Date()),
updatedAt: f
.column('timestamptz')
.nullable()
.onUpdate(() => new Date()),
},
});
export const Firm = defineEntity({
name: 'Firm',
table: 'firm',
extends: BaseEntity,
fields: {
name: f.string().nullable().length(150),
},
});
export interface Firm extends InstanceType<typeof Firm> {}

These timestamps come from the app clock, on uql’s writes only. To take them from the database clock on every write, see timestamps.

A child inherits the base’s fields, relations, hooks, filters, indexes, checks, triggers and wire, a child declaring none taking its base’s. They are typed and queried like its own, and a change to the base reaches every child. The base’s entries come first, and a child entry with the same name replaces the base’s. A base that is never a table says abstract: true, whether it declares indexes, filters, checks or triggers for its children or only fields. A query or write on such a base does not compile. A decorated base of fields alone needs no @Entity(), and one that is never a table is an abstract class with @Entity({ abstract: true }), which is what the compiler reads. A child may add nothing of its own, which gives a second table of the same shape:

import { defineEntity, f, type QuerySql } from 'uql-orm';
declare function logInsert(): QuerySql;
export const Tenanted = defineEntity({
abstract: true,
name: 'Tenanted',
fields: {
tenantId: f.string().nullable(),
},
})
.indexes((tenanted, index) => [index([tenanted.tenantId])])
.triggers((_tenanted, trigger) => [
trigger({
on: 'afterInsert',
name: 'audit',
run: logInsert,
}),
]);
export const Invoice = defineEntity({
name: 'Invoice',
extends: Tenanted,
fields: {},
}); // gets its own tenant index and audit trigger
export interface Invoice extends InstanceType<typeof Invoice> {}

Each table derives its own index and trigger names. An index you name yourself keeps that name in every child, and PostgreSQL and SQLite need index names unique across tables, so leave a shared index unnamed.

Only a decorated class can replace an inherited key: a decorated child that declares its own .id() column replaces the parent’s key entirely (every column of it, if it was composite) and keeps every other inherited field. In defineEntity the child still reads the base’s id. Name the new key with the idKey brand, or TypeScript still reads the inherited id as the key. The decorator form is in Entities:

import { f, Entity, Field, idKey } from 'uql-orm';
import { v7 as uuidv7 } from 'uuid';
class Keyed {
@Field(f.string().id().onInsert(uuidv7))
id?: string;
@Field(f.column('timestamptz').onInsert(() => new Date())) createdAt?: Date;
}
@Entity()
export class TaxCategory extends Keyed {
[idKey]?: 'pk';
@Field(f.string().id().onInsert(uuidv7)) pk?: string;
@Field(f.string().nullable()) name?: string | null;
}

defineEntity names the class it inherits from as extends, and the class it declares extends that one, so it merges the same way. The base can be another defineEntity class or a class whose fields are decorated, which has no table of its own:

import { f, defineEntity, Field } from 'uql-orm';
class Audited {
@Field(f.string().nullable()) createdBy?: string | null;
}
export const Faq = defineEntity({
name: 'faq',
extends: Audited,
fields: { id: f.column('bigint').id(), question: f.text().nullable() },
});

The row type includes the base’s properties, so createdBy is read and written like the entity’s own columns.