All files / src/connection options.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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 202                                                                                                                                                                                                                                                                                                                                                                                                                   
import type { PoolConfig } from 'pg';
import type { HooksOption } from '../hooks/hooks.ts';
import type { RetryOptions } from './retry.ts';
 
/**
 * Function receiving executed SQL. The text has the values inlined as literals, like in Sequelize v6,
 * so it can be run as it is; the statement sent to PostgreSQL keeps its bound parameters.
 *
 * With `benchmark: true` the second argument is the duration in ms; otherwise it is an object
 * `{ bind, parameterizedSql }` (v6 passes its query options there, whose typings declare the parameter
 * as `timing?: number`): `bind` holds the bound values and `parameterizedSql` the statement as it was
 * sent (`$1`, `$2`, ...), without the `Executing (...)` prefix. `parameterizedSql` is a postgrelize
 * extension.
 */
export type LoggingFunction = (sql: string, timing?: number) => void;
 
/**
 * Connection pool settings (Sequelize v6 `PoolOptions`), given as the `pool` option of {@link SequelizeOptions}.
 *
 * @example
 * ```ts
 * const pool: PoolOptions = { max: 10, idle: 10_000, acquire: 30_000 };
 * const db = new Sequelize('postgres://user:pass@localhost:5432/app', { pool });
 * ```
 */
export type PoolOptions = Readonly<{
  /** Maximum number of connections. Default 5. */
  max?: number;
  /** Minimum number of connections. Accepted for compatibility; `pg` does not keep idle minimums. */
  min?: number;
  /** Milliseconds a connection may be idle before release. Default 10000. */
  idle?: number;
  /** Milliseconds to wait for a connection before throwing `ConnectionAcquireTimeoutError`. Default 60000. */
  acquire?: number;
  /** Accepted for compatibility; eviction is handled by `idle`. */
  evict?: number;
  /** Maximum times a connection may be used before it is replaced (`pg` `maxUses`). */
  maxUses?: number;
}>;
 
/**
 * Named prepared statement cache settings, given as the `statementCache` option of {@link SequelizeOptions}. PostgreSQL extension.
 *
 * @postgrelize
 * @example
 * ```ts
 * const db = new Sequelize('postgres://user:pass@localhost:5432/app', { statementCache: { max: 50 } });
 * ```
 */
export type StatementCacheOptions = Readonly<{
  /** Maximum named statements per connection. Default 100. Further statements run unnamed. */
  max?: number;
}>;
 
/** Defaults applied to every model defined through this instance (Sequelize v6 `define` option). */
export type ModelDefaults = Readonly<{
  /** Add `createdAt` / `updatedAt`. Default true. */
  timestamps?: boolean;
  /** Use snake_case column names for attributes and the table name. */
  underscored?: boolean;
  /** Use the model name as table name without pluralization. */
  freezeTableName?: boolean;
  /** PostgreSQL schema of the tables. */
  schema?: string;
  /** Soft-delete rows (`deletedAt`) instead of deleting them. */
  paranoid?: boolean;
  /** Hooks added to every model. */
  hooks?: HooksOption;
}>;
 
/** Connection settings of one replication member. */
export type ReplicationMember = Readonly<{
  /** Host. */
  host?: string;
  /** Port. */
  port?: number;
  /** User name. */
  username?: string;
  /** Password. */
  password?: string;
  /** Database name. */
  database?: string;
}>;
 
/**
 * The `replication` option: readers receive SELECTs outside transactions, the writer everything else.
 *
 * Members inherit missing connection settings from the top-level options. See {@link ReplicationMember}.
 *
 * @example
 * ```ts
 * const replication: ReplicationOptions = {
 *   read: [{ host: 'replica-1.internal' }, { host: 'replica-2.internal' }],
 *   write: { host: 'primary.internal' },
 * };
 * const db = new Sequelize('app', 'user', 'pass', { dialect: 'postgres', replication });
 * ```
 */
export type ReplicationOptions = Readonly<{
  /** Reader(s). */
  read: ReplicationMember | readonly ReplicationMember[];
  /** Writer; defaults to the top-level connection settings. */
  write?: ReplicationMember;
}>;
 
/**
 * Options of the {@link Sequelize} constructor (PostgreSQL subset of Sequelize v6 `Options`).
 *
 * All fields are optional; values given in a connection URL are overridden by these fields.
 *
 * @example
 * ```ts
 * const options: SequelizeOptions = {
 *   dialect: 'postgres',
 *   host: 'localhost',
 *   port: 5432,
 *   pool: { max: 10 },
 *   logging: false,
 *   define: { underscored: true },
 * };
 * const db = new Sequelize('app', 'user', 'pass', options);
 * ```
 */
export type SequelizeOptions = Readonly<{
  /** Must be `'postgres'` when given. */
  dialect?: 'postgres';
  /** Database name. */
  database?: string;
  /** User name. */
  username?: string;
  /** Password. */
  password?: string;
  /** Host. Default `'localhost'`. */
  host?: string;
  /** Port. Default 5432. */
  port?: number;
  /** SSL settings passed to `pg`. */
  ssl?: PoolConfig['ssl'];
  /** Extra options passed to the `pg` pool/client as is. */
  dialectOptions?: Readonly<Omit<PoolConfig, 'types'>>;
  /**
   * Accepted for Sequelize v6 type compatibility; postgrelize ignores it and always loads `pg`.
   * @defaultValue undefined
   */
  dialectModule?: object;
  /** Pool settings. */
  pool?: PoolOptions;
  /** SQL logger, or `false` to disable. Default `console.log`. */
  logging?: LoggingFunction | false;
  /** Pass query duration to the logger. */
  benchmark?: boolean;
  /** Session time zone. Default `'+00:00'`. */
  timezone?: string;
  /** Defaults for all models. */
  define?: ModelDefaults;
  /** Read replicas and writer (Sequelize v6 `replication`); entries inherit missing connection settings. */
  replication?: ReplicationOptions;
  /** Sequelize-level hooks run for every model (and `beforeConnect`). */
  hooks?: HooksOption;
  /**
   * Shorten the aliases of included columns (`Posts.Comments.title` becomes `_0`) and of table aliases
   * of 64 or more characters, as in Sequelize v6, so deep includes stay within PostgreSQL's 63-byte
   * identifier limit. Without it PostgreSQL truncates longer aliases and columns can collide.
   * Default `false`.
   */
  minifyAliases?: boolean;
  /**
   * Retries failed queries and transaction starts (Sequelize v6 `retry`, backed by the same rules as
   * `retry-as-promised`). Applies to connection acquisition and query execution of `query`, finders
   * and writes, and to acquiring the connection and `BEGIN` of a transaction; never to a statement
   * inside an open transaction (PostgreSQL aborts it) or to COPY. A per-query or per-transaction
   * `retry` option is merged over it. Off by default.
   */
  retry?: RetryOptions;
  /**
   * Minimum IN-list length compiled to `= ANY($n)`. Default 100. PostgreSQL extension.
   * @postgrelize
   */
  inAsAnyThreshold?: number;
  /**
   * Opt-in named prepared statements (PostgreSQL extension): repeated SELECT / INSERT / UPDATE / DELETE texts are
   * parsed and planned once per connection. `true` uses the default bound of 100 statements per connection.
   * Off by default; not for PgBouncer in transaction pooling mode; pair with a bounded `pool.max`.
   * @postgrelize
   */
  statementCache?: boolean | StatementCacheOptions;
}>;
 
/** Connection settings exposed as `sequelize.config` (Sequelize v6 shape). */
export type ConnectionConfig = Readonly<{
  /** Database name. */
  database: string | undefined;
  /** User name. */
  username: string | undefined;
  /** Password. */
  password: string | undefined;
  /** Host. */
  host: string;
  /** Port. */
  port: number;
}>;