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 | 131x 131x 131x 792x 792x 792x 792x 792x 1x 1x 7x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x | import validatorJs from 'validator';
import { Association } from '../associations/association.ts';
import { SqlLiteral } from '../ddl/sql-literal.ts';
import { and, or } from '../expressions/logic.ts';
import { fn, type Fn } from '../expressions/methods.ts';
import type { QueryOptionsBase } from '../model/runtime.ts';
import { QueryTypes } from '../query/query-result-shaper.ts';
import type { QueryInterface } from '../query-interface/query-interface.ts';
import type { SequelizeBase } from './sequelize-base.ts';
import type { SequelizeConstructor } from './sequelize-constructor.ts';
const literals = new SqlLiteral();
/**
* Small Sequelize v6 instance members: `queryInterface`, `random`, `escape`, `validate`, `log`,
* `getDatabaseName`, `refreshTypes` and the `and` / `or` / `QueryTypes` / `Association` / `Validator` aliases.
*
* @internal
*/
// oxlint-disable-next-line max-lines-per-function, typescript/explicit-module-boundary-types -- a mixin factory's body is its class (each method is still size-checked); its type can only be inferred.
export const SequelizeHelpers = <TBase extends SequelizeConstructor>(Base: TBase) =>
class HelpersCapability extends Base {
/** Result shapes of `query`, alias of {@link QueryTypes}. */
readonly QueryTypes = QueryTypes;
/** Base class of the association classes. */
readonly Association = Association;
/** The `validator` library used by the built-in validators. */
readonly Validator = validatorJs;
/** Combines conditions with `AND`. */
readonly and = and;
/** Combines conditions with `OR`. */
readonly or = or;
/**
* Schema API of this instance (Sequelize v6 property; same object as `getQueryInterface()`).
*
* @returns The QueryInterface.
*/
get queryInterface(): QueryInterface {
return (this as unknown as SequelizeBase).getQueryInterface();
}
/**
* SQL `RANDOM()` call, e.g. for `order: [sequelize.random()]`.
*
* @returns A `fn('RANDOM')` expression.
*
* @example
* ```ts
* const sample = await User.findAll({ order: sequelize.random(), limit: 5 });
* ```
*/
random(): Fn {
return fn('RANDOM');
}
/**
* Renders a value as a PostgreSQL literal, e.g. `'O''Brien'`. Prefer bound parameters for queries.
*
* @param value - String, number, boolean, `null`, `Date`, array or JSON value.
* @returns SQL literal text.
*
* @example
* ```ts
* const sql = `SELECT ${sequelize.escape("O'Brien")}`; // SELECT 'O''Brien'
* ```
*/
escape(value: unknown): string {
return literals.escape(value);
}
/**
* Alias of `authenticate`: checks that the database is reachable.
*
* @param options - Query options.
* @returns Resolves when the database is reachable.
* @throws ConnectionError and its subclasses when the connection cannot be established.
*
* @example
* ```ts
* await sequelize.validate();
* ```
*/
validate(options: QueryOptionsBase = {}): Promise<void> {
return (this as unknown as SequelizeBase).authenticate(options);
}
/**
* Name of the connected database.
*
* @returns Database name from the connection settings.
*
* @example
* ```ts
* const name = sequelize.getDatabaseName();
* ```
*/
getDatabaseName(): string | undefined {
return (this as unknown as SequelizeBase).config.database;
}
/**
* Accepted for compatibility; postgrelize reads type information from each result, so there is nothing to refresh.
*
* @example
* ```ts
* sequelize.refreshTypes();
* ```
*/
refreshTypes(): void {}
/**
* Passes a message to the configured `logging` function (or to the `logging` option of the last argument, as in v6).
*
* @param args - Message parts; an options object with `logging` may come last.
*
* @example
* ```ts
* sequelize.log('Cache warmed up');
* ```
*/
log(...args: unknown[]): void {
const last = args.at(-1) as { logging?: unknown; benchmark?: boolean } | undefined;
const own = typeof last === 'object' && last !== null && 'logging' in last;
const options = own ? last : (this as unknown as SequelizeBase).options;
Iif (own && options.logging === console.log) {
args.pop();
}
const logging = options.logging === true ? console.log : options.logging;
if (typeof logging === 'function') {
const benchmark =
options.benchmark === true ||
(this as unknown as SequelizeBase).options.benchmark === true;
(logging as (...messages: unknown[]) => void)(
...(benchmark && logging === console.log
? [`${args[0]} Elapsed time: ${args[1]}ms`]
: args),
);
}
}
};
|