All files / src/model runtime.ts

100% Statements 1/1
100% Branches 0/0
100% Functions 0/0
100% Lines 1/1

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                                                                                                                                                                                      131x                  
import type { QueryResult } from '../connection/connection-manager.ts';
import type { LoggingFunction, ModelDefaults } from '../connection/options.ts';
import type { RetryOptions } from '../connection/retry.ts';
import type { Replacements } from '../query/raw-query.ts';
import type { CompiledQuery } from '../sql/compile.ts';
import type { HookRegistry } from '../hooks/hooks.ts';
import type { QueryInterface } from '../query-interface/query-interface.ts';
import type { Transaction, TransactionOptions } from '../transaction/transaction.ts';
 
/** Options common to every query issued by a model. */
export type QueryOptionsBase = {
  /** Transaction to run in; `null` forces running outside the CLS transaction. */
  transaction?: Transaction | null;
  /** SQL logger for this query, or `false`. */
  logging?: LoggingFunction | false;
  /** Pass query duration to the logger. */
  benchmark?: boolean;
  /**
   * `statement_timeout` for this query only, in milliseconds (non-negative integer; `0` disables it).
   * PostgreSQL cancels the statement with a `DatabaseError` (code `57014`) when it runs longer.
   * Outside a transaction the query runs in its own short transaction; inside one the previous
   * timeout is restored afterwards. PostgreSQL extension.
   * @postgrelize
   */
  statementTimeout?: number;
  /**
   * Retries this query when it fails, merged over the instance `retry` option (Sequelize v6). The
   * connection acquisition is part of the retried work; a query that runs in a transaction is never
   * retried.
   */
  retry?: RetryOptions;
  /**
   * Force the writer even for SELECTs when read replicas are configured. postgrelize extension.
   * @postgrelize
   */
  useMaster?: boolean;
  /**
   * Values of `?` (array) or `:name` (object) placeholders written in raw SQL inside the options
   * (`where: literal('"a" = :a')`, `attributes: [[literal(...), 'x']]`), as in Sequelize v6. Every
   * value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a
   * comma separated list of parameters.
   */
  replacements?: Replacements;
};
 
/** Services a `Sequelize` instance provides to its models. */
export type ModelRuntime = Readonly<{
  execute: (
    query: CompiledQuery,
    options: QueryOptionsBase,
    kind?: 'read' | 'write',
  ) => Promise<QueryResult>;
  transaction: <T>(
    run: (transaction: Transaction) => Promise<T>,
    parent?: Transaction | null,
  ) => Promise<T>;
  register: (name: string, model: object) => void;
  /** Defines a model through the owning Sequelize instance (used for implicit through models). */
  defineModel: (
    name: string,
    attributes: Readonly<Record<string, unknown>>,
    options: Readonly<Record<string, unknown>>,
  ) => object;
  /** Schema API of the owning Sequelize instance. */
  queryInterface: () => QueryInterface;
  /** Streams encoded lines into `COPY ... FROM STDIN`. */
  copyFrom: (
    text: string,
    lines: AsyncIterable<string>,
    options: QueryOptionsBase,
  ) => Promise<number>;
  /** Streams `COPY ... TO STDOUT` output. */
  copyTo: (
    text: string,
    options: QueryOptionsBase,
    kind?: 'read' | 'write',
  ) => AsyncGenerator<Buffer, void, undefined>;
  /** Starts an unmanaged transaction. */
  beginTransaction: (options?: TransactionOptions) => Promise<Transaction>;
  /** `server_version_num` of the database (cached). */
  serverVersion: () => Promise<number>;
  /** Registered models by name. */
  models: () => Readonly<Record<string, object>>;
  hooks: () => HookRegistry;
  defaults: ModelDefaults;
  inAsAnyThreshold: number | undefined;
  /** `minifyAliases` option: shorten include aliases. */
  minifyAliases?: boolean | undefined;
}>;
 
/** @internal */
export const runtimeKey: unique symbol = Symbol('postgrelize.runtime');
 
/** Owner of the internal model services: the `Sequelize` instance. */
// An interface (not a type alias) so declaration files reference it by name instead of inlining
// the internal symbol key.
export interface RuntimeOwner {
  /** Internal services used by models. */
  readonly [runtimeKey]: ModelRuntime;
}