Skip to content

Commit bc4234a

Browse files
committed
feat(lib/types): add attributes for composite types
1 parent 100b5d9 commit bc4234a

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

src/lib/sql/types.sql

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
1-
SELECT
2-
t.oid :: int8 AS id,
3-
t.typname AS name,
4-
n.nspname AS schema,
5-
format_type (t.oid, NULL) AS format,
6-
array_to_json(
7-
array(
8-
SELECT
9-
e.enumlabel
10-
FROM
11-
pg_enum e
12-
WHERE
13-
e.enumtypid = t.oid
14-
ORDER BY
15-
e.oid
16-
)
17-
) AS enums,
18-
obj_description (t.oid, 'pg_type') AS comment
19-
FROM
1+
select
2+
t.oid::int8 as id,
3+
t.typname as name,
4+
n.nspname as schema,
5+
format_type (t.oid, null) as format,
6+
coalesce(t_enums.enums, '[]') as enums,
7+
coalesce(t_attributes.attributes, '[]') as attributes,
8+
obj_description (t.oid, 'pg_type') as comment
9+
from
2010
pg_type t
21-
LEFT JOIN pg_namespace n ON n.oid = t.typnamespace
22-
WHERE
11+
left join pg_namespace n on n.oid = t.typnamespace
12+
left join (
13+
select
14+
enumtypid,
15+
jsonb_agg(enumlabel order by enumsortorder) as enums
16+
from
17+
pg_enum
18+
group by
19+
enumtypid
20+
) as t_enums on t_enums.enumtypid = t.oid
21+
left join (
22+
select
23+
oid,
24+
jsonb_agg(
25+
jsonb_build_object('name', a.attname, 'type_id', a.atttypid::int8)
26+
order by a.attnum asc
27+
) as attributes
28+
from
29+
pg_class c
30+
join pg_attribute a on a.attrelid = c.oid
31+
where
32+
c.relkind = 'c'
33+
group by
34+
c.oid
35+
) as t_attributes on t_attributes.oid = t.typrelid
36+
where
2337
(
2438
t.typrelid = 0
25-
OR (
26-
SELECT
39+
or (
40+
select
2741
c.relkind = 'c'
28-
FROM
42+
from
2943
pg_class c
30-
WHERE
44+
where
3145
c.oid = t.typrelid
3246
)
3347
)

src/lib/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const postgresColumnSchema = Type.Object({
3737
is_nullable: Type.Boolean(),
3838
is_updatable: Type.Boolean(),
3939
is_unique: Type.Boolean(),
40-
enums: Type.Array(Type.Unknown()),
40+
enums: Type.Array(Type.String()),
4141
comment: Type.Union([Type.String(), Type.Null()]),
4242
})
4343
export type PostgresColumn = Static<typeof postgresColumnSchema>
@@ -326,7 +326,13 @@ export const postgresTypeSchema = Type.Object({
326326
name: Type.String(),
327327
schema: Type.String(),
328328
format: Type.String(),
329-
enums: Type.Array(Type.Unknown()),
329+
enums: Type.Array(Type.String()),
330+
attributes: Type.Array(
331+
Type.Object({
332+
name: Type.String(),
333+
type_id: Type.Integer(),
334+
})
335+
),
330336
comment: Type.Union([Type.String(), Type.Null()]),
331337
})
332338
export type PostgresType = Static<typeof postgresTypeSchema>

0 commit comments

Comments
 (0)