@@ -16,33 +16,51 @@ export default class PostgresMetaTables {
16
16
this . query = query
17
17
}
18
18
19
+ async list ( options : {
20
+ includeSystemSchemas ?: boolean
21
+ includedSchemas ?: string [ ]
22
+ excludedSchemas ?: string [ ]
23
+ limit ?: number
24
+ offset ?: number
25
+ includeColumns : false
26
+ } ) : Promise < PostgresMetaResult < ( PostgresTable & { columns : never } ) [ ] > >
27
+ async list ( options ?: {
28
+ includeSystemSchemas ?: boolean
29
+ includedSchemas ?: string [ ]
30
+ excludedSchemas ?: string [ ]
31
+ limit ?: number
32
+ offset ?: number
33
+ includeColumns ?: boolean
34
+ } ) : Promise < PostgresMetaResult < ( PostgresTable & { columns : unknown [ ] } ) [ ] > >
19
35
async list ( {
20
36
includeSystemSchemas = false ,
21
37
includedSchemas,
22
38
excludedSchemas,
23
39
limit,
24
40
offset,
41
+ includeColumns = true ,
25
42
} : {
26
43
includeSystemSchemas ?: boolean
27
44
includedSchemas ?: string [ ]
28
45
excludedSchemas ?: string [ ]
29
46
limit ?: number
30
47
offset ?: number
48
+ includeColumns ?: boolean
31
49
} = { } ) : Promise < PostgresMetaResult < PostgresTable [ ] > > {
32
- let sql = enrichedTablesSql
50
+ let sql = generateEnrichedTablesSql ( { includeColumns } )
33
51
const filter = filterByList (
34
52
includedSchemas ,
35
53
excludedSchemas ,
36
54
! includeSystemSchemas ? DEFAULT_SYSTEM_SCHEMAS : undefined
37
55
)
38
56
if ( filter ) {
39
- sql += ` WHERE schema ${ filter } `
57
+ sql += ` where schema ${ filter } `
40
58
}
41
59
if ( limit ) {
42
- sql = `${ sql } LIMIT ${ limit } `
60
+ sql + = ` limit ${ limit } `
43
61
}
44
62
if ( offset ) {
45
- sql = `${ sql } OFFSET ${ offset } `
63
+ sql + = ` offset ${ offset } `
46
64
}
47
65
return await this . query ( sql )
48
66
}
@@ -65,7 +83,9 @@ export default class PostgresMetaTables {
65
83
schema ?: string
66
84
} ) : Promise < PostgresMetaResult < PostgresTable > > {
67
85
if ( id ) {
68
- const sql = `${ enrichedTablesSql } WHERE tables.id = ${ literal ( id ) } ;`
86
+ const sql = `${ generateEnrichedTablesSql ( {
87
+ includeColumns : true ,
88
+ } ) } where tables.id = ${ literal ( id ) } ;`
69
89
const { data, error } = await this . query ( sql )
70
90
if ( error ) {
71
91
return { data, error }
@@ -75,9 +95,9 @@ export default class PostgresMetaTables {
75
95
return { data : data [ 0 ] , error }
76
96
}
77
97
} else if ( name ) {
78
- const sql = `${ enrichedTablesSql } WHERE tables.name = ${ literal (
79
- name
80
- ) } AND tables.schema = ${ literal ( schema ) } ;`
98
+ const sql = `${ generateEnrichedTablesSql ( {
99
+ includeColumns : true ,
100
+ } ) } where tables.name = ${ literal ( name ) } and tables.schema = ${ literal ( schema ) } ;`
81
101
const { data, error } = await this . query ( sql )
82
102
if ( error ) {
83
103
return { data, error }
@@ -227,18 +247,18 @@ COMMIT;`
227
247
}
228
248
}
229
249
230
- const enrichedTablesSql = `
231
- WITH tables AS (${ tablesSql } ),
232
- columns AS (${ columnsSql } ),
233
- primary_keys AS (${ primaryKeysSql } ),
234
- relationships AS (${ relationshipsSql } )
235
- SELECT
236
- *,
237
- ${ coalesceRowsToArray ( 'columns' , 'columns.table_id = tables.id' ) } ,
238
- ${ coalesceRowsToArray ( 'primary_keys' , 'primary_keys.table_id = tables.id' ) } ,
239
- ${ coalesceRowsToArray (
250
+ const generateEnrichedTablesSql = ( { includeColumns } : { includeColumns : boolean } ) => `
251
+ with tables as (${ tablesSql } )
252
+ ${ includeColumns ? `, columns as (${ columnsSql } )` : '' }
253
+ , primary_keys as (${ primaryKeysSql } )
254
+ , relationships as (${ relationshipsSql } )
255
+ select
256
+ *
257
+ ${ includeColumns ? `, ${ coalesceRowsToArray ( 'columns' , 'columns.table_id = tables.id' ) } ` : '' }
258
+ , ${ coalesceRowsToArray ( 'primary_keys' , 'primary_keys.table_id = tables.id' ) }
259
+ , ${ coalesceRowsToArray (
240
260
'relationships' ,
241
261
`(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
242
262
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
243
263
) }
244
- FROM tables`
264
+ from tables`
0 commit comments