Skip to content

Commit b91d026

Browse files
committed
feat(types): allow bigint for int8 rpc args
1 parent be1e9de commit b91d026

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

src/server/templates/typescript.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export const apply = async ({
421421
const type = typesById.get(type_id)
422422
let tsType = 'unknown'
423423
if (type) {
424-
tsType = pgTypeToTsType(schema, type.name, {
424+
tsType = pgTypeToTsRpcArgType(schema, type.name, {
425425
types,
426426
schemas,
427427
tables,
@@ -440,7 +440,7 @@ export const apply = async ({
440440
const type = typesById.get(type_id)
441441
let tsType = 'unknown'
442442
if (type) {
443-
tsType = pgTypeToTsType(schema, type.name, {
443+
tsType = pgTypeToTsRpcArgType(schema, type.name, {
444444
types,
445445
schemas,
446446
tables,
@@ -457,7 +457,7 @@ export const apply = async ({
457457
const type = typesById.get(type_id)
458458
let tsType = 'unknown'
459459
if (type) {
460-
tsType = pgTypeToTsType(schema, type.name, {
460+
tsType = pgTypeToTsRpcArgType(schema, type.name, {
461461
types,
462462
schemas,
463463
tables,
@@ -972,3 +972,22 @@ export const pgTypeToTsType = (
972972
return 'unknown'
973973
}
974974
}
975+
976+
export const pgTypeToTsRpcArgType = (
977+
schema: PostgresSchema,
978+
pgType: string,
979+
context: {
980+
types: PostgresType[]
981+
schemas: PostgresSchema[]
982+
tables: PostgresTable[]
983+
views: PostgresView[]
984+
}
985+
): string => {
986+
if (pgType === 'int8') {
987+
return 'number | bigint'
988+
}
989+
if (pgType.startsWith('_')) {
990+
return `(${pgTypeToTsRpcArgType(schema, pgType.substring(1), context)})[]`
991+
}
992+
return pgTypeToTsType(schema, pgType, context)
993+
}

test/types.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, test, describe } from 'vitest'
22
import { build } from '../src/server/app.js'
33
import { TEST_CONNECTION_STRING } from './lib/utils.js'
4-
import { pgTypeToTsType } from '../src/server/templates/typescript'
4+
import { pgTypeToTsRpcArgType, pgTypeToTsType } from '../src/server/templates/typescript'
55

66
describe('server/routes/types', () => {
77
test('should list types', async () => {
@@ -55,4 +55,29 @@ describe('server/routes/types', () => {
5555

5656
expect(result).toBe('string')
5757
})
58+
59+
test('int8 maps to number', () => {
60+
const context = {
61+
types: [],
62+
schemas: [],
63+
tables: [],
64+
views: [],
65+
}
66+
67+
expect(pgTypeToTsType({ name: 'public' } as any, 'int8', context)).toBe('number')
68+
})
69+
70+
test('int8 rpc args allow bigint', () => {
71+
const context = {
72+
types: [],
73+
schemas: [],
74+
tables: [],
75+
views: [],
76+
}
77+
78+
expect(pgTypeToTsRpcArgType({ name: 'public' } as any, 'int8', context)).toBe('number | bigint')
79+
expect(pgTypeToTsRpcArgType({ name: 'public' } as any, '_int8', context)).toBe(
80+
'(number | bigint)[]'
81+
)
82+
})
5883
})

0 commit comments

Comments
 (0)