Skip to content

Commit dbe8d2b

Browse files
authored
Merge branch 'master' into avallete/fix-indexes-array-results
2 parents bdab758 + 8418eaa commit dbe8d2b

File tree

7 files changed

+94
-29
lines changed

7 files changed

+94
-29
lines changed

src/lib/PostgresMetaTypes.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,36 @@ export default class PostgresMetaTypes {
1111
}
1212

1313
async list({
14+
includeTableTypes = false,
1415
includeArrayTypes = false,
1516
includeSystemSchemas = false,
1617
includedSchemas,
1718
excludedSchemas,
1819
limit,
1920
offset,
2021
}: {
22+
includeTableTypes?: boolean
2123
includeArrayTypes?: boolean
2224
includeSystemSchemas?: boolean
2325
includedSchemas?: string[]
2426
excludedSchemas?: string[]
2527
limit?: number
2628
offset?: number
2729
} = {}): Promise<PostgresMetaResult<PostgresType[]>> {
28-
let sql = typesSql
30+
let sql = `${typesSql}
31+
where
32+
(
33+
t.typrelid = 0
34+
or (
35+
select
36+
c.relkind ${includeTableTypes ? `in ('c', 'r')` : `= 'c'`}
37+
from
38+
pg_class c
39+
where
40+
c.oid = t.typrelid
41+
)
42+
)
43+
`
2944
if (!includeArrayTypes) {
3045
sql += ` and not exists (
3146
select

src/lib/generators.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import {
44
PostgresForeignTable,
55
PostgresFunction,
66
PostgresMaterializedView,
7+
PostgresMetaResult,
78
PostgresRelationship,
89
PostgresSchema,
910
PostgresTable,
1011
PostgresType,
1112
PostgresView,
1213
} from './types.js'
13-
import { PostgresMetaResult } from './types.js'
1414

1515
export type GeneratorMetadata = {
1616
schemas: PostgresSchema[]
@@ -98,6 +98,7 @@ export async function getGeneratorMetadata(
9898
}
9999

100100
const { data: types, error: typesError } = await pgMeta.types.list({
101+
includeTableTypes: true,
101102
includeArrayTypes: true,
102103
includeSystemSchemas: true,
103104
})

src/lib/sql/types.sql

-12
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,3 @@ from
3333
group by
3434
c.oid
3535
) as t_attributes on t_attributes.oid = t.typrelid
36-
where
37-
(
38-
t.typrelid = 0
39-
or (
40-
select
41-
c.relkind = 'c'
42-
from
43-
pg_class c
44-
where
45-
c.oid = t.typrelid
46-
)
47-
)

src/server/server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ async function getTypeOutput(): Promise<string | null> {
7777
GENERATE_TYPES_INCLUDED_SCHEMAS.length > 0 ? GENERATE_TYPES_INCLUDED_SCHEMAS : undefined,
7878
}),
7979
pgMeta.types.list({
80+
includeTableTypes: true,
8081
includeArrayTypes: true,
8182
includeSystemSchemas: true,
8283
}),

test/db/00-init.sql

+4
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,7 @@ create table table_with_primary_key_other_than_id (
137137
other_id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
138138
name text
139139
);
140+
141+
create type composite_type_with_record_attribute as (
142+
todo todos
143+
);

test/lib/types.ts

+31
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,37 @@ test('list types with excluded schemas and include System Schemas', async () =>
5757
})
5858
})
5959

60+
test('list types with include Table Types', async () => {
61+
const res = await pgMeta.types.list({
62+
includeTableTypes: true,
63+
})
64+
65+
expect(res.data?.find(({ name }) => name === 'todos')).toMatchInlineSnapshot(
66+
{ id: expect.any(Number) },
67+
`
68+
{
69+
"attributes": [],
70+
"comment": null,
71+
"enums": [],
72+
"format": "todos",
73+
"id": Any<Number>,
74+
"name": "todos",
75+
"schema": "public",
76+
}
77+
`
78+
)
79+
})
80+
81+
test('list types without Table Types', async () => {
82+
const res = await pgMeta.types.list({
83+
includeTableTypes: false,
84+
})
85+
86+
res.data?.forEach((type) => {
87+
expect(type.name).not.toBe('todos')
88+
})
89+
})
90+
6091
test('composite type attributes', async () => {
6192
await pgMeta.query(`create type test_composite as (id int8, data text);`)
6293

test/server/typegen.ts

+40-15
Original file line numberDiff line numberDiff line change
@@ -333,31 +333,31 @@ test('typegen: typescript', async () => {
333333
Functions: {
334334
blurb: {
335335
Args: {
336-
"": unknown
336+
"": Database["public"]["Tables"]["todos"]["Row"]
337337
}
338338
Returns: string
339339
}
340340
blurb_varchar: {
341341
Args: {
342-
"": unknown
342+
"": Database["public"]["Tables"]["todos"]["Row"]
343343
}
344344
Returns: string
345345
}
346346
details_is_long: {
347347
Args: {
348-
"": unknown
348+
"": Database["public"]["Tables"]["todos"]["Row"]
349349
}
350350
Returns: boolean
351351
}
352352
details_length: {
353353
Args: {
354-
"": unknown
354+
"": Database["public"]["Tables"]["todos"]["Row"]
355355
}
356356
Returns: number
357357
}
358358
details_words: {
359359
Args: {
360-
"": unknown
360+
"": Database["public"]["Tables"]["todos"]["Row"]
361361
}
362362
Returns: string[]
363363
}
@@ -424,6 +424,9 @@ test('typegen: typescript', async () => {
424424
composite_type_with_array_attribute: {
425425
my_text_array: string[] | null
426426
}
427+
composite_type_with_record_attribute: {
428+
todo: Database["public"]["Tables"]["todos"]["Row"] | null
429+
}
427430
}
428431
}
429432
}
@@ -877,31 +880,31 @@ test('typegen w/ one-to-one relationships', async () => {
877880
Functions: {
878881
blurb: {
879882
Args: {
880-
"": unknown
883+
"": Database["public"]["Tables"]["todos"]["Row"]
881884
}
882885
Returns: string
883886
}
884887
blurb_varchar: {
885888
Args: {
886-
"": unknown
889+
"": Database["public"]["Tables"]["todos"]["Row"]
887890
}
888891
Returns: string
889892
}
890893
details_is_long: {
891894
Args: {
892-
"": unknown
895+
"": Database["public"]["Tables"]["todos"]["Row"]
893896
}
894897
Returns: boolean
895898
}
896899
details_length: {
897900
Args: {
898-
"": unknown
901+
"": Database["public"]["Tables"]["todos"]["Row"]
899902
}
900903
Returns: number
901904
}
902905
details_words: {
903906
Args: {
904-
"": unknown
907+
"": Database["public"]["Tables"]["todos"]["Row"]
905908
}
906909
Returns: string[]
907910
}
@@ -968,6 +971,9 @@ test('typegen w/ one-to-one relationships', async () => {
968971
composite_type_with_array_attribute: {
969972
my_text_array: string[] | null
970973
}
974+
composite_type_with_record_attribute: {
975+
todo: Database["public"]["Tables"]["todos"]["Row"] | null
976+
}
971977
}
972978
}
973979
}
@@ -1421,31 +1427,31 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
14211427
Functions: {
14221428
blurb: {
14231429
Args: {
1424-
"": unknown
1430+
"": Database["public"]["Tables"]["todos"]["Row"]
14251431
}
14261432
Returns: string
14271433
}
14281434
blurb_varchar: {
14291435
Args: {
1430-
"": unknown
1436+
"": Database["public"]["Tables"]["todos"]["Row"]
14311437
}
14321438
Returns: string
14331439
}
14341440
details_is_long: {
14351441
Args: {
1436-
"": unknown
1442+
"": Database["public"]["Tables"]["todos"]["Row"]
14371443
}
14381444
Returns: boolean
14391445
}
14401446
details_length: {
14411447
Args: {
1442-
"": unknown
1448+
"": Database["public"]["Tables"]["todos"]["Row"]
14431449
}
14441450
Returns: number
14451451
}
14461452
details_words: {
14471453
Args: {
1448-
"": unknown
1454+
"": Database["public"]["Tables"]["todos"]["Row"]
14491455
}
14501456
Returns: string[]
14511457
}
@@ -1512,6 +1518,9 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
15121518
composite_type_with_array_attribute: {
15131519
my_text_array: string[] | null
15141520
}
1521+
composite_type_with_record_attribute: {
1522+
todo: Database["public"]["Tables"]["todos"]["Row"] | null
1523+
}
15151524
}
15161525
}
15171526
}
@@ -1803,6 +1812,10 @@ test('typegen: go', async () => {
18031812
18041813
type PublicCompositeTypeWithArrayAttribute struct {
18051814
MyTextArray interface{} \`json:"my_text_array"\`
1815+
}
1816+
1817+
type PublicCompositeTypeWithRecordAttribute struct {
1818+
Todo interface{} \`json:"todo"\`
18061819
}"
18071820
`)
18081821
})
@@ -2144,6 +2157,12 @@ test('typegen: swift', async () => {
21442157
case MyTextArray = "my_text_array"
21452158
}
21462159
}
2160+
internal struct CompositeTypeWithRecordAttribute: Codable, Hashable, Sendable {
2161+
internal let Todo: TodosSelect
2162+
internal enum CodingKeys: String, CodingKey {
2163+
case Todo = "todo"
2164+
}
2165+
}
21472166
}"
21482167
`)
21492168
})
@@ -2489,6 +2508,12 @@ test('typegen: swift w/ public access control', async () => {
24892508
case MyTextArray = "my_text_array"
24902509
}
24912510
}
2511+
public struct CompositeTypeWithRecordAttribute: Codable, Hashable, Sendable {
2512+
public let Todo: TodosSelect
2513+
public enum CodingKeys: String, CodingKey {
2514+
case Todo = "todo"
2515+
}
2516+
}
24922517
}"
24932518
`)
24942519
})

0 commit comments

Comments
 (0)