Skip to content

Commit 9c5e78b

Browse files
committed
Fix .count() method for querysets with offset defined
1 parent 26bf427 commit 9c5e78b

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-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.5.1",
3+
"version": "1.5.2",
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
@@ -126,6 +126,7 @@ export class QueryBuilder<GenericResultWrapper> {
126126
this._select({
127127
...params,
128128
fields: 'count(*) as total',
129+
offset: undefined,
129130
limit: 1,
130131
}),
131132
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params
@@ -148,6 +149,7 @@ export class QueryBuilder<GenericResultWrapper> {
148149
this._select({
149150
...params,
150151
fields: 'count(*) as total',
152+
offset: undefined,
151153
limit: 1,
152154
}),
153155
typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params

tests/builder/select.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,41 @@ describe('Select Builder', () => {
125125
}
126126
})
127127

128+
test('count from fetchOne with offset defined', async () => {
129+
for (const result of [
130+
await new QuerybuilderTest()
131+
.fetchOne({
132+
tableName: 'testTable',
133+
fields: '*',
134+
where: {
135+
conditions: [],
136+
params: [],
137+
},
138+
offset: 3,
139+
})
140+
.count(),
141+
]) {
142+
expect((result.results as any).query).toEqual('SELECT count(*) as total FROM testTable LIMIT 1')
143+
expect((result.results as any).arguments).toEqual([])
144+
expect((result.results as any).fetchType).toEqual('ONE')
145+
}
146+
})
147+
148+
test('count from fetchAll with offset defined', async () => {
149+
for (const result of [
150+
await new QuerybuilderTest()
151+
.fetchAll({
152+
tableName: 'testTable',
153+
offset: 4,
154+
})
155+
.count(),
156+
]) {
157+
expect((result.results as any).query).toEqual('SELECT count(*) as total FROM testTable LIMIT 1')
158+
expect((result.results as any).arguments).toEqual(undefined)
159+
expect((result.results as any).fetchType).toEqual('ONE')
160+
}
161+
})
162+
128163
test('select with simplified where list', async () => {
129164
for (const result of [
130165
new QuerybuilderTest().fetchOne({

0 commit comments

Comments
 (0)