diff --git a/src/server/templates/typescript.ts b/src/server/templates/typescript.ts index 352c4ddc..48a395cc 100644 --- a/src/server/templates/typescript.ts +++ b/src/server/templates/typescript.ts @@ -421,7 +421,7 @@ export const apply = async ({ const type = typesById.get(type_id) let tsType = 'unknown' if (type) { - tsType = pgTypeToTsType(schema, type.name, { + tsType = pgTypeToTsRpcArgType(schema, type.name, { types, schemas, tables, @@ -440,7 +440,7 @@ export const apply = async ({ const type = typesById.get(type_id) let tsType = 'unknown' if (type) { - tsType = pgTypeToTsType(schema, type.name, { + tsType = pgTypeToTsRpcArgType(schema, type.name, { types, schemas, tables, @@ -457,7 +457,7 @@ export const apply = async ({ const type = typesById.get(type_id) let tsType = 'unknown' if (type) { - tsType = pgTypeToTsType(schema, type.name, { + tsType = pgTypeToTsRpcArgType(schema, type.name, { types, schemas, tables, @@ -972,3 +972,22 @@ export const pgTypeToTsType = ( return 'unknown' } } + +export const pgTypeToTsRpcArgType = ( + schema: PostgresSchema, + pgType: string, + context: { + types: PostgresType[] + schemas: PostgresSchema[] + tables: PostgresTable[] + views: PostgresView[] + } +): string => { + if (pgType === 'int8') { + return 'number | bigint' + } + if (pgType.startsWith('_')) { + return `(${pgTypeToTsRpcArgType(schema, pgType.substring(1), context)})[]` + } + return pgTypeToTsType(schema, pgType, context) +} diff --git a/test/types.test.ts b/test/types.test.ts index 8a213902..36eddc12 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -1,7 +1,7 @@ import { expect, test, describe } from 'vitest' import { build } from '../src/server/app.js' import { TEST_CONNECTION_STRING } from './lib/utils.js' -import { pgTypeToTsType } from '../src/server/templates/typescript' +import { pgTypeToTsRpcArgType, pgTypeToTsType } from '../src/server/templates/typescript' describe('server/routes/types', () => { test('should list types', async () => { @@ -55,4 +55,29 @@ describe('server/routes/types', () => { expect(result).toBe('string') }) + + test('int8 maps to number', () => { + const context = { + types: [], + schemas: [], + tables: [], + views: [], + } + + expect(pgTypeToTsType({ name: 'public' } as any, 'int8', context)).toBe('number') + }) + + test('int8 rpc args allow bigint', () => { + const context = { + types: [], + schemas: [], + tables: [], + views: [], + } + + expect(pgTypeToTsRpcArgType({ name: 'public' } as any, 'int8', context)).toBe('number | bigint') + expect(pgTypeToTsRpcArgType({ name: 'public' } as any, '_int8', context)).toBe( + '(number | bigint)[]' + ) + }) })