| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { entityKind } from "../entity.js";
- import { TableName } from "../table.utils.js";
- class ForeignKeyBuilder {
- static [entityKind] = "MySqlForeignKeyBuilder";
- /** @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] = "MySqlForeignKey";
- 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() {
- const { name, columns, foreignColumns } = config;
- return {
- name,
- columns,
- foreignColumns
- };
- }
- return new ForeignKeyBuilder(mappedConfig);
- }
- export {
- ForeignKey,
- ForeignKeyBuilder,
- foreignKey
- };
- //# sourceMappingURL=foreign-keys.js.map
|