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

100% Statements 7/7
100% Branches 5/5
100% Functions 6/6
100% Lines 7/7

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                                              387x 387x                                       75x                                     2x                                                       237x                                           149x                                         77x      
import type {
  AlterPolicyOptions,
  CreatePolicyOptions,
  DisableRowLevelSecurityOptions,
  DropPolicyOptions,
  EnableRowLevelSecurityOptions,
} from '../ddl/policy-ddl.ts';
import type { TableName } from '../ddl/sql-literal.ts';
import type { QueryOptionsBase, RuntimeOwner } from '../model/runtime.ts';
import { PolicyCommands } from './policy-commands.ts';
import { QueryInterfaceViews } from './query-interface-views.ts';
import { StatementRunner } from './statement-runner.ts';
 
/**
 * Row-level security part of {@link QueryInterface}.
 */
export class QueryInterfacePolicies extends QueryInterfaceViews {
  readonly #policies: PolicyCommands;
 
  /**
   * @param sequelize - Owning `Sequelize` instance.
   */
  constructor(sequelize: RuntimeOwner) {
    super(sequelize);
    this.#policies = new PolicyCommands(new StatementRunner(sequelize));
  }
 
  /**
   * Turns on row-level security for a table (`ALTER TABLE ... ENABLE ROW LEVEL SECURITY`). Without policies, non-owners see no rows.
   *
   * @postgrelize
   * @param table - Table name or `{ tableName, schema }`.
   * @param options - `force` (also applies to the table owner) and query options.
   *
   * @example
   * ```ts
   * await queryInterface.enableRowLevelSecurity('documents', { force: true });
   * ```
   * @see [Schema objects guide](/guide/schema-objects)
   */
  async enableRowLevelSecurity(
    table: TableName,
    options: EnableRowLevelSecurityOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#policies.enable(table, options);
  }
 
  /**
   * Turns off row-level security for a table (`ALTER TABLE ... DISABLE ROW LEVEL SECURITY`); policies are kept.
   *
   * @postgrelize
   * @param table - Table name or `{ tableName, schema }`.
   * @param options - `noForce` and query options.
   *
   * @example
   * ```ts
   * await queryInterface.disableRowLevelSecurity('documents', { noForce: true });
   * ```
   */
  async disableRowLevelSecurity(
    table: TableName,
    options: DisableRowLevelSecurityOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#policies.disable(table, options);
  }
 
  /**
   * Creates a row-level security policy with `CREATE POLICY`.
   *
   * `using` and `withCheck` are SQL text you provide (not escaped, never build them from user input) or a where object, whose values are rendered as escaped literals. Session variables set with the `settings` transaction option work in conditions: `"tenant_id = current_setting('app.tenant_id')::int"`.
   *
   * @postgrelize
   * @param table - Table name or `{ tableName, schema }`.
   * @param name - Policy name.
   * @param options - `command`, `kind`, `roles`, `using`, `withCheck` and query options.
   * @throws Error for an unknown `command` or `kind`, empty `roles` or an empty condition.
   *
   * @example
   * ```ts
   * await queryInterface.createPolicy('documents', 'tenant_isolation', {
   *   roles: ['app_user'],
   *   using: "tenant_id = current_setting('app.tenant_id')::int",
   *   withCheck: "tenant_id = current_setting('app.tenant_id')::int",
   * });
   * ```
   */
  async createPolicy(
    table: TableName,
    name: string,
    options: CreatePolicyOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#policies.create(table, name, options);
  }
 
  /**
   * Changes a policy with `ALTER POLICY`: roles, conditions and name (`rename`, applied last).
   *
   * @postgrelize
   * @param table - Table name or `{ tableName, schema }`.
   * @param name - Current policy name.
   * @param options - Changes (at least one) and query options.
   * @throws Error when no change is given.
   *
   * @example
   * ```ts
   * await queryInterface.alterPolicy('documents', 'tenant_isolation', { roles: ['app_user', 'app_admin'] });
   * ```
   */
  async alterPolicy(
    table: TableName,
    name: string,
    options: AlterPolicyOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#policies.alter(table, name, options);
  }
 
  /**
   * Drops a policy with `DROP POLICY`.
   *
   * @postgrelize
   * @param table - Table name or `{ tableName, schema }`.
   * @param name - Policy name.
   * @param options - `ifExists`, `cascade` and query options.
   *
   * @example
   * ```ts
   * await queryInterface.dropPolicy('documents', 'tenant_isolation', { ifExists: true });
   * ```
   */
  async dropPolicy(
    table: TableName,
    name: string,
    options: DropPolicyOptions & QueryOptionsBase = {},
  ): Promise<void> {
    await this.#policies.drop(table, name, options);
  }
}