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 | 387x 387x 167x 164x 92x 92x 19x | import type {
CreateMaterializedViewOptions,
CreateViewOptions,
DropViewOptions,
RefreshMaterializedViewOptions,
} from '../ddl/view-ddl.ts';
import type { TableName } from '../ddl/sql-literal.ts';
import type { QueryOptionsBase, RuntimeOwner } from '../model/runtime.ts';
import { QueryInterfaceRoutines } from './query-interface-routines.ts';
import { StatementRunner } from './statement-runner.ts';
import { ViewCommands } from './view-commands.ts';
/**
* View part of {@link QueryInterface}: views and materialized views.
*/
export class QueryInterfaceViews extends QueryInterfaceRoutines {
readonly #views: ViewCommands;
/**
* @param sequelize - Owning `Sequelize` instance.
*/
constructor(sequelize: RuntimeOwner) {
super(sequelize);
this.#views = new ViewCommands(new StatementRunner(sequelize));
}
/**
* Creates a view with `CREATE VIEW`; `replace` renders `CREATE OR REPLACE VIEW`.
*
* The `query` is SQL text you provide: it is not escaped, so never build it from user input. Names and column lists are quoted identifiers. A trailing `;` is removed.
*
* @postgrelize
* @param name - View name or `{ tableName, schema }`.
* @param query - Trusted `SELECT` text.
* @param options - `replace`, `temporary`, `recursive`, `columns`, `checkOption`, `securityBarrier`, `securityInvoker` and query options.
* @throws Error for an empty query, a recursive view without `columns` or an unknown `checkOption`.
*
* @example
* ```ts
* await queryInterface.createView('published_posts', 'SELECT id, title FROM "Posts" WHERE published', { replace: true });
* ```
* @see [Schema objects guide](/guide/schema-objects)
*/
async createView(
name: TableName,
query: string,
options: CreateViewOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#views.createView(name, query, options);
}
/**
* Drops a view with `DROP VIEW`.
*
* @postgrelize
* @param name - View name or `{ tableName, schema }`.
* @param options - `ifExists`, `cascade` and query options.
*
* @example
* ```ts
* await queryInterface.dropView('published_posts', { ifExists: true });
* ```
*/
async dropView(name: TableName, options: DropViewOptions & QueryOptionsBase = {}): Promise<void> {
await this.#views.dropView(name, false, options);
}
/**
* Creates a materialized view with `CREATE MATERIALIZED VIEW`.
*
* The `query` is trusted SQL text, as in {@link QueryInterface.createView}. PostgreSQL has no `OR REPLACE` for materialized views: drop and create, or use `ifNotExists`.
*
* @postgrelize
* @param name - View name or `{ tableName, schema }`.
* @param query - Trusted `SELECT` text.
* @param options - `ifNotExists`, `columns`, `using`, `withData` and query options.
* @throws Error for an empty query or an invalid access method.
*
* @example
* ```ts
* await queryInterface.createMaterializedView('post_stats', 'SELECT author_id, count(*) AS n FROM "Posts" GROUP BY author_id');
* ```
*/
async createMaterializedView(
name: TableName,
query: string,
options: CreateMaterializedViewOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#views.createMaterializedView(name, query, options);
}
/**
* Recomputes a materialized view with `REFRESH MATERIALIZED VIEW`.
*
* @postgrelize
* @param name - View name or `{ tableName, schema }`.
* @param options - `concurrently` (needs a unique index), `withData` and query options.
* @throws Error when `concurrently` is combined with `withData: false`.
*
* @example
* ```ts
* await queryInterface.refreshMaterializedView('post_stats', { concurrently: true });
* ```
*/
async refreshMaterializedView(
name: TableName,
options: RefreshMaterializedViewOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#views.refreshMaterializedView(name, options);
}
/**
* Drops a materialized view with `DROP MATERIALIZED VIEW`.
*
* @postgrelize
* @param name - View name or `{ tableName, schema }`.
* @param options - `ifExists`, `cascade` and query options.
*
* @example
* ```ts
* await queryInterface.dropMaterializedView('post_stats', { ifExists: true });
* ```
*/
async dropMaterializedView(
name: TableName,
options: DropViewOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#views.dropView(name, true, options);
}
}
|