Skip to content

Commit 4dcc6b4

Browse files
mahmoudzeyadabenjamin658
mahmoudzeyada
authored andcommitted
fix($Paginator): clone a query builder to prevent mutating the actual passed one
currently, the paginate function takes a query builder and mutates the query builder it takes, This behavior can be problematic if someone intends to use the query builder again after passing it to paginate function ex. getting the total count with or after pagination. this commit contains fixes on both paginator class and tests - contains a fix to the issue by just cloning the passed query builder with the class SelectQueryBuilder that clones the query builder with its query runner. - contains a refactor to the paginator tests by not using the clone method anymore when passing the same query builder to multiple paginate function.
1 parent 6b991b9 commit 4dcc6b4

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Paginator.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export default class Paginator<Entity> {
116116
builder: SelectQueryBuilder<Entity>,
117117
): SelectQueryBuilder<Entity> {
118118
const cursors: CursorParam = {};
119+
const clonedBuilder = new SelectQueryBuilder<Entity>(builder)
119120

120121
if (this.hasAfterCursor()) {
121122
Object.assign(cursors, this.decode(this.afterCursor as string));
@@ -124,15 +125,15 @@ export default class Paginator<Entity> {
124125
}
125126

126127
if (Object.keys(cursors).length > 0) {
127-
builder.andWhere(
128+
clonedBuilder.andWhere(
128129
new Brackets((where) => this.buildCursorQuery(where, cursors)),
129130
);
130131
}
131132

132-
builder.take(this.limit + 1);
133-
builder.orderBy(this.buildOrder());
133+
clonedBuilder.take(this.limit + 1);
134+
clonedBuilder.orderBy(this.buildOrder());
134135

135-
return builder;
136+
return clonedBuilder;
136137
}
137138

138139
private buildCursorQuery(

0 commit comments

Comments
 (0)