Skip to content

Commit 364d18c

Browse files
authored
Fix .count() in queries with group by defined (#59)
1 parent e84ab42 commit 364d18c

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "workers-qb",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"description": "Zero dependencies Query Builder for Cloudflare Workers",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/builder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
111111
...params,
112112
fields: 'count(*) as total',
113113
offset: undefined,
114+
groupBy: undefined,
114115
limit: 1,
115116
}),
116117
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
@@ -134,6 +135,7 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
134135
...params,
135136
fields: 'count(*) as total',
136137
offset: undefined,
138+
groupBy: undefined,
137139
limit: 1,
138140
}),
139141
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params

tests/builder/select.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ describe('Select Builder', () => {
160160
}
161161
})
162162

163+
test('count should remove group by', async () => {
164+
for (const result of [
165+
await new QuerybuilderTest()
166+
.fetchAll({
167+
tableName: 'testTable',
168+
offset: 4,
169+
groupBy: ['field'],
170+
})
171+
.count(),
172+
]) {
173+
expect((result.results as any).query).toEqual('SELECT count(*) as total FROM testTable LIMIT 1')
174+
expect((result.results as any).arguments).toEqual(undefined)
175+
expect((result.results as any).fetchType).toEqual('ONE')
176+
}
177+
})
178+
163179
test('select with simplified where list', async () => {
164180
for (const result of [
165181
new QuerybuilderTest().fetchOne({

0 commit comments

Comments
 (0)