Typed results from QueryBuilder

Is there a way to infer result type from executing a query built with QueryBuilder?

export interface NeedsToBeManuallyTyped extends Record<string, unknown> {
    customerId: number;
    amount: number;
    lastTimestamp: string;
}

export function getLeaderboardDataSourceQuery(
    currencyId: number,
    period: Period,
    customerId?: number,
) {
    const _afterDateTime = zdtToISO(period.startDate);
    const _beforeDateTime = zdtToISO(period.endDate);

    return new QueryBuilder()
        .select({
            customerId: balanceEventsTable.customerId,
            amount: sql<number>`CAST(SUM(${balanceEventsTable.amount}) AS INTEGER) AS amount`,
            lastTimestamp: sql<string>`MAX(${balanceEventsTable.timestamp}) AS last_timestamp`,
        })
        .from(balanceEventsTable)
        .where(
            and(
                eq(balanceEventsTable.currencyId, currencyId),
                gte(balanceEventsTable.timestamp, _afterDateTime),
                lt(balanceEventsTable.timestamp, _beforeDateTime),
                customerId ? eq(balanceEventsTable.customerId, customerId) : undefined,
            ),
        )
        .groupBy(balanceEventsTable.customerId)
        .orderBy(sql`amount DESC, last_timestamp ASC`)
        .limit(10_000);
}
//
const results = await db.execute<NeedsToBeManuallyTyped>(getLeaderboardDataSourceQuery(currencyId, period))
Was this page helpful?