Skip to content

Commit

Permalink
refactor(triggers): reorganize operations triggers (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 11, 2024
1 parent ce945b5 commit 48bb57d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 102 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ export type {
} from './operations/tables';
export type {
CreateTrigger,
CreateTriggerFn,
CreateTriggerFn1,
CreateTriggerFn2,
DropTrigger,
RenameTrigger,
RenameTriggerFn,
TriggerOptions,
} from './operations/triggersTypes';
} from './operations/triggers';
export type {
AddTypeAttribute,
AddTypeValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import type { MigrationOptions } from '../types';
import { escapeValue } from '../utils';
import type { FunctionOptions } from './functions';
import { createFunction, dropFunction } from './functions';
import type { DropOptions, Name, Value } from './generalTypes';
import type {
CreateTrigger,
DropTrigger,
RenameTrigger,
TriggerOptions,
} from './triggersTypes';

export type { CreateTrigger, DropTrigger, RenameTrigger };

export function dropTrigger(mOptions: MigrationOptions): DropTrigger {
const _drop: DropTrigger = (tableName, triggerName, options = {}) => {
const { ifExists, cascade } = options;

const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const triggerNameStr = mOptions.literal(triggerName);
const tableNameStr = mOptions.literal(tableName);

return `DROP TRIGGER${ifExistsStr} ${triggerNameStr} ON ${tableNameStr}${cascadeStr};`;
};

return _drop;
}
import type { MigrationOptions } from '../../types';
import { escapeValue } from '../../utils';
import type { FunctionOptions } from '../functions';
import { createFunction, dropFunction } from '../functions';
import type { DropOptions, Name, Value } from '../generalTypes';
import { dropTrigger } from './dropTrigger';
import type { TriggerOptions } from './shared';

export type CreateTriggerFn1 = (
tableName: Name,
triggerName: string,
triggerOptions: TriggerOptions & DropOptions
) => string | string[];

export type CreateTriggerFn2 = (
tableName: Name,
triggerName: string,
triggerOptions: TriggerOptions & FunctionOptions & DropOptions,
definition: Value
) => string | string[];

export type CreateTriggerFn = CreateTriggerFn1 | CreateTriggerFn2;

export type CreateTrigger = CreateTriggerFn & { reverse: CreateTriggerFn };

export function createTrigger(mOptions: MigrationOptions): CreateTrigger {
const _create: CreateTrigger = (
Expand Down Expand Up @@ -129,22 +125,3 @@ export function createTrigger(mOptions: MigrationOptions): CreateTrigger {

return _create;
}

export function renameTrigger(mOptions: MigrationOptions): RenameTrigger {
const _rename: RenameTrigger = (
tableName,
oldTriggerName,
newTriggerName
) => {
const oldTriggerNameStr = mOptions.literal(oldTriggerName);
const tableNameStr = mOptions.literal(tableName);
const newTriggerNameStr = mOptions.literal(newTriggerName);

return `ALTER TRIGGER ${oldTriggerNameStr} ON ${tableNameStr} RENAME TO ${newTriggerNameStr};`;
};

_rename.reverse = (tableName, oldTriggerName, newTriggerName) =>
_rename(tableName, newTriggerName, oldTriggerName);

return _rename;
}
23 changes: 23 additions & 0 deletions src/operations/triggers/dropTrigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { MigrationOptions } from '../../types';
import type { DropOptions, Name } from '../generalTypes';

export type DropTrigger = (
tableName: Name,
triggerName: string,
dropOptions?: DropOptions
) => string | string[];

export function dropTrigger(mOptions: MigrationOptions): DropTrigger {
const _drop: DropTrigger = (tableName, triggerName, options = {}) => {
const { ifExists, cascade } = options;

const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const triggerNameStr = mOptions.literal(triggerName);
const tableNameStr = mOptions.literal(tableName);

return `DROP TRIGGER${ifExistsStr} ${triggerNameStr} ON ${tableNameStr}${cascadeStr};`;
};

return _drop;
}
4 changes: 4 additions & 0 deletions src/operations/triggers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './createTrigger';
export * from './dropTrigger';
export * from './renameTrigger';
export * from './shared';
29 changes: 29 additions & 0 deletions src/operations/triggers/renameTrigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { MigrationOptions } from '../../types';
import type { Name } from '../generalTypes';

export type RenameTriggerFn = (
tableName: Name,
oldTriggerName: string,
newTriggerName: string
) => string | string[];

export type RenameTrigger = RenameTriggerFn & { reverse: RenameTriggerFn };

export function renameTrigger(mOptions: MigrationOptions): RenameTrigger {
const _rename: RenameTrigger = (
tableName,
oldTriggerName,
newTriggerName
) => {
const oldTriggerNameStr = mOptions.literal(oldTriggerName);
const tableNameStr = mOptions.literal(tableName);
const newTriggerNameStr = mOptions.literal(newTriggerName);

return `ALTER TRIGGER ${oldTriggerNameStr} ON ${tableNameStr} RENAME TO ${newTriggerNameStr};`;
};

_rename.reverse = (tableName, oldTriggerName, newTriggerName) =>
_rename(tableName, newTriggerName, oldTriggerName);

return _rename;
}
21 changes: 21 additions & 0 deletions src/operations/triggers/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Name, Value } from '../generalTypes';

export interface TriggerOptions {
when?: 'BEFORE' | 'AFTER' | 'INSTEAD OF';

operation: string | string[];

constraint?: boolean;

function?: Name;

functionParams?: Value[];

level?: 'STATEMENT' | 'ROW';

condition?: string;

deferrable?: boolean;

deferred?: boolean;
}
53 changes: 0 additions & 53 deletions src/operations/triggersTypes.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type * as schemas from './operations/schemas';
import type * as sequences from './operations/sequences';
import type * as sql from './operations/sql';
import type * as tables from './operations/tables';
import type * as triggers from './operations/triggersTypes';
import type * as triggers from './operations/triggers';
import type * as types from './operations/typesTypes';
import type * as mViews from './operations/viewsMaterializedTypes';
import type * as views from './operations/viewsTypes';
Expand Down

0 comments on commit 48bb57d

Please sign in to comment.