How to create a generic function that creates select query with the provided fields? #2689
Unanswered
romario333
asked this question in
Q&A
Replies: 1 comment
-
Not exactly the same implementation you were going for, but Drizzle has the following example in their documentation under "Guides" for implementing reusable pagination logic: import { SQL, asc } from 'drizzle-orm';
import { PgColumn, PgSelect } from 'drizzle-orm/pg-core';
function withPagination<T extends PgSelect>(
qb: T,
orderByColumn: PgColumn | SQL | SQL.Aliased,
page = 1,
pageSize = 3,
) {
return qb
.orderBy(orderByColumn)
.limit(pageSize)
.offset((page - 1) * pageSize);
}
const query = db.select().from(users); // query that you want to execute with pagination
await withPagination(query.$dynamic(), asc(users.id)); Source: https://orm.drizzle.team/docs/guides/limit-offset-pagination |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I need to run the same query twice for pagination:
limit
andoffset
.The core of the query is identical in both cases, differing only in the fields they select and limit being applied. To avoid repeating the query logic I have created a function to generate it:
This approach works, but I lose type safety. I haven’t figured out how to properly type the
query
function. Does anyone have any ideas how to make this work, or how to approach this problem differently?Beta Was this translation helpful? Give feedback.
All reactions