Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added clearGroupBy() #921

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added clearGroupBy
  • Loading branch information
dswbx committed Mar 24, 2024
commit 06616c0312c45cd7ad91621439b6d9d414fa1d9d
7 changes: 7 additions & 0 deletions src/operation-node/select-query-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 218,11 @@ export const SelectQueryNode = freeze({
orderBy: undefined,
})
},

cloneWithoutGroupBy(select: SelectQueryNode): SelectQueryNode {
return freeze({
...select,
groupBy: undefined,
})
},
})
27 changes: 27 additions & 0 deletions src/query-builder/select-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 1500,26 @@ export interface SelectQueryBuilder<DB, TB extends keyof DB, O>
*/
clearOrderBy(): SelectQueryBuilder<DB, TB, O>

/**
* Clears `group by` clause from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .orderBy('id')
* .clearGroupBy()
koskimas marked this conversation as resolved.
Show resolved Hide resolved
* ```
*
* The generated SQL(PostgreSQL):
koskimas marked this conversation as resolved.
Show resolved Hide resolved
*
* ```sql
* select * from "person"
koskimas marked this conversation as resolved.
Show resolved Hide resolved
* ```
*/
clearGroupBy(): SelectQueryBuilder<DB, TB, O>

/**
* Simply calls the provided function passing `this` as the only argument. `$call` returns
* what the provided function returns.
Expand Down Expand Up @@ -2279,6 2299,13 @@ class SelectQueryBuilderImpl<DB, TB extends keyof DB, O>
})
}

clearGroupBy(): SelectQueryBuilder<DB, TB, O> {
return new SelectQueryBuilderImpl<DB, TB, O>({
...this.#props,
queryNode: SelectQueryNode.cloneWithoutGroupBy(this.#props.queryNode),
})
}

$call<T>(func: (qb: this) => T): T {
return func(this)
}
Expand Down
27 changes: 27 additions & 0 deletions test/node/src/clear.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 395,32 @@ for (const dialect of DIALECTS) {
},
})
})

it('should clear groupBy', () => {
const query = ctx.db
.selectFrom('person')
.selectAll()
.groupBy('id')
.clearGroupBy()

testSql(query, dialect, {
postgres: {
sql: `select * from "person"`,
parameters: [],
},
mysql: {
sql: 'select * from `person`',
parameters: [],
},
mssql: {
sql: `select * from "person"`,
parameters: [],
},
sqlite: {
sql: `select * from "person"`,
parameters: [],
},
})
})
})
}
Loading