Skip to content

Commit 6f2c41e

Browse files
authored
Add offset and order by to delete queries (#62)
1 parent e900a48 commit 6f2c41e

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
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.4",
3+
"version": "1.6.5",
44
"description": "Zero dependencies Query Builder for Cloudflare Workers",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/builder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,10 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
382382
`DELETE
383383
FROM ${params.tableName}` +
384384
this._where(params.where) +
385+
this._returning(params.returning) +
386+
this._orderBy(params.orderBy) +
385387
this._limit(params.limit) +
386-
this._returning(params.returning)
388+
this._offset(params.offset)
387389
)
388390
}
389391

src/interfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ export type UpdateWithoutReturning = Omit<Update, 'returning'>
105105
export type Delete = {
106106
tableName: string
107107
where: Where // This field is optional, but is kept required in type to warn users of delete without where
108-
limit?: number
109108
returning?: string | Array<string>
109+
orderBy?: string | Array<string> | Record<string, string | OrderTypes>
110+
limit?: number
111+
offset?: number
110112
}
111113

112114
export type DeleteReturning = Omit<Delete, 'returning'> & {

tests/builder/delete.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ describe('Delete Builder', () => {
3838
expect(result.fetchType).toEqual('ALL')
3939
})
4040

41+
test('delete with limit and offset', async () => {
42+
const result = new QuerybuilderTest().delete({
43+
tableName: 'testTable',
44+
where: 'field = false',
45+
limit: 10,
46+
offset: 10,
47+
})
48+
49+
expect(result.query).toEqual('DELETE FROM testTable WHERE field = false LIMIT 10 OFFSET 10')
50+
expect(result.arguments).toEqual(undefined)
51+
expect(result.fetchType).toEqual('ALL')
52+
})
53+
4154
test('delete with simplified where list', async () => {
4255
const result = new QuerybuilderTest().delete({
4356
tableName: 'testTable',
@@ -104,7 +117,24 @@ describe('Delete Builder', () => {
104117
limit: 10000
105118
})
106119

107-
expect(result.query).toEqual('DELETE FROM testTable WHERE field = ?1 AND id = ?2 LIMIT 10000 RETURNING id, field')
120+
expect(result.query).toEqual('DELETE FROM testTable WHERE field = ?1 AND id = ?2 RETURNING id, field LIMIT 10000')
121+
expect(result.arguments).toEqual(['test', 123])
122+
expect(result.fetchType).toEqual('ALL')
123+
})
124+
125+
test('delete with multiple where with multiple returning and limit and order', async () => {
126+
const result = new QuerybuilderTest().delete({
127+
tableName: 'testTable',
128+
where: {
129+
conditions: ['field = ?1', 'id = ?2'],
130+
params: ['test', 123],
131+
},
132+
returning: ['id', 'field'],
133+
orderBy: "id",
134+
limit: 10000
135+
})
136+
137+
expect(result.query).toEqual('DELETE FROM testTable WHERE field = ?1 AND id = ?2 RETURNING id, field ORDER BY id LIMIT 10000')
108138
expect(result.arguments).toEqual(['test', 123])
109139
expect(result.fetchType).toEqual('ALL')
110140
})

0 commit comments

Comments
 (0)