View Operations
Operation: createView
pgm.createView( viewName, options, definition )
IMPORTANT
Create a new view - postgres docs
Arguments
| Name | Type | Description |
|---|---|---|
viewName | string | name of the new view |
options | object | Check below for available options |
definition | string | SQL of SELECT statement |
Options
| Option | Type | Description |
|---|---|---|
temporary | boolean | adds TEMPORARY clause |
replace | boolean | adds OR REPLACE clause |
recursive | boolean | adds RECURSIVE clause |
columns | string or array | use if you want to name columns differently then inferred from definition |
options | object | key value pairs of View Options |
checkOption | string | CASCADED or LOCAL |
Option names are quoted and folded to lowercase, without camel-case conversion. Values may be strings, finite numbers, or booleans. Strings are escaped as SQL values; pass check_option: 'local', without adding SQL quotes around local. The check_option enum is normalized to lowercase so information_schema.views reports LOCAL or CASCADED consistently with the checkOption convenience option. Other string values retain their supplied case.
NaN, Infinity, and -Infinity throw a TypeError naming the option and value during SQL generation, before SQL for that operation is queued. Finite numbers, including negatives, zero, and decimals, are rendered unchanged; PostgreSQL validates which values each option supports.
pgm.createView(
'active_accounts',
{ options: { check_option: 'local', security_barrier: true } },
'SELECT * FROM accounts WHERE active'
);Reverse Operation: dropView
pgm.dropView( viewName, options )
IMPORTANT
Drop a view - postgres docs
Arguments
| Name | Type | Description |
|---|---|---|
viewName | string | name of the view to delete |
options | object | Check below for available options |
Options
| Option | Type | Description |
|---|---|---|
ifExists | boolean | drops view only if it exists |
cascade | boolean | drops also dependent objects |
Operation: alterView
pgm.alterView( viewName, options )
IMPORTANT
Alter a view - postgres docs
Arguments
| Name | Type | Description |
|---|---|---|
viewName | string | name of the view to alter |
options | object | Check below for available options |
Options
| Option | Type | Description |
|---|---|---|
checkOption | string | CASCADED, LOCAL or null to reset |
options | object | key value (null to reset) pairs of View Options |
Option names and values use the same escaping and finite-number validation as createView. A null value resets the named option; it is not included in SET. For example:
pgm.alterView('active_accounts', {
options: { security_barrier: false, check_option: null },
});Operation: alterViewColumn
pgm.alterViewColumn( viewName, columnName, options )
IMPORTANT
Alter a view column - postgres docs
Arguments
| Name | Type | Description |
|---|---|---|
viewName | string | name of the view to alter |
columnName | string | name of the column to alter |
options | object | Check below for available options |
Options
| Option | Type | Description |
|---|---|---|
default | string | default value of column |
Operation: renameView
pgm.renameView( viewName, newViewName )
IMPORTANT
Rename a view - postgres docs
Arguments
| Name | Type | Description |
|---|---|---|
viewName | Name | old name of the view |
newViewName | Name | new name of the view |
Renaming preserves the source schema, including during automatic reversal. See Renaming and schemas for destination-schema validation and supported PgLiteral identifiers.
To move a view between schemas, use SQL explicitly, for example pgm.sql('ALTER VIEW "old_schema"."my_view" SET SCHEMA "new_schema"'). Provide the corresponding SQL in your down migration to reverse that move.