Skip to content

Commit a5ea005

Browse files
committed
fix: only get reltype in types
1 parent 5d002df commit a5ea005

File tree

6 files changed

+41
-39
lines changed

6 files changed

+41
-39
lines changed

src/lib/sql/functions.sql.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ from
124124
'mode', mode,
125125
'name', name,
126126
'type_id', type_id,
127-
'has_default', has_default,
128-
'table_name', table_name
127+
'has_default', has_default
129128
)) as args
130129
from
131130
(
@@ -134,11 +133,7 @@ from
134133
t2.mode,
135134
t1.name,
136135
t1.type_id,
137-
t1.has_default,
138-
case
139-
when pt.typrelid != 0 then pc.relname
140-
else null
141-
end as table_name
136+
t1.has_default
142137
from
143138
(
144139
select
@@ -161,7 +156,6 @@ from
161156
end as mode
162157
) as t2
163158
left join pg_type pt on pt.oid = t1.type_id
164-
left join pg_class pc on pc.oid = pt.typrelid
165159
order by t1.name asc
166160
) sub
167161
group by

src/lib/sql/types.sql.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ select
1313
format_type (t.oid, null) as format,
1414
coalesce(t_enums.enums, '[]') as enums,
1515
coalesce(t_attributes.attributes, '[]') as attributes,
16-
obj_description (t.oid, 'pg_type') as comment
16+
obj_description (t.oid, 'pg_type') as comment,
17+
nullif(t.typrelid::int8, 0) as type_relation_id
1718
from
1819
pg_type t
1920
left join pg_namespace n on n.oid = t.typnamespace

src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ const postgresFunctionSchema = Type.Object({
148148
name: Type.String(),
149149
type_id: Type.Number(),
150150
has_default: Type.Boolean(),
151-
table_name: Type.Union([Type.String(), Type.Null()]),
152151
})
153152
),
154153
argument_types: Type.String(),
@@ -444,6 +443,7 @@ export const postgresTypeSchema = Type.Object({
444443
enums: Type.Array(Type.String()),
445444
attributes: Type.Array(Type.Object({ name: Type.String(), type_id: Type.Integer() })),
446445
comment: Type.Union([Type.String(), Type.Null()]),
446+
type_relation_id: Type.Union([Type.Integer(), Type.Null()]),
447447
})
448448
export type PostgresType = Static<typeof postgresTypeSchema>
449449

src/server/templates/typescript.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const apply = async ({
3939

4040
const columnsByTableId: Record<number, PostgresColumn[]> = {}
4141
const tablesNamesByTableId: Record<number, string> = {}
42+
const relationTypeByIds = new Map<number, PostgresType>()
4243
// group types by id for quicker lookup
4344
const typesById = types.reduce(
4445
(acc, type) => {
@@ -148,6 +149,10 @@ export const apply = async ({
148149
}
149150
}
150151
for (const type of types) {
152+
// Save all the types that are relation types for quicker lookup
153+
if (type.type_relation_id) {
154+
relationTypeByIds.set(type.id, type)
155+
}
151156
if (type.schema in introspectionBySchema) {
152157
if (type.enums.length > 0) {
153158
introspectionBySchema[type.schema].enums.push(type)
@@ -196,10 +201,10 @@ export const apply = async ({
196201
inArgs[0].name === '' &&
197202
(VALID_UNNAMED_FUNCTION_ARG_TYPES.has(inArgs[0].type_id) ||
198203
// OR if the function have a single unnamed args which is another table (embeded function)
199-
(inArgs[0].table_name &&
204+
(relationTypeByIds.get(inArgs[0].type_id) &&
200205
getTableNameFromRelationId(func.return_type_relation_id, func.return_type_id)) ||
201206
// OR if the function takes a table row but doesn't qualify as embedded (for error reporting)
202-
(inArgs[0].table_name &&
207+
(relationTypeByIds.get(inArgs[0].type_id) &&
203208
!getTableNameFromRelationId(func.return_type_relation_id, func.return_type_id))))
204209
) {
205210
introspectionBySchema[func.schema].functions.push({ fn: func, inArgs })
@@ -227,7 +232,7 @@ export const apply = async ({
227232

228233
// Only add SetofOptions for functions with table arguments (embedded functions)
229234
// or specific functions that RETURNS table-name
230-
if (fn.args.length === 1 && fn.args[0].table_name) {
235+
if (fn.args.length === 1 && relationTypeByIds.get(fn.args[0].type_id)) {
231236
// Case 1: Standard embedded function with proper setof detection
232237
if (returnsSetOfTable && returnTableName) {
233238
setofOptionsInfo = `SetofOptions: {
@@ -337,7 +342,7 @@ export const apply = async ({
337342
if (
338343
inArgs.length === 1 &&
339344
inArgs[0].name === '' &&
340-
inArgs[0].table_name &&
345+
relationTypeByIds.get(inArgs[0].type_id) &&
341346
!getTableNameFromRelationId(fn.return_type_relation_id, fn.return_type_id)
342347
) {
343348
return true

test/lib/functions.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import { pgMeta } from './utils'
44
test('list', async () => {
55
const res = await pgMeta.functions.list()
66
expect(res.data?.find(({ name }) => name === 'add')).toMatchInlineSnapshot(
7-
{ id: expect.any(Number) }, `
7+
{ id: expect.any(Number) },
8+
`
89
{
910
"args": [
1011
{
1112
"has_default": false,
1213
"mode": "in",
1314
"name": "",
14-
"table_name": null,
1515
"type_id": 23,
1616
},
1717
{
1818
"has_default": false,
1919
"mode": "in",
2020
"name": "",
21-
"table_name": null,
2221
"type_id": 23,
2322
},
2423
],
@@ -44,7 +43,8 @@ test('list', async () => {
4443
"schema": "public",
4544
"security_definer": false,
4645
}
47-
`)
46+
`
47+
)
4848
})
4949

5050
test('list set-returning function with single object limit', async () => {
@@ -58,7 +58,6 @@ test('list set-returning function with single object limit', async () => {
5858
"has_default": false,
5959
"mode": "in",
6060
"name": "user_row",
61-
"table_name": "users",
6261
"type_id": 16395,
6362
},
6463
],
@@ -102,7 +101,6 @@ test('list set-returning function with multiples definitions', async () => {
102101
"has_default": false,
103102
"mode": "in",
104103
"name": "user_row",
105-
"table_name": "users",
106104
"type_id": 16395,
107105
},
108106
],
@@ -138,7 +136,6 @@ test('list set-returning function with multiples definitions', async () => {
138136
"has_default": false,
139137
"mode": "in",
140138
"name": "todo_row",
141-
"table_name": "todos",
142139
"type_id": 16404,
143140
},
144141
],
@@ -224,22 +221,21 @@ test('retrieve, create, update, delete', async () => {
224221
config_params: { search_path: 'hooks, auth', role: 'postgres' },
225222
})
226223
expect(res).toMatchInlineSnapshot(
227-
{ data: { id: expect.any(Number) } }, `
224+
{ data: { id: expect.any(Number) } },
225+
`
228226
{
229227
"data": {
230228
"args": [
231229
{
232230
"has_default": false,
233231
"mode": "in",
234232
"name": "a",
235-
"table_name": null,
236233
"type_id": 21,
237234
},
238235
{
239236
"has_default": false,
240237
"mode": "in",
241238
"name": "b",
242-
"table_name": null,
243239
"type_id": 21,
244240
},
245241
],
@@ -272,25 +268,25 @@ test('retrieve, create, update, delete', async () => {
272268
},
273269
"error": null,
274270
}
275-
`)
271+
`
272+
)
276273
res = await pgMeta.functions.retrieve({ id: res.data!.id })
277274
expect(res).toMatchInlineSnapshot(
278-
{ data: { id: expect.any(Number) } }, `
275+
{ data: { id: expect.any(Number) } },
276+
`
279277
{
280278
"data": {
281279
"args": [
282280
{
283281
"has_default": false,
284282
"mode": "in",
285283
"name": "a",
286-
"table_name": null,
287284
"type_id": 21,
288285
},
289286
{
290287
"has_default": false,
291288
"mode": "in",
292289
"name": "b",
293-
"table_name": null,
294290
"type_id": 21,
295291
},
296292
],
@@ -323,29 +319,29 @@ test('retrieve, create, update, delete', async () => {
323319
},
324320
"error": null,
325321
}
326-
`)
322+
`
323+
)
327324
res = await pgMeta.functions.update(res.data!.id, {
328325
name: 'test_func_renamed',
329326
schema: 'test_schema',
330327
definition: 'select b - a',
331328
})
332329
expect(res).toMatchInlineSnapshot(
333-
{ data: { id: expect.any(Number) } }, `
330+
{ data: { id: expect.any(Number) } },
331+
`
334332
{
335333
"data": {
336334
"args": [
337335
{
338336
"has_default": false,
339337
"mode": "in",
340338
"name": "a",
341-
"table_name": null,
342339
"type_id": 21,
343340
},
344341
{
345342
"has_default": false,
346343
"mode": "in",
347344
"name": "b",
348-
"table_name": null,
349345
"type_id": 21,
350346
},
351347
],
@@ -378,25 +374,25 @@ test('retrieve, create, update, delete', async () => {
378374
},
379375
"error": null,
380376
}
381-
`)
377+
`
378+
)
382379
res = await pgMeta.functions.remove(res.data!.id)
383380
expect(res).toMatchInlineSnapshot(
384-
{ data: { id: expect.any(Number) } }, `
381+
{ data: { id: expect.any(Number) } },
382+
`
385383
{
386384
"data": {
387385
"args": [
388386
{
389387
"has_default": false,
390388
"mode": "in",
391389
"name": "a",
392-
"table_name": null,
393390
"type_id": 21,
394391
},
395392
{
396393
"has_default": false,
397394
"mode": "in",
398395
"name": "b",
399-
"table_name": null,
400396
"type_id": 21,
401397
},
402398
],
@@ -429,7 +425,8 @@ test('retrieve, create, update, delete', async () => {
429425
},
430426
"error": null,
431427
}
432-
`)
428+
`
429+
)
433430
res = await pgMeta.functions.retrieve({ id: res.data!.id })
434431
expect(res).toMatchObject({
435432
data: null,
@@ -452,7 +449,8 @@ test('retrieve set-returning function', async () => {
452449
id: expect.any(Number),
453450
return_type_id: expect.any(Number),
454451
return_type_relation_id: expect.any(Number),
455-
}, `
452+
},
453+
`
456454
{
457455
"args": [],
458456
"argument_types": "",
@@ -481,7 +479,8 @@ test('retrieve set-returning function', async () => {
481479
"schema": "public",
482480
"security_definer": false,
483481
}
484-
`)
482+
`
483+
)
485484
})
486485

487486
test('retrieve function by args filter - polymorphic function with text argument', async () => {

test/lib/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ test('list', async () => {
1717
"id": Any<Number>,
1818
"name": "user_status",
1919
"schema": "public",
20+
"type_relation_id": null,
2021
}
2122
`
2223
)
@@ -73,6 +74,7 @@ test('list types with include Table Types', async () => {
7374
"id": Any<Number>,
7475
"name": "todos",
7576
"schema": "public",
77+
"type_relation_id": 16402,
7678
}
7779
`
7880
)
@@ -112,6 +114,7 @@ test('composite type attributes', async () => {
112114
"id": Any<Number>,
113115
"name": "test_composite",
114116
"schema": "public",
117+
"type_relation_id": 16966,
115118
}
116119
`
117120
)

0 commit comments

Comments
 (0)