Skip to content

Commit c6c2795

Browse files
committed
fix(types): aggregate on a missing column with alias
1 parent e01b7c2 commit c6c2795

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

src/select-query-parser/result.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,21 @@ type ProcessSimpleField<
165165
Row extends Record<string, unknown>,
166166
RelationName extends string,
167167
Field extends Ast.FieldNode
168-
> = Field['aggregateFunction'] extends AggregateFunctions
169-
? {
170-
// An aggregate function will always override the column name id.sum() will become sum
171-
// except if it has been aliased
172-
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes
173-
? TypeScriptTypes<Field['castType']>
174-
: number
175-
}
176-
: [Field['name']] extends [keyof Row]
177-
? {
178-
// Aliases override the property name in the result
179-
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes // We apply the detected casted as the result type
180-
? TypeScriptTypes<Field['castType']>
181-
: Row[Field['name']]
182-
}
168+
> = Field['name'] extends keyof Row | 'count'
169+
? Field['aggregateFunction'] extends AggregateFunctions
170+
? {
171+
// An aggregate function will always override the column name id.sum() will become sum
172+
// except if it has been aliased
173+
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes
174+
? TypeScriptTypes<Field['castType']>
175+
: number
176+
}
177+
: {
178+
// Aliases override the property name in the result
179+
[K in GetFieldNodeResultName<Field>]: Field['castType'] extends PostgreSQLTypes // We apply the detected casted as the result type
180+
? TypeScriptTypes<Field['castType']>
181+
: Row[Field['name']]
182+
}
183183
: SelectQueryError<`column '${Field['name']}' does not exist on '${RelationName}'.`>
184184

185185
/**

test/relationships.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ export const selectParams = {
177177
from: 'collections',
178178
select: '*, parent_id(*)',
179179
},
180+
aggregateOnMissingColumnWithAlias: {
181+
from: 'users',
182+
select: 'alias:missing_column.count()',
183+
},
180184
} as const
181185

182186
export const selectQueries = {
@@ -349,6 +353,9 @@ export const selectQueries = {
349353
selfReferenceRelationViaColumn: postgrest
350354
.from(selectParams.selfReferenceRelationViaColumn.from)
351355
.select(selectParams.selfReferenceRelationViaColumn.select),
356+
aggregateOnMissingColumnWithAlias: postgrest
357+
.from(selectParams.aggregateOnMissingColumnWithAlias.from)
358+
.select(selectParams.aggregateOnMissingColumnWithAlias.select),
352359
} as const
353360

354361
test('nested query with selective fields', async () => {
@@ -1806,3 +1813,21 @@ test('self reference relation via column', async () => {
18061813
}
18071814
`)
18081815
})
1816+
1817+
test('aggregate on missing column with alias', async () => {
1818+
const res = await selectQueries.aggregateOnMissingColumnWithAlias.eq('id', 1).limit(1).single()
1819+
expect(res).toMatchInlineSnapshot(`
1820+
Object {
1821+
"count": null,
1822+
"data": null,
1823+
"error": Object {
1824+
"code": "42703",
1825+
"details": null,
1826+
"hint": null,
1827+
"message": "column users.missing_column does not exist",
1828+
},
1829+
"status": 400,
1830+
"statusText": "Bad Request",
1831+
}
1832+
`)
1833+
})

test/select-query-parser/select.test-d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,10 @@ type Schema = Database['public']
736736
}
737737
expectType<TypeEqual<typeof result, typeof expected>>(true)
738738
}
739+
740+
// aggregate on missing column with alias
741+
{
742+
const { data, error } = await selectQueries.aggregateOnMissingColumnWithAlias.limit(1).single()
743+
if (error) throw error
744+
expectType<SelectQueryError<`column 'missing_column' does not exist on 'users'.`>>(data)
745+
}

0 commit comments

Comments
 (0)