Home / File/ foreign-keys.ts — drizzle-orm Source File

foreign-keys.ts — drizzle-orm Source File

Architecture documentation for foreign-keys.ts, a typescript file in the drizzle-orm codebase. 4 imports, 2 dependents.

File typescript DrizzleORM RelationalQuery 4 imports 2 dependents 2 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  fe0bf683_1731_529c_f1b4_81cc9db56955["foreign-keys.ts"]
  a2e1e4f4_2b6e_c197_a707_793466cb421f["index.ts"]
  fe0bf683_1731_529c_f1b4_81cc9db56955 --> a2e1e4f4_2b6e_c197_a707_793466cb421f
  cbe7af57_41be_454d_306f_02d0e6f81949["table.ts"]
  fe0bf683_1731_529c_f1b4_81cc9db56955 --> cbe7af57_41be_454d_306f_02d0e6f81949
  27705a9d_afe9_57dd_8c97_e52d8a67d426["entity.ts"]
  fe0bf683_1731_529c_f1b4_81cc9db56955 --> 27705a9d_afe9_57dd_8c97_e52d8a67d426
  05fb227e_0b02_2bf4_175f_8e70216a34a7["table.utils.ts"]
  fe0bf683_1731_529c_f1b4_81cc9db56955 --> 05fb227e_0b02_2bf4_175f_8e70216a34a7
  cbe7af57_41be_454d_306f_02d0e6f81949["table.ts"]
  cbe7af57_41be_454d_306f_02d0e6f81949 --> fe0bf683_1731_529c_f1b4_81cc9db56955
  9cb8488e_0ece_edef_af6a_5851d56cddda["utils.ts"]
  9cb8488e_0ece_edef_af6a_5851d56cddda --> fe0bf683_1731_529c_f1b4_81cc9db56955
  style fe0bf683_1731_529c_f1b4_81cc9db56955 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { entityKind } from '~/entity.ts';
import { TableName } from '~/table.utils.ts';
import type { AnyGelColumn, GelColumn } from './columns/index.ts';
import type { GelTable } from './table.ts';

export type UpdateDeleteAction = 'cascade' | 'restrict' | 'no action' | 'set null' | 'set default';

export type Reference = () => {
	readonly name?: string;
	readonly columns: GelColumn[];
	readonly foreignTable: GelTable;
	readonly foreignColumns: GelColumn[];
};

export class ForeignKeyBuilder {
	static readonly [entityKind]: string = 'GelForeignKeyBuilder';

	/** @internal */
	reference: Reference;

	/** @internal */
	_onUpdate: UpdateDeleteAction | undefined = 'no action';

	/** @internal */
	_onDelete: UpdateDeleteAction | undefined = 'no action';

	constructor(
		config: () => {
			name?: string;
			columns: GelColumn[];
			foreignColumns: GelColumn[];
		},
		actions?: {
			onUpdate?: UpdateDeleteAction;
			onDelete?: UpdateDeleteAction;
		} | undefined,
	) {
		this.reference = () => {
			const { name, columns, foreignColumns } = config();
			return { name, columns, foreignTable: foreignColumns[0]!.table as GelTable, foreignColumns };
		};
		if (actions) {
			this._onUpdate = actions.onUpdate;
			this._onDelete = actions.onDelete;
		}
	}

	onUpdate(action: UpdateDeleteAction): this {
		this._onUpdate = action === undefined ? 'no action' : action;
		return this;
	}

	onDelete(action: UpdateDeleteAction): this {
		this._onDelete = action === undefined ? 'no action' : action;
		return this;
	}

	/** @internal */
	build(table: GelTable): ForeignKey {
		return new ForeignKey(table, this);
	}
}

export type AnyForeignKeyBuilder = ForeignKeyBuilder;

export class ForeignKey {
	static readonly [entityKind]: string = 'GelForeignKey';

	readonly reference: Reference;
	readonly onUpdate: UpdateDeleteAction | undefined;
	readonly onDelete: UpdateDeleteAction | undefined;

	constructor(readonly table: GelTable, builder: ForeignKeyBuilder) {
		this.reference = builder.reference;
		this.onUpdate = builder._onUpdate;
		this.onDelete = builder._onDelete;
	}

	getName(): string {
		const { name, columns, foreignColumns } = this.reference();
		const columnNames = columns.map((column) => column.name);
		const foreignColumnNames = foreignColumns.map((column) => column.name);
		const chunks = [
			this.table[TableName],
			...columnNames,
			foreignColumns[0]!.table[TableName],
			...foreignColumnNames,
		];
		return name ?? `${chunks.join('_')}_fk`;
	}
}

type ColumnsWithTable<
	TTableName extends string,
	TColumns extends GelColumn[],
> = { [Key in keyof TColumns]: AnyGelColumn<{ tableName: TTableName }> };

export function foreignKey<
	TTableName extends string,
	TForeignTableName extends string,
	TColumns extends [AnyGelColumn<{ tableName: TTableName }>, ...AnyGelColumn<{ tableName: TTableName }>[]],
>(
	config: {
		name?: string;
		columns: TColumns;
		foreignColumns: ColumnsWithTable<TForeignTableName, TColumns>;
	},
): ForeignKeyBuilder {
	function mappedConfig() {
		const { name, columns, foreignColumns } = config;
		return {
			name,
			columns,
			foreignColumns,
		};
	}

	return new ForeignKeyBuilder(mappedConfig);
}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does foreign-keys.ts do?
foreign-keys.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the DrizzleORM domain, RelationalQuery subdomain.
What functions are defined in foreign-keys.ts?
foreign-keys.ts defines 2 function(s): GelColumn, foreignKey.
What does foreign-keys.ts depend on?
foreign-keys.ts imports 4 module(s): entity.ts, index.ts, table.ts, table.utils.ts.
What files import foreign-keys.ts?
foreign-keys.ts is imported by 2 file(s): table.ts, utils.ts.
Where is foreign-keys.ts in the architecture?
foreign-keys.ts is located at drizzle-orm/src/gel-core/foreign-keys.ts (domain: DrizzleORM, subdomain: RelationalQuery, directory: drizzle-orm/src/gel-core).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free