Generic type for select list query so that Awaited<T> returns unknown[] #439
-
|
I am working on a generic pagination library, supporting both offset pagination and cursor pagination on multiple fields. I currently have some ad-hoc code in my project, which looks more or less like below (some code removed for brevity): import type { Query } from "orchid-orm"
type ListQuery = Query & { returnType: undefined | "all" }
export type PagePaginationPage<T extends ListQuery = ListQuery> = {
items: Awaited<T>
offset: number
limit: number
pages: number
count: number
}
export async function paginateWithPage<T extends ListQuery>(query: T, event: H3Event, config: PaginationConfig): Promise<PagePaginationPage<T>> {
const params = await getValidatedQuery(event, parseQuery)
const limit = Math.max(1, Math.min(params.limit ?? config.limit, config.limit))
const count = await query.clear("order").count() as unknown as number
const pages = Math.ceil(count / limit)
const page = Math.max(1, Math.min(params.page ?? 1, pages))
const offset = (page - 1) * limit
const items = await query.offset(offset).limit(limit)
return { items, offset, limit, pages, count }
}which is then consumed as: // HTTP handler
export default defineEventHandler(async (event) => {
const page = await paginateWithCursor(db.user.select(...).where(...), event, { limit: 10 })
return page
})The proof of concept works, what I'm struggling with is that I tried |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Beta Was this translation helpful? Give feedback.

I see, you can override
thenof theQuery: