Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
27 changes: 26 additions & 1 deletion test/types.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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)[]'
)
})
})
Loading