Query Builder Mutation Behavior

is it expected that doing something like
const paginatedQuery = qb.limit(pageSize).offset((page - 1) * pageSize);
const paginatedQuery = qb.limit(pageSize).offset((page - 1) * pageSize);
would cause the
qb
qb
to mutated?
page=1&pageSize=20
page=1&pageSize=20
My code and output:
export async function withPagination<TQuery extends PgSelect>(
qb: TQuery,
options: PaginationOptions,
): Promise<PaginatedResult<TQuery["_"]["result"][number]>> {
const { page, pageSize } = options;

console.log("Original query:", qb.toSQL());
/* Output:
Original query: {
sql: 'select "id", "creator_id", "event", "status", "created_at", "updated_at", "deleted_at" from "events" order by "events"."created_at" desc',
params: []
}
*/

const firstCount = await db.$count(qb.as("sq"));
/* Output:
First count: 18
*/

const paginatedQuery = qb.limit(pageSize).offset((page - 1) * pageSize);

//After this line , the qb would mutated?

console.log("Query after pagination:", qb.toSQL());
/* Output:
Query after pagination: {
sql: 'select "id", "creator_id", "event", "status", "created_at", "updated_at", "deleted_at" from "events" order by "events"."created_at" desc limit $1',
params: [ 10 ]
}
*/

console.log("Paginated query:", paginatedQuery.toSQL());
/* Output:
Paginated query: {
sql: 'select "id", "creator_id", "event", "status", "created_at", "updated_at", "deleted_at" from "events" order by "events"."created_at" desc limit $1',
params: [ 10 ]
}
*/

console.log("Are qb and paginatedQuery the same?", qb === paginatedQuery);
/* Output:
Are qb and paginatedQuery the same? true
*/

const items = await paginatedQuery;
const count = await db.$count(qb.as("sq"));
/* Output:
Total count: 10
*/

return {
items,
currentPage: page,
totalPages: Math.ceil(count / pageSize),
totalItems: count,
pageSize,
};
}
export async function withPagination<TQuery extends PgSelect>(
qb: TQuery,
options: PaginationOptions,
): Promise<PaginatedResult<TQuery["_"]["result"][number]>> {
const { page, pageSize } = options;

console.log("Original query:", qb.toSQL());
/* Output:
Original query: {
sql: 'select "id", "creator_id", "event", "status", "created_at", "updated_at", "deleted_at" from "events" order by "events"."created_at" desc',
params: []
}
*/

const firstCount = await db.$count(qb.as("sq"));
/* Output:
First count: 18
*/

const paginatedQuery = qb.limit(pageSize).offset((page - 1) * pageSize);

//After this line , the qb would mutated?

console.log("Query after pagination:", qb.toSQL());
/* Output:
Query after pagination: {
sql: 'select "id", "creator_id", "event", "status", "created_at", "updated_at", "deleted_at" from "events" order by "events"."created_at" desc limit $1',
params: [ 10 ]
}
*/

console.log("Paginated query:", paginatedQuery.toSQL());
/* Output:
Paginated query: {
sql: 'select "id", "creator_id", "event", "status", "created_at", "updated_at", "deleted_at" from "events" order by "events"."created_at" desc limit $1',
params: [ 10 ]
}
*/

console.log("Are qb and paginatedQuery the same?", qb === paginatedQuery);
/* Output:
Are qb and paginatedQuery the same? true
*/

const items = await paginatedQuery;
const count = await db.$count(qb.as("sq"));
/* Output:
Total count: 10
*/

return {
items,
currentPage: page,
totalPages: Math.ceil(count / pageSize),
totalItems: count,
pageSize,
};
}
2 Replies
Ave
AveOP3w ago
i am on postgrres with
"drizzle-orm": "0.36.3",
"drizzle-zod": "^0.5.1",
"drizzle-orm": "0.36.3",
"drizzle-zod": "^0.5.1",
rphlmr ⚡
rphlmr ⚡3w ago
looks like ok for me since paginatedQuery is a ref to qb. It's the same instance in 2 variables
Want results from more Discord servers?
Add your server