Query Builder Mutation Behavior

is it expected that doing something like

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


would cause the
qb
to mutated?

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,
  };
}
Was this page helpful?