1
- import { ident , literal } from 'pg-format'
2
- import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
3
- import { coalesceRowsToArray } from './helpers'
4
1
import {
5
- columnsSql ,
6
- grantsSql ,
7
- policiesSql ,
8
- primaryKeysSql ,
9
- relationshipsSql ,
10
- tablesSql ,
11
2
triggersSql ,
12
3
} from './sql'
13
4
import { PostgresMetaResult , PostgresTable } from './types'
@@ -19,171 +10,59 @@ export default class PostgresMetaTriggers {
19
10
this . query = query
20
11
}
21
12
22
- async list ( { includeSystemSchemas = false } = { } ) : Promise < PostgresMetaResult < PostgresTable [ ] > > {
13
+ async list ( { } = { } ) : Promise < PostgresMetaResult < PostgresTable [ ] > > {
23
14
const sql = enrichedTriggersSql
24
15
return await this . query ( sql )
25
16
}
26
17
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
- }
18
+ // async retrieve({ id }: { id: number }): Promise<PostgresMetaResult<PostgresTable>>
19
+ // async retrieve({
20
+ // name,
21
+ // schema,
22
+ // }: {
23
+ // name: string
24
+ // schema: string
25
+ // }): Promise<PostgresMetaResult<PostgresTable>>
26
+ // async retrieve({
27
+ // id,
28
+ // name,
29
+ // schema = 'public',
30
+ // }: {
31
+ // id?: number
32
+ // name?: string
33
+ // schema?: string
34
+ // }): Promise<PostgresMetaResult<PostgresTable>> {
35
+ // // @TODO
36
+ // }
73
37
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
- }
38
+ // async create({
39
+ // name,
40
+ // schema = 'public',
41
+ // comment,
42
+ // }: {
43
+ // name: string
44
+ // schema?: string
45
+ // comment?: string
46
+ // }): Promise<PostgresMetaResult<PostgresTable>> {
47
+ // // @TODO
48
+ // }
95
49
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
- }
50
+ // async update(
51
+ // id: number,
52
+ // {
53
+ // name,
54
+ // schema,
55
+ // }: {
56
+ // name?: string
57
+ // schema?: string
58
+ // }
59
+ // ): Promise<PostgresMetaResult<PostgresTable>> {
60
+ // // @TODO
61
+ // }
120
62
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
- }
63
+ // async remove(id: number, { cascade = false } = {}): Promise<PostgresMetaResult<PostgresTable>> {
64
+ // // @TODO
65
+ // }
187
66
}
188
67
189
68
const enrichedTriggersSql = `
0 commit comments