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.
Entity Profile
Dependency Diagram
graph LR b9d8b4e3_8c35_6c97_3af0_a74313048074["foreign-keys.ts"] b5f68e91_0c18_4699_4724_dcbb5d28898e["index.ts"] b9d8b4e3_8c35_6c97_3af0_a74313048074 --> b5f68e91_0c18_4699_4724_dcbb5d28898e 2d5c8884_973c_561c_def6_5e394ea36d1a["table.ts"] b9d8b4e3_8c35_6c97_3af0_a74313048074 --> 2d5c8884_973c_561c_def6_5e394ea36d1a 27705a9d_afe9_57dd_8c97_e52d8a67d426["entity.ts"] b9d8b4e3_8c35_6c97_3af0_a74313048074 --> 27705a9d_afe9_57dd_8c97_e52d8a67d426 05fb227e_0b02_2bf4_175f_8e70216a34a7["table.utils.ts"] b9d8b4e3_8c35_6c97_3af0_a74313048074 --> 05fb227e_0b02_2bf4_175f_8e70216a34a7 2d5c8884_973c_561c_def6_5e394ea36d1a["table.ts"] 2d5c8884_973c_561c_def6_5e394ea36d1a --> b9d8b4e3_8c35_6c97_3af0_a74313048074 ce49a5a3_688d_ddce_fd32_28428e51cae2["utils.ts"] ce49a5a3_688d_ddce_fd32_28428e51cae2 --> b9d8b4e3_8c35_6c97_3af0_a74313048074 style b9d8b4e3_8c35_6c97_3af0_a74313048074 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import { entityKind } from '~/entity.ts';
import { TableName } from '~/table.utils.ts';
import type { AnyPgColumn, PgColumn } from './columns/index.ts';
import type { PgTable } from './table.ts';
export type UpdateDeleteAction = 'cascade' | 'restrict' | 'no action' | 'set null' | 'set default';
export type Reference = () => {
readonly name?: string;
readonly columns: PgColumn[];
readonly foreignTable: PgTable;
readonly foreignColumns: PgColumn[];
};
export class ForeignKeyBuilder {
static readonly [entityKind]: string = 'PgForeignKeyBuilder';
/** @internal */
reference: Reference;
/** @internal */
_onUpdate: UpdateDeleteAction | undefined = 'no action';
/** @internal */
_onDelete: UpdateDeleteAction | undefined = 'no action';
constructor(
config: () => {
name?: string;
columns: PgColumn[];
foreignColumns: PgColumn[];
},
actions?: {
onUpdate?: UpdateDeleteAction;
onDelete?: UpdateDeleteAction;
} | undefined,
) {
this.reference = () => {
const { name, columns, foreignColumns } = config();
return { name, columns, foreignTable: foreignColumns[0]!.table as PgTable, 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: PgTable): ForeignKey {
return new ForeignKey(table, this);
}
}
export type AnyForeignKeyBuilder = ForeignKeyBuilder;
export class ForeignKey {
static readonly [entityKind]: string = 'PgForeignKey';
readonly reference: Reference;
readonly onUpdate: UpdateDeleteAction | undefined;
readonly onDelete: UpdateDeleteAction | undefined;
constructor(readonly table: PgTable, 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 PgColumn[],
> = { [Key in keyof TColumns]: AnyPgColumn<{ tableName: TTableName }> };
export function foreignKey<
TTableName extends string,
TForeignTableName extends string,
TColumns extends [AnyPgColumn<{ tableName: TTableName }>, ...AnyPgColumn<{ 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
Functions
Classes
Source
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, DatabaseDrivers subdomain.
What functions are defined in foreign-keys.ts?
foreign-keys.ts defines 2 function(s): PgColumn, 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/pg-core/foreign-keys.ts (domain: DrizzleORM, subdomain: DatabaseDrivers, directory: drizzle-orm/src/pg-core).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free