UNPKG

drizzle-orm

Version:

Drizzle ORM package for SQL databases

77 lines 1.92 kB
import { entityKind } from "../entity.js"; import { TableName } from "../table.utils.js"; class ForeignKeyBuilder { static [entityKind] = "SQLiteForeignKeyBuilder"; /** @internal */ reference; /** @internal */ _onUpdate; /** @internal */ _onDelete; constructor(config, actions) { this.reference = () => { const { name, columns, foreignColumns } = config(); return { name, columns, foreignTable: foreignColumns[0].table, foreignColumns }; }; if (actions) { this._onUpdate = actions.onUpdate; this._onDelete = actions.onDelete; } } onUpdate(action) { this._onUpdate = action; return this; } onDelete(action) { this._onDelete = action; return this; } /** @internal */ build(table) { return new ForeignKey(table, this); } } class ForeignKey { constructor(table, builder) { this.table = table; this.reference = builder.reference; this.onUpdate = builder._onUpdate; this.onDelete = builder._onDelete; } static [entityKind] = "SQLiteForeignKey"; reference; onUpdate; onDelete; getName() { 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`; } } function foreignKey(config) { function mappedConfig() { if (typeof config === "function") { const { name, columns, foreignColumns } = config(); return { name, columns, foreignColumns }; } return config; } return new ForeignKeyBuilder(mappedConfig); } export { ForeignKey, ForeignKeyBuilder, foreignKey }; //# sourceMappingURL=foreign-keys.js.map