Is there a way to properly type a "generic query"

Is there a way to properly type a "generic query" with drizzle, rather than trying to duck-type it like this
export async function paginateWithCursor<
T,
TQuery extends {
where: (condition: SQL) => TQuery;
orderBy: (...fields: SQL[]) => TQuery;
limit: (limit: number) => TQuery;
} & Promise<T[]>,
>(
baseQuery: TQuery,
cursorConfig: CursorConfig,
options?: CursorPaginationOptions,
): Promise<CursorPaginationResult<T>> {
export async function paginateWithCursor<
T,
TQuery extends {
where: (condition: SQL) => TQuery;
orderBy: (...fields: SQL[]) => TQuery;
limit: (limit: number) => TQuery;
} & Promise<T[]>,
>(
baseQuery: TQuery,
cursorConfig: CursorConfig,
options?: CursorPaginationOptions,
): Promise<CursorPaginationResult<T>> {
1 Reply
JustWayne
JustWayne3mo ago
I have been using PgTable & Record<"id" | "other_field" | "that_id_need", AnyPgColumn> in order to accept table schemas, however sometimes I have to cast table as any to the query builder and then also the return type must be cast as well. I just dumped a thing I'm working on for my own project where I want to have reusable functions that I can just pass my table schemas into - it's completely untested at this time, but I did a lot of work this week to figure out the Drizzle types. Check this comment for the small gist of it - https://github.com/drizzle-team/drizzle-orm/issues/1644#issuecomment-3058858399 - and expand the EDIT section to see the solution. And here's the dump including today's progress - https://gist.github.com/waynesbrain/707547d58fda5f33bc452a8fdf54516a

Did you find this page helpful?