AK-21/Graphite-Industrial-Intelligence
0
1import { entityKind } from "../entity.js";2import { TableName } from "../table.utils.js";3class ForeignKeyBuilder {4 static [entityKind] = "SQLiteForeignKeyBuilder";5 /** @internal */6 reference;7 /** @internal */8 _onUpdate;9 /** @internal */10 _onDelete;11 constructor(config, actions) {12 this.reference = () => {13 const { name, columns, foreignColumns } = config();14 return { name, columns, foreignTable: foreignColumns[0].table, foreignColumns };15 };16 if (actions) {17 this._onUpdate = actions.onUpdate;18 this._onDelete = actions.onDelete;19 }20 }21 onUpdate(action) {22 this._onUpdate = action;23 return this;24 }25 onDelete(action) {26 this._onDelete = action;27 return this;28 }29 /** @internal */30 build(table) {31 return new ForeignKey(table, this);32 }33}34class ForeignKey {35 constructor(table, builder) {36 this.table = table;37 this.reference = builder.reference;38 this.onUpdate = builder._onUpdate;39 this.onDelete = builder._onDelete;40 }41 static [entityKind] = "SQLiteForeignKey";42 reference;43 onUpdate;44 onDelete;45 getName() {46 const { name, columns, foreignColumns } = this.reference();47 const columnNames = columns.map((column) => column.name);48 const foreignColumnNames = foreignColumns.map((column) => column.name);49 const chunks = [50 this.table[TableName],51 ...columnNames,52 foreignColumns[0].table[TableName],53 ...foreignColumnNames54 ];55 return name ?? `${chunks.join("_")}_fk`;56 }57}58function foreignKey(config) {59 function mappedConfig() {60 if (typeof config === "function") {61 const { name, columns, foreignColumns } = config();62 return {63 name,64 columns,65 foreignColumns66 };67 }68 return config;69 }70 return new ForeignKeyBuilder(mappedConfig);71}72export {73 ForeignKey,74 ForeignKeyBuilder,75 foreignKey76};77//# sourceMappingURL=foreign-keys.js.map