Skip to content

Commit

Permalink
fix(types): aggregate on a missing column with alias
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Oct 18, 2024
1 parent e01b7c2 commit c6c2795
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/select-query-parser/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,21 @@ type ProcessSimpleField<
Row extends Record<string, unknown>,
RelationName extends string,
Field extends Ast.FieldNode
> = Field['aggregateFunction'] extends AggregateFunctions
? {
// An aggregate function will always override the column name id.sum() will become sum
// except if it has been aliased
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes
? TypeScriptTypes<Field['castType']>
: number
}
: [Field['name']] extends [keyof Row]
? {
// Aliases override the property name in the result
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes // We apply the detected casted as the result type
? TypeScriptTypes<Field['castType']>
: Row[Field['name']]
}
> = Field['name'] extends keyof Row | 'count'
? Field['aggregateFunction'] extends AggregateFunctions
? {
// An aggregate function will always override the column name id.sum() will become sum
// except if it has been aliased
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes
? TypeScriptTypes<Field['castType']>
: number
}
: {
// Aliases override the property name in the result
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes // We apply the detected casted as the result type
? TypeScriptTypes<Field['castType']>
: Row[Field['name']]
}
: SelectQueryError<`column '${Field['name']}' does not exist on '${RelationName}'.`>

/**
Expand Down
25 changes: 25 additions & 0 deletions test/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ export const selectParams = {
from: 'collections',
select: '*, parent_id(*)',
},
aggregateOnMissingColumnWithAlias: {
from: 'users',
select: 'alias:missing_column.count()',
},
} as const

export const selectQueries = {
Expand Down Expand Up @@ -349,6 +353,9 @@ export const selectQueries = {
selfReferenceRelationViaColumn: postgrest
.from(selectParams.selfReferenceRelationViaColumn.from)
.select(selectParams.selfReferenceRelationViaColumn.select),
aggregateOnMissingColumnWithAlias: postgrest
.from(selectParams.aggregateOnMissingColumnWithAlias.from)
.select(selectParams.aggregateOnMissingColumnWithAlias.select),
} as const

test('nested query with selective fields', async () => {
Expand Down Expand Up @@ -1806,3 +1813,21 @@ test('self reference relation via column', async () => {
}
`)
})

test('aggregate on missing column with alias', async () => {
const res = await selectQueries.aggregateOnMissingColumnWithAlias.eq('id', 1).limit(1).single()
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": null,
"error": Object {
"code": "42703",
"details": null,
"hint": null,
"message": "column users.missing_column does not exist",
},
"status": 400,
"statusText": "Bad Request",
}
`)
})
7 changes: 7 additions & 0 deletions test/select-query-parser/select.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,3 +736,10 @@ type Schema = Database['public']
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}

// aggregate on missing column with alias
{
const { data, error } = await selectQueries.aggregateOnMissingColumnWithAlias.limit(1).single()
if (error) throw error
expectType<SelectQueryError<`column 'missing_column' does not exist on 'users'.`>>(data)
}

0 comments on commit c6c2795

Please sign in to comment.