Skip to content

Commit

Permalink
Fix .count() in queries with group by defined
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Sep 21, 2024
1 parent e84ab42 commit 7915a33
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "workers-qb",
"version": "1.6.2",
"version": "1.6.3",
"description": "Zero dependencies Query Builder for Cloudflare Workers",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
2 changes: 2 additions & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
...params,
fields: 'count(*) as total',
offset: undefined,
groupBy: undefined,
limit: 1,
}),
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
Expand All @@ -134,6 +135,7 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
...params,
fields: 'count(*) as total',
offset: undefined,
groupBy: undefined,
limit: 1,
}),
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
Expand Down
16 changes: 16 additions & 0 deletions tests/builder/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ describe('Select Builder', () => {
}
})

test('count should remove group by', async () => {
for (const result of [
await new QuerybuilderTest()
.fetchAll({
tableName: 'testTable',
offset: 4,
groupBy: ['field'],
})
.count(),
]) {
expect((result.results as any).query).toEqual('SELECT count(*) as total FROM testTable LIMIT 1')
expect((result.results as any).arguments).toEqual(undefined)
expect((result.results as any).fetchType).toEqual('ONE')
}
})

test('select with simplified where list', async () => {
for (const result of [
new QuerybuilderTest().fetchOne({
Expand Down

0 comments on commit 7915a33

Please sign in to comment.