1
- import format , { ident , literal } from 'pg-format'
1
+ import { ident , literal } from 'pg-format'
2
2
import { triggersSql } from './sql'
3
3
import { PostgresMetaResult , PostgresTrigger } from './types'
4
4
@@ -35,7 +35,7 @@ export default class PostgresMetaTriggers {
35
35
table ?: string
36
36
} ) : Promise < PostgresMetaResult < PostgresTrigger > > {
37
37
if ( id ) {
38
- const sql = `${ enrichedTriggersSql } WHERE triggers. id = ${ literal ( id ) } ;`
38
+ const sql = `${ enrichedTriggersSql } WHERE id = ${ literal ( id ) } ;`
39
39
40
40
const { data, error } = await this . query ( sql )
41
41
@@ -53,9 +53,9 @@ export default class PostgresMetaTriggers {
53
53
}
54
54
55
55
if ( name && schema && table ) {
56
- const sql = `${ enrichedTriggersSql } WHERE triggers. name = ${ literal (
57
- name
58
- ) } AND triggers.schema = ${ literal ( schema ) } AND triggers. table = ${ literal ( table ) } ;`
56
+ const sql = `${ enrichedTriggersSql } WHERE name = ${ literal ( name ) } AND schema = ${ literal (
57
+ schema
58
+ ) } AND triggers.table = ${ literal ( table ) } ;`
59
59
60
60
const { data, error } = await this . query ( sql )
61
61
@@ -125,17 +125,12 @@ export default class PostgresMetaTriggers {
125
125
} ) : Promise < PostgresMetaResult < PostgresTrigger > > {
126
126
const qualifiedTableName = `${ ident ( schema ) } .${ ident ( table ) } `
127
127
const qualifiedFunctionName = `${ ident ( function_schema ) } .${ ident ( function_name ) } `
128
+ const triggerEvents = events . join ( ' OR ' )
129
+ const triggerOrientation = orientation ? `FOR EACH ${ orientation } ` : ''
130
+ const triggerCondition = condition ? `WHEN (${ condition } )` : ''
131
+ const functionArgs = `${ function_args ?. map ( literal ) . join ( ',' ) ?? '' } `
128
132
129
- const triggerOrientation = orientation ? `FOR EACH ${ format . string ( orientation ) } ` : ''
130
- const triggerCondition = condition ? `WHEN ( ${ format . string ( condition ) } )` : ''
131
- const triggerEvents = Array . isArray ( events ) ? `${ format . string ( events . join ( ' OR ' ) ) } ` : ''
132
- const functionArgs = Array . isArray ( function_args )
133
- ? `${ function_args . map ( ( arg ) => literal ( arg ) ) . join ( ',' ) } `
134
- : ''
135
-
136
- const sql = `CREATE TRIGGER ${ ident ( name ) } ${ format . string (
137
- activation
138
- ) } ${ triggerEvents } ON ${ qualifiedTableName } ${ triggerOrientation } ${ triggerCondition } EXECUTE FUNCTION ${ qualifiedFunctionName } ( ${ functionArgs } );`
133
+ const sql = `CREATE TRIGGER ${ ident ( name ) } ${ activation } ${ triggerEvents } ON ${ qualifiedTableName } ${ triggerOrientation } ${ triggerCondition } EXECUTE FUNCTION ${ qualifiedFunctionName } (${ functionArgs } );`
139
134
140
135
const { error } = await this . query ( sql )
141
136
@@ -206,8 +201,9 @@ export default class PostgresMetaTriggers {
206
201
}
207
202
208
203
const { name, schema, table } = triggerRecord !
209
- const qualifiedTableName = `${ ident ( schema ) } .${ ident ( table ) } `
210
- const sql = `DROP TRIGGER ${ ident ( name ) } ON ${ qualifiedTableName } ${ cascade ? 'CASCADE' : '' } ;`
204
+ const sql = `DROP TRIGGER ${ ident ( name ) } ON ${ ident ( schema ) } .${ ident ( table ) } ${
205
+ cascade ? 'CASCADE' : ''
206
+ } ;`
211
207
212
208
{
213
209
const { error } = await this . query ( sql )
@@ -222,7 +218,10 @@ export default class PostgresMetaTriggers {
222
218
}
223
219
224
220
const enrichedTriggersSql = `
225
- WITH triggers AS (${ triggersSql } )
226
- SELECT
227
- *
228
- FROM triggers`
221
+ WITH triggers AS (
222
+ ${ triggersSql }
223
+ )
224
+ SELECT
225
+ *
226
+ FROM triggers
227
+ `
0 commit comments