Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 387x 387x 609x 91x 74x 460x 460x 92x 1x | import type {
CreateFunctionOptions,
DropFunctionOptions,
FunctionName,
FunctionParam,
} from '../ddl/function-ddl.ts';
import type { TableName } from '../ddl/sql-literal.ts';
import type {
CreateTriggerOptions,
DropTriggerOptions,
TriggerArgument,
TriggerEvent,
TriggerTiming,
} from '../ddl/trigger-ddl.ts';
import type { QueryOptionsBase, RuntimeOwner } from '../model/runtime.ts';
import { QueryInterfaceObjects } from './query-interface-objects.ts';
import { RoutineCommands } from './routine-commands.ts';
import { StatementRunner } from './statement-runner.ts';
/**
* Function and trigger part of {@link QueryInterface}.
*/
export class QueryInterfaceRoutines extends QueryInterfaceObjects {
readonly #routines: RoutineCommands;
/**
* @param sequelize - Owning `Sequelize` instance.
*/
constructor(sequelize: RuntimeOwner) {
super(sequelize);
this.#routines = new RoutineCommands(new StatementRunner(sequelize));
}
/**
* Creates a function with `CREATE [OR REPLACE] FUNCTION` (Sequelize v6 `createFunction`). The body is wrapped in `BEGIN ... END;` as in v6.
*
* The `body` is SQL text you provide: it is not escaped, so never build it from user input. Names, parameter names and the language are validated identifier words; types and characteristic keywords are checked against strict patterns and allowlists. A function name given as a string is unquoted (folds to lower case, like v6); use `{ name, schema }` for an exact quoted name.
*
* @param name - Function name (`'touch'`, `'app.touch'` or `{ name, schema }`).
* @param params - Parameters `{ type, name?, direction? }`; pass `[]` for none.
* @param returnType - Return type, e.g. `'trigger'`, `'integer'`, `'SETOF text'`.
* @param language - Language, e.g. `'plpgsql'`.
* @param body - Trusted function body without `BEGIN` / `END`.
* @param optionsArray - Characteristic keywords such as `'IMMUTABLE'`, `'SECURITY DEFINER'`, `'COST 10'`.
* @param options - `variables`, `force` (`OR REPLACE`) and query options.
* @throws Error when a part is missing or fails validation.
* @throws DatabaseError when PostgreSQL rejects the function.
*
* @example
* ```ts
* await queryInterface.createFunction(
* 'touch_updated_at',
* [],
* 'trigger',
* 'plpgsql',
* 'NEW."updatedAt" := now(); RETURN NEW;',
* ['VOLATILE'],
* { force: true },
* );
* ```
* @see [Schema objects guide](/guide/schema-objects)
*/
async createFunction(
name: FunctionName,
params: readonly FunctionParam[],
returnType: string,
language: string,
body: string,
optionsArray: readonly string[] = [],
options: CreateFunctionOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#routines.createFunction(
name,
params,
returnType,
language,
body,
optionsArray,
options,
);
}
/**
* Drops a function with `DROP FUNCTION ... RESTRICT` (Sequelize v6 `dropFunction`).
*
* @param name - Function name.
* @param params - Parameters that identify the overload; pass `[]` for none.
* @param options - `ifExists`, `cascade` and query options.
*
* @example
* ```ts
* await queryInterface.dropFunction('touch_updated_at', [], { ifExists: true });
* ```
*/
async dropFunction(
name: FunctionName,
params: readonly FunctionParam[],
options: DropFunctionOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#routines.dropFunction(name, params, options);
}
/**
* Renames a function with `ALTER FUNCTION ... RENAME TO` (Sequelize v6 `renameFunction`).
*
* @param name - Current function name.
* @param params - Parameters that identify the overload.
* @param newName - New unqualified name.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.renameFunction('touch_updated_at', [], 'set_updated_at');
* ```
*/
async renameFunction(
name: FunctionName,
params: readonly FunctionParam[],
newName: string,
options: QueryOptionsBase = {},
): Promise<void> {
await this.#routines.renameFunction(name, params, newName, options);
}
/**
* Creates a trigger with `CREATE TRIGGER` (Sequelize v6 `createTrigger`; `'after_constraint'` creates a `CONSTRAINT TRIGGER`).
*
* `functionArgs` are rendered as literals (v6 rendered typed parameter declarations, which PostgreSQL rejects). The `optionsArray` clauses are checked against an allowlist (`'FOR EACH ROW'`, `'FOR EACH STATEMENT'`, `'DEFERRABLE'`, `'INITIALLY DEFERRED'`, `REFERENCING ... TABLE AS name`); a `WHEN (condition)` clause is trusted SQL and is not escaped.
*
* @param table - Table name or `{ tableName, schema }`.
* @param name - Trigger name.
* @param timing - `'before'`, `'after'`, `'instead_of'` or `'after_constraint'`.
* @param events - Events that fire the trigger: `'insert'`, `'update'`, `'delete'`, `'truncate'`.
* @param functionName - Trigger function (see {@link QueryInterface.createFunction}).
* @param functionArgs - Literal arguments for the function; pass `[]` for none.
* @param optionsArray - Clauses such as `['FOR EACH ROW']`.
* @param options - `replace` (`OR REPLACE`, PostgreSQL 14+) and query options.
* @throws Error for an unknown timing, event or clause.
*
* @example
* ```ts
* await queryInterface.createTrigger('Posts', 'posts_touch', 'before', ['insert', 'update'], 'touch_updated_at', [], ['FOR EACH ROW']);
* ```
*/
async createTrigger(
table: TableName,
name: string,
timing: TriggerTiming,
events: readonly TriggerEvent[],
functionName: FunctionName,
functionArgs: readonly TriggerArgument[] = [],
optionsArray: readonly string[] = [],
options: CreateTriggerOptions & QueryOptionsBase = {},
): Promise<void> {
const spec = { table, name, timing, events, functionName, functionArgs, optionsArray };
await this.#routines.createTrigger(spec, options);
}
/**
* Drops a trigger with `DROP TRIGGER ... RESTRICT` (Sequelize v6 `dropTrigger`).
*
* @param table - Table name or `{ tableName, schema }`.
* @param name - Trigger name.
* @param options - `ifExists`, `cascade` and query options.
*
* @example
* ```ts
* await queryInterface.dropTrigger('Posts', 'posts_touch', { ifExists: true });
* ```
*/
async dropTrigger(
table: TableName,
name: string,
options: DropTriggerOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#routines.dropTrigger(table, name, options);
}
/**
* Renames a trigger with `ALTER TRIGGER ... RENAME TO` (Sequelize v6 `renameTrigger`).
*
* @param table - Table name or `{ tableName, schema }`.
* @param name - Current trigger name.
* @param newName - New trigger name.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.renameTrigger('Posts', 'posts_touch', 'posts_set_updated_at');
* ```
*/
async renameTrigger(
table: TableName,
name: string,
newName: string,
options: QueryOptionsBase = {},
): Promise<void> {
await this.#routines.renameTrigger(table, name, newName, options);
}
}
|