All files / src/ddl extension-ddl.ts

100% Statements 3/3
100% Branches 12/12
100% Functions 2/2
100% Lines 3/3

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      132x                                                                                                           229x                               79x      
import { SqlCompiler } from '../sql/compile.ts';
import { SqlLiteral } from './sql-literal.ts';
 
const sqlLiteral = new SqlLiteral();
 
/**
 * Options of `createExtension`. PostgreSQL extension.
 *
 * @postgrelize
 * @example
 * ```ts
 * const options: CreateExtensionOptions = { ifNotExists: true, schema: 'public' };
 * await queryInterface.createExtension('pg_trgm', options);
 * ```
 */
export type CreateExtensionOptions = Readonly<{
  /** Adds `IF NOT EXISTS`. */
  ifNotExists?: boolean;
  /** Schema that receives the extension objects. */
  schema?: string;
  /** Extension version to install. */
  version?: string;
  /** Also installs the extensions it depends on. */
  cascade?: boolean;
}>;
 
/**
 * Options of `dropExtension`. PostgreSQL extension.
 *
 * @postgrelize
 * @example
 * ```ts
 * const options: DropExtensionOptions = { ifExists: true, cascade: true };
 * await queryInterface.dropExtension('pg_trgm', options);
 * ```
 */
export type DropExtensionOptions = Readonly<{
  /** Adds `IF EXISTS`. */
  ifExists?: boolean;
  /** Also drops objects that depend on the extension. */
  cascade?: boolean;
}>;
 
/**
 * `CREATE EXTENSION` and `DROP EXTENSION` statements.
 *
 * @internal
 */
export class ExtensionDdl {
  /**
   * `CREATE EXTENSION` statement.
   *
   * @param name - Extension name.
   * @param options - Statement options.
   * @returns Statement.
   */
  createSql(name: string, options: CreateExtensionOptions): string {
    return [
      `CREATE EXTENSION ${options.ifNotExists === true ? 'IF NOT EXISTS ' : ''}${SqlCompiler.quoteIdent(name)}`,
      options.schema === undefined ? '' : ` SCHEMA ${SqlCompiler.quoteIdent(options.schema)}`,
      options.version === undefined ? '' : ` VERSION ${sqlLiteral.escape(options.version)}`,
      options.cascade === true ? ' CASCADE' : '',
    ].join('');
  }
 
  /**
   * `DROP EXTENSION` statement.
   *
   * @param name - Extension name.
   * @param options - Statement options.
   * @returns Statement.
   */
  dropSql(name: string, options: DropExtensionOptions): string {
    return `DROP EXTENSION ${options.ifExists === true ? 'IF EXISTS ' : ''}${SqlCompiler.quoteIdent(name)}${options.cascade === true ? ' CASCADE' : ''}`;
  }
}