File tree Expand file tree Collapse file tree 4 files changed +76
-3
lines changed Expand file tree Collapse file tree 4 files changed +76
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { literal } from 'pg-format'
2
2
import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
3
- import { viewsSql } from './sql'
3
+ import { coalesceRowsToArray } from './helpers'
4
+ import { columnsSql , viewsSql } from './sql'
4
5
import { PostgresMetaResult , PostgresView } from './types'
5
6
6
7
export default class PostgresMetaViews {
@@ -19,9 +20,9 @@ export default class PostgresMetaViews {
19
20
limit ?: number
20
21
offset ?: number
21
22
} = { } ) : Promise < PostgresMetaResult < PostgresView [ ] > > {
22
- let sql = viewsSql
23
+ let sql = enrichedViewsSql
23
24
if ( ! includeSystemSchemas ) {
24
- sql = `${ sql } AND n.nspname NOT IN (${ DEFAULT_SYSTEM_SCHEMAS . map ( literal ) . join ( ',' ) } )`
25
+ sql = `${ sql } WHERE schema NOT IN (${ DEFAULT_SYSTEM_SCHEMAS . map ( literal ) . join ( ',' ) } )`
25
26
}
26
27
if ( limit ) {
27
28
sql = `${ sql } LIMIT ${ limit } `
@@ -32,3 +33,11 @@ export default class PostgresMetaViews {
32
33
return await this . query ( sql )
33
34
}
34
35
}
36
+
37
+ const enrichedViewsSql = `
38
+ WITH views AS (${ viewsSql } ),
39
+ columns AS (${ columnsSql } )
40
+ SELECT
41
+ *,
42
+ ${ coalesceRowsToArray ( 'columns' , 'columns.table_id = views.id' ) }
43
+ FROM views`
Original file line number Diff line number Diff line change 2
2
c .oid :: int8 AS id,
3
3
n .nspname AS schema,
4
4
c .relname AS name,
5
+ -- See definition of information_schema.views
6
+ (pg_relation_is_updatable(c .oid , false) & 20 ) = 20 AS is_updatable,
5
7
obj_description(c .oid ) AS comment
6
8
FROM
7
9
pg_class c
Original file line number Diff line number Diff line change @@ -279,6 +279,8 @@ export const postgresViewSchema = Type.Object({
279
279
id : Type . Integer ( ) ,
280
280
schema : Type . String ( ) ,
281
281
name : Type . String ( ) ,
282
+ is_updatable : Type . Boolean ( ) ,
282
283
comment : Type . Union ( [ Type . String ( ) , Type . Null ( ) ] ) ,
284
+ columns : Type . Array ( postgresColumnSchema ) ,
283
285
} )
284
286
export type PostgresView = Static < typeof postgresViewSchema >
Original file line number Diff line number Diff line change @@ -6,8 +6,68 @@ test('list', async () => {
6
6
{ id : expect . any ( Number ) } ,
7
7
`
8
8
Object {
9
+ "columns": Array [
10
+ Object {
11
+ "comment": null,
12
+ "data_type": "bigint",
13
+ "default_value": null,
14
+ "enums": Array [],
15
+ "format": "int8",
16
+ "id": "16420.1",
17
+ "identity_generation": null,
18
+ "is_generated": false,
19
+ "is_identity": false,
20
+ "is_nullable": true,
21
+ "is_unique": false,
22
+ "is_updatable": true,
23
+ "name": "id",
24
+ "ordinal_position": 1,
25
+ "schema": "public",
26
+ "table": "todos_view",
27
+ "table_id": 16420,
28
+ },
29
+ Object {
30
+ "comment": null,
31
+ "data_type": "text",
32
+ "default_value": null,
33
+ "enums": Array [],
34
+ "format": "text",
35
+ "id": "16420.2",
36
+ "identity_generation": null,
37
+ "is_generated": false,
38
+ "is_identity": false,
39
+ "is_nullable": true,
40
+ "is_unique": false,
41
+ "is_updatable": true,
42
+ "name": "details",
43
+ "ordinal_position": 2,
44
+ "schema": "public",
45
+ "table": "todos_view",
46
+ "table_id": 16420,
47
+ },
48
+ Object {
49
+ "comment": null,
50
+ "data_type": "bigint",
51
+ "default_value": null,
52
+ "enums": Array [],
53
+ "format": "int8",
54
+ "id": "16420.3",
55
+ "identity_generation": null,
56
+ "is_generated": false,
57
+ "is_identity": false,
58
+ "is_nullable": true,
59
+ "is_unique": false,
60
+ "is_updatable": true,
61
+ "name": "user-id",
62
+ "ordinal_position": 3,
63
+ "schema": "public",
64
+ "table": "todos_view",
65
+ "table_id": 16420,
66
+ },
67
+ ],
9
68
"comment": null,
10
69
"id": Any<Number>,
70
+ "is_updatable": true,
11
71
"name": "todos_view",
12
72
"schema": "public",
13
73
}
You can’t perform that action at this time.
0 commit comments