@@ -16,33 +16,51 @@ export default class PostgresMetaTables {
1616 this . query = query
1717 }
1818
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 [ ] } ) [ ] > >
1935 async list ( {
2036 includeSystemSchemas = false ,
2137 includedSchemas,
2238 excludedSchemas,
2339 limit,
2440 offset,
41+ includeColumns = true ,
2542 } : {
2643 includeSystemSchemas ?: boolean
2744 includedSchemas ?: string [ ]
2845 excludedSchemas ?: string [ ]
2946 limit ?: number
3047 offset ?: number
48+ includeColumns ?: boolean
3149 } = { } ) : Promise < PostgresMetaResult < PostgresTable [ ] > > {
32- let sql = enrichedTablesSql
50+ let sql = generateEnrichedTablesSql ( { includeColumns } )
3351 const filter = filterByList (
3452 includedSchemas ,
3553 excludedSchemas ,
3654 ! includeSystemSchemas ? DEFAULT_SYSTEM_SCHEMAS : undefined
3755 )
3856 if ( filter ) {
39- sql += ` WHERE schema ${ filter } `
57+ sql += ` where schema ${ filter } `
4058 }
4159 if ( limit ) {
42- sql = `${ sql } LIMIT ${ limit } `
60+ sql + = ` limit ${ limit } `
4361 }
4462 if ( offset ) {
45- sql = `${ sql } OFFSET ${ offset } `
63+ sql + = ` offset ${ offset } `
4664 }
4765 return await this . query ( sql )
4866 }
@@ -65,7 +83,9 @@ export default class PostgresMetaTables {
6583 schema ?: string
6684 } ) : Promise < PostgresMetaResult < PostgresTable > > {
6785 if ( id ) {
68- const sql = `${ enrichedTablesSql } WHERE tables.id = ${ literal ( id ) } ;`
86+ const sql = `${ generateEnrichedTablesSql ( {
87+ includeColumns : true ,
88+ } ) } where tables.id = ${ literal ( id ) } ;`
6989 const { data, error } = await this . query ( sql )
7090 if ( error ) {
7191 return { data, error }
@@ -75,9 +95,9 @@ export default class PostgresMetaTables {
7595 return { data : data [ 0 ] , error }
7696 }
7797 } 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 ) } ;`
81101 const { data, error } = await this . query ( sql )
82102 if ( error ) {
83103 return { data, error }
@@ -227,18 +247,18 @@ COMMIT;`
227247 }
228248}
229249
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 (
240260 'relationships' ,
241261 `(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
242262 OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
243263 ) }
244- FROM tables`
264+ from tables`
0 commit comments