Skip to content

Commit e2fe8d5

Browse files
authored
Merge pull request #333 from supabase/feat/views-columns
feat(views): add `is_updatable` & `columns`
2 parents 3e0fdad + 4f24341 commit e2fe8d5

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

src/lib/PostgresMetaViews.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { literal } from 'pg-format'
22
import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
3-
import { viewsSql } from './sql'
3+
import { coalesceRowsToArray } from './helpers'
4+
import { columnsSql, viewsSql } from './sql'
45
import { PostgresMetaResult, PostgresView } from './types'
56

67
export default class PostgresMetaViews {
@@ -19,9 +20,9 @@ export default class PostgresMetaViews {
1920
limit?: number
2021
offset?: number
2122
} = {}): Promise<PostgresMetaResult<PostgresView[]>> {
22-
let sql = viewsSql
23+
let sql = enrichedViewsSql
2324
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(',')})`
2526
}
2627
if (limit) {
2728
sql = `${sql} LIMIT ${limit}`
@@ -32,3 +33,11 @@ export default class PostgresMetaViews {
3233
return await this.query(sql)
3334
}
3435
}
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`

src/lib/sql/views.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ SELECT
22
c.oid :: int8 AS id,
33
n.nspname AS schema,
44
c.relname AS name,
5+
-- See definition of information_schema.views
6+
(pg_relation_is_updatable(c.oid, false) & 20) = 20 AS is_updatable,
57
obj_description(c.oid) AS comment
68
FROM
79
pg_class c

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ export const postgresViewSchema = Type.Object({
279279
id: Type.Integer(),
280280
schema: Type.String(),
281281
name: Type.String(),
282+
is_updatable: Type.Boolean(),
282283
comment: Type.Union([Type.String(), Type.Null()]),
284+
columns: Type.Array(postgresColumnSchema),
283285
})
284286
export type PostgresView = Static<typeof postgresViewSchema>

test/lib/views.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,68 @@ test('list', async () => {
66
{ id: expect.any(Number) },
77
`
88
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+
],
968
"comment": null,
1069
"id": Any<Number>,
70+
"is_updatable": true,
1171
"name": "todos_view",
1272
"schema": "public",
1373
}

0 commit comments

Comments
 (0)