© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•15mo ago•
2 replies
Ave

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,
  };
}
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Weird Query builder behavior (extras)
Drizzle TeamDTDrizzle Team / help
3y ago
Query builder insert
Drizzle TeamDTDrizzle Team / help
3y ago
Dynamic select() query builder
Drizzle TeamDTDrizzle Team / help
12mo ago
Query builder for presto
Drizzle TeamDTDrizzle Team / help
2y ago