All files / src/query-interface query-interface-objects.ts

100% Statements 9/9
100% Branches 8/8
100% Functions 8/8
100% Lines 9/9

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                                              387x 387x                                         227x                                       77x                                       243x                                       92x                                     93x                                 101x                                               92x      
import type { CreateExtensionOptions, DropExtensionOptions } from '../ddl/extension-ddl.ts';
import type {
  AlterSequenceOptions,
  CreateSequenceOptions,
  DropSequenceOptions,
  SequenceNumber,
} from '../ddl/sequence-ddl.ts';
import type { TableName } from '../ddl/sql-literal.ts';
import type { QueryOptionsBase, RuntimeOwner } from '../model/runtime.ts';
import { ObjectCommands } from './object-commands.ts';
import { QueryInterfaceTables } from './query-interface-tables.ts';
import { StatementRunner } from './statement-runner.ts';
 
/**
 * Schema object part of {@link QueryInterface}: extensions and sequences.
 */
export class QueryInterfaceObjects extends QueryInterfaceTables {
  readonly #objects: ObjectCommands;
 
  /**
   * @param sequelize - Owning `Sequelize` instance.
   */
  constructor(sequelize: RuntimeOwner) {
    super(sequelize);
    this.#objects = new ObjectCommands(new StatementRunner(sequelize));
  }
 
  /**
   * Installs an extension with `CREATE EXTENSION`. Needs the privileges PostgreSQL requires for that extension (superuser or a trusted extension).
   *
   * @postgrelize
   * @param name - Extension name, quoted as an identifier.
   * @param options - `ifNotExists`, `schema`, `version`, `cascade` and query options.
   * @throws DatabaseError when the extension is not available on the server or the role may not install it.
   *
   * @example
   * ```ts
   * await queryInterface.createExtension('pg_trgm', { ifNotExists: true });
   * ```
   * @see [Schema guide](/guide/schema)
   */
  async createExtension(
    name: string,
    options: CreateExtensionOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#objects.createExtension(name, options);
  }
 
  /**
   * Removes an extension with `DROP EXTENSION`.
   *
   * @postgrelize
   * @param name - Extension name.
   * @param options - `ifExists`, `cascade` and query options.
   * @throws DatabaseError when objects depend on the extension and `cascade` is not set.
   *
   * @example
   * ```ts
   * await queryInterface.dropExtension('pg_trgm', { ifExists: true });
   * ```
   */
  async dropExtension(
    name: string,
    options: DropExtensionOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#objects.dropExtension(name, options);
  }
 
  /**
   * Creates a sequence with `CREATE SEQUENCE`. Numbers must be whole (`number` or `bigint`).
   *
   * @postgrelize
   * @param name - Sequence name or `{ tableName, schema }`.
   * @param options - Sequence properties and query options.
   * @throws Error for an unknown `dataType` or a non-integer number.
   *
   * @example
   * ```ts
   * await queryInterface.createSequence('invoice_number', { start: 1000, increment: 1, cache: 10 });
   * ```
   */
  async createSequence(
    name: TableName,
    options: CreateSequenceOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#objects.createSequence(name, options);
  }
 
  /**
   * Changes a sequence with `ALTER SEQUENCE`.
   *
   * @postgrelize
   * @param name - Sequence name or `{ tableName, schema }`.
   * @param options - Properties to change (at least one) and query options.
   * @throws Error when no property is given or a number is not whole.
   *
   * @example
   * ```ts
   * await queryInterface.alterSequence('invoice_number', { restartWith: 5000, cycle: false });
   * ```
   */
  async alterSequence(
    name: TableName,
    options: AlterSequenceOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#objects.alterSequence(name, options);
  }
 
  /**
   * Drops a sequence with `DROP SEQUENCE`.
   *
   * @postgrelize
   * @param name - Sequence name or `{ tableName, schema }`.
   * @param options - `ifExists`, `cascade` and query options.
   *
   * @example
   * ```ts
   * await queryInterface.dropSequence('invoice_number', { ifExists: true });
   * ```
   */
  async dropSequence(
    name: TableName,
    options: DropSequenceOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#objects.dropSequence(name, options);
  }
 
  /**
   * Advances a sequence and returns the new value (`SELECT nextval(...)`; the name is bound).
   *
   * @postgrelize
   * @param name - Sequence name or `{ tableName, schema }`.
   * @param options - Query options.
   * @returns The value as text, so bigint values stay exact.
   *
   * @example
   * ```ts
   * const next = await queryInterface.nextval('invoice_number');
   * ```
   */
  async nextval(name: TableName, options: QueryOptionsBase = {}): Promise<string> {
    return await this.#objects.nextval(name, options);
  }
 
  /**
   * Sets the current value of a sequence (`SELECT setval(...)`; name and value are bound).
   *
   * @postgrelize
   * @param name - Sequence name or `{ tableName, schema }`.
   * @param value - New value (`number` or `bigint`).
   * @param isCalled - `true` (default): the next `nextval` returns `value + increment`; `false`: it returns `value`.
   * @param options - Query options.
   * @returns The value set, as text.
   *
   * @example
   * ```ts
   * await queryInterface.setval('invoice_number', 9000, false);
   * ```
   */
  async setval(
    name: TableName,
    value: SequenceNumber,
    isCalled = true,
    options: QueryOptionsBase = {},
  ): Promise<string> {
    return await this.#objects.setval(name, value, isCalled, options);
  }
}