All files / src/query-interface policy-commands.ts

100% Statements 7/7
100% Branches 0/0
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                        131x                           387x                         75x                         2x                             237x                             149x                             77x      
import {
  PolicyDdl,
  type AlterPolicyOptions,
  type CreatePolicyOptions,
  type DisableRowLevelSecurityOptions,
  type DropPolicyOptions,
  type EnableRowLevelSecurityOptions,
} from '../ddl/policy-ddl.ts';
import type { TableName } from '../ddl/sql-literal.ts';
import type { QueryOptionsBase } from '../model/runtime.ts';
import type { StatementRunner } from './statement-runner.ts';
 
const policies = new PolicyDdl();
 
/**
 * Row-level security DDL of `QueryInterface`. Names are quoted; string conditions are trusted SQL.
 *
 * @internal
 */
export class PolicyCommands {
  readonly #runner: StatementRunner;
 
  /**
   * @param runner - Statement runner.
   */
  constructor(runner: StatementRunner) {
    this.#runner = runner;
  }
 
  /**
   * Runs `ENABLE ROW LEVEL SECURITY`.
   *
   * @param table - Table.
   * @param options - Statement and query options.
   */
  async enable(
    table: TableName,
    options: EnableRowLevelSecurityOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(policies.enableSql(table, options), options);
  }
 
  /**
   * Runs `DISABLE ROW LEVEL SECURITY`.
   *
   * @param table - Table.
   * @param options - Statement and query options.
   */
  async disable(
    table: TableName,
    options: DisableRowLevelSecurityOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(policies.disableSql(table, options), options);
  }
 
  /**
   * Runs `CREATE POLICY`.
   *
   * @param table - Table.
   * @param name - Policy name.
   * @param options - Policy and query options.
   */
  async create(
    table: TableName,
    name: string,
    options: CreatePolicyOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(policies.createSql(table, name, options), options);
  }
 
  /**
   * Runs `ALTER POLICY`.
   *
   * @param table - Table.
   * @param name - Policy name.
   * @param options - Changes and query options.
   */
  async alter(
    table: TableName,
    name: string,
    options: AlterPolicyOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.runAll(policies.alterSql(table, name, options), options);
  }
 
  /**
   * Runs `DROP POLICY`.
   *
   * @param table - Table.
   * @param name - Policy name.
   * @param options - Statement and query options.
   */
  async drop(
    table: TableName,
    name: string,
    options: DropPolicyOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(policies.dropSql(table, name, options), options);
  }
}