|
| 1 | +import { ident, literal } from 'pg-format' |
| 2 | +import { DEFAULT_SYSTEM_SCHEMAS } from './constants' |
| 3 | +import { coalesceRowsToArray } from './helpers' |
| 4 | +import { |
| 5 | + columnsSql, |
| 6 | + grantsSql, |
| 7 | + policiesSql, |
| 8 | + primaryKeysSql, |
| 9 | + relationshipsSql, |
| 10 | + tablesSql, |
| 11 | + triggersSql, |
| 12 | +} from './sql' |
| 13 | +import { PostgresMetaResult, PostgresTable } from './types' |
| 14 | + |
| 15 | +export default class PostgresMetaTriggers { |
| 16 | + query: (sql: string) => Promise<PostgresMetaResult<any>> |
| 17 | + |
| 18 | + constructor(query: (sql: string) => Promise<PostgresMetaResult<any>>) { |
| 19 | + this.query = query |
| 20 | + } |
| 21 | + |
| 22 | + async list({ includeSystemSchemas = false } = {}): Promise<PostgresMetaResult<PostgresTable[]>> { |
| 23 | + const sql = enrichedTriggersSql |
| 24 | + return await this.query(sql) |
| 25 | + } |
| 26 | + |
| 27 | + async retrieve({ id }: { id: number }): Promise<PostgresMetaResult<PostgresTable>> |
| 28 | + async retrieve({ |
| 29 | + name, |
| 30 | + schema, |
| 31 | + }: { |
| 32 | + name: string |
| 33 | + schema: string |
| 34 | + }): Promise<PostgresMetaResult<PostgresTable>> |
| 35 | + async retrieve({ |
| 36 | + id, |
| 37 | + name, |
| 38 | + schema = 'public', |
| 39 | + }: { |
| 40 | + id?: number |
| 41 | + name?: string |
| 42 | + schema?: string |
| 43 | + }): Promise<PostgresMetaResult<PostgresTable>> { |
| 44 | + if (id) { |
| 45 | + const sql = `${enrichedTriggersSql} WHERE tables.id = ${literal(id)};` |
| 46 | + const { data, error } = await this.query(sql) |
| 47 | + if (error) { |
| 48 | + return { data, error } |
| 49 | + } else if (data.length === 0) { |
| 50 | + return { data: null, error: { message: `Cannot find a table with ID ${id}` } } |
| 51 | + } else { |
| 52 | + return { data: data[0], error } |
| 53 | + } |
| 54 | + } else if (name) { |
| 55 | + const sql = `${enrichedTriggersSql} WHERE tables.name = ${literal( |
| 56 | + name |
| 57 | + )} AND tables.schema = ${literal(schema)};` |
| 58 | + const { data, error } = await this.query(sql) |
| 59 | + if (error) { |
| 60 | + return { data, error } |
| 61 | + } else if (data.length === 0) { |
| 62 | + return { |
| 63 | + data: null, |
| 64 | + error: { message: `Cannot find a table named ${name} in schema ${schema}` }, |
| 65 | + } |
| 66 | + } else { |
| 67 | + return { data: data[0], error } |
| 68 | + } |
| 69 | + } else { |
| 70 | + return { data: null, error: { message: 'Invalid parameters on table retrieve' } } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + async create({ |
| 75 | + name, |
| 76 | + schema = 'public', |
| 77 | + comment, |
| 78 | + }: { |
| 79 | + name: string |
| 80 | + schema?: string |
| 81 | + comment?: string |
| 82 | + }): Promise<PostgresMetaResult<PostgresTable>> { |
| 83 | + const tableSql = `CREATE TABLE ${ident(schema)}.${ident(name)} ();` |
| 84 | + const commentSql = |
| 85 | + comment === undefined |
| 86 | + ? '' |
| 87 | + : `COMMENT ON TABLE ${ident(schema)}.${ident(name)} IS ${literal(comment)};` |
| 88 | + const sql = `BEGIN; ${tableSql} ${commentSql} COMMIT;` |
| 89 | + const { error } = await this.query(sql) |
| 90 | + if (error) { |
| 91 | + return { data: null, error } |
| 92 | + } |
| 93 | + return await this.retrieve({ name, schema }) |
| 94 | + } |
| 95 | + |
| 96 | + async update( |
| 97 | + id: number, |
| 98 | + { |
| 99 | + name, |
| 100 | + schema, |
| 101 | + rls_enabled, |
| 102 | + rls_forced, |
| 103 | + replica_identity, |
| 104 | + replica_identity_index, |
| 105 | + comment, |
| 106 | + }: { |
| 107 | + name?: string |
| 108 | + schema?: string |
| 109 | + rls_enabled?: boolean |
| 110 | + rls_forced?: boolean |
| 111 | + replica_identity?: 'DEFAULT' | 'INDEX' | 'FULL' | 'NOTHING' |
| 112 | + replica_identity_index?: string |
| 113 | + comment?: string |
| 114 | + } |
| 115 | + ): Promise<PostgresMetaResult<PostgresTable>> { |
| 116 | + const { data: old, error } = await this.retrieve({ id }) |
| 117 | + if (error) { |
| 118 | + return { data: null, error } |
| 119 | + } |
| 120 | + |
| 121 | + const alter = `ALTER TABLE ${ident(old!.schema)}.${ident(old!.name)}` |
| 122 | + const schemaSql = schema === undefined ? '' : `${alter} SET SCHEMA ${ident(schema)};` |
| 123 | + let nameSql = '' |
| 124 | + if (name !== undefined && name !== old!.name) { |
| 125 | + const currentSchema = schema === undefined ? old!.schema : schema |
| 126 | + nameSql = `ALTER TABLE ${ident(currentSchema)}.${ident(old!.name)} RENAME TO ${ident(name)};` |
| 127 | + } |
| 128 | + let enableRls = '' |
| 129 | + if (rls_enabled !== undefined) { |
| 130 | + const enable = `${alter} ENABLE ROW LEVEL SECURITY;` |
| 131 | + const disable = `${alter} DISABLE ROW LEVEL SECURITY;` |
| 132 | + enableRls = rls_enabled ? enable : disable |
| 133 | + } |
| 134 | + let forceRls = '' |
| 135 | + if (rls_forced !== undefined) { |
| 136 | + const enable = `${alter} FORCE ROW LEVEL SECURITY;` |
| 137 | + const disable = `${alter} NO FORCE ROW LEVEL SECURITY;` |
| 138 | + forceRls = rls_forced ? enable : disable |
| 139 | + } |
| 140 | + let replicaSql: string |
| 141 | + if (replica_identity === undefined) { |
| 142 | + replicaSql = '' |
| 143 | + } else if (replica_identity === 'INDEX') { |
| 144 | + replicaSql = `${alter} REPLICA IDENTITY USING INDEX ${replica_identity_index};` |
| 145 | + } else { |
| 146 | + replicaSql = `${alter} REPLICA IDENTITY ${replica_identity};` |
| 147 | + } |
| 148 | + const commentSql = |
| 149 | + comment === undefined |
| 150 | + ? '' |
| 151 | + : `COMMENT ON TABLE ${ident(old!.schema)}.${ident(old!.name)} IS ${literal(comment)};` |
| 152 | + // nameSql must be last, right below schemaSql |
| 153 | + const sql = ` |
| 154 | +BEGIN; |
| 155 | + ${enableRls} |
| 156 | + ${forceRls} |
| 157 | + ${replicaSql} |
| 158 | + ${commentSql} |
| 159 | + ${schemaSql} |
| 160 | + ${nameSql} |
| 161 | +COMMIT;` |
| 162 | + { |
| 163 | + const { error } = await this.query(sql) |
| 164 | + if (error) { |
| 165 | + return { data: null, error } |
| 166 | + } |
| 167 | + } |
| 168 | + return await this.retrieve({ id }) |
| 169 | + } |
| 170 | + |
| 171 | + async remove(id: number, { cascade = false } = {}): Promise<PostgresMetaResult<PostgresTable>> { |
| 172 | + const { data: table, error } = await this.retrieve({ id }) |
| 173 | + if (error) { |
| 174 | + return { data: null, error } |
| 175 | + } |
| 176 | + const sql = `DROP TABLE ${ident(table!.schema)}.${ident(table!.name)} ${ |
| 177 | + cascade ? 'CASCADE' : 'RESTRICT' |
| 178 | + };` |
| 179 | + { |
| 180 | + const { error } = await this.query(sql) |
| 181 | + if (error) { |
| 182 | + return { data: null, error } |
| 183 | + } |
| 184 | + } |
| 185 | + return { data: table!, error: null } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +const enrichedTriggersSql = ` |
| 190 | +WITH triggers AS (${triggersSql}) |
| 191 | +SELECT |
| 192 | + * |
| 193 | +FROM triggers` |
0 commit comments