Skip to content

Commit e900a48

Browse files
authored
Add limit to delete queries (#60)
1 parent 364d18c commit e900a48

File tree

5 files changed

+33
-3
lines changed

5 files changed

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

src/builder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
382382
`DELETE
383383
FROM ${params.tableName}` +
384384
this._where(params.where) +
385+
this._limit(params.limit) +
385386
this._returning(params.returning)
386387
)
387388
}

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ 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
108109
returning?: string | Array<string>
109110
}
110111

tests/builder/delete.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ describe('Delete Builder', () => {
2626
expect(result.fetchType).toEqual('ALL')
2727
})
2828

29+
test('delete with limit', async () => {
30+
const result = new QuerybuilderTest().delete({
31+
tableName: 'testTable',
32+
where: 'field = false',
33+
limit: 10,
34+
})
35+
36+
expect(result.query).toEqual('DELETE FROM testTable WHERE field = false LIMIT 10')
37+
expect(result.arguments).toEqual(undefined)
38+
expect(result.fetchType).toEqual('ALL')
39+
})
40+
2941
test('delete with simplified where list', async () => {
3042
const result = new QuerybuilderTest().delete({
3143
tableName: 'testTable',
@@ -80,4 +92,20 @@ describe('Delete Builder', () => {
8092
expect(result.arguments).toEqual(['test', 123])
8193
expect(result.fetchType).toEqual('ALL')
8294
})
95+
96+
test('delete with multiple where with multiple returning and limit', async () => {
97+
const result = new QuerybuilderTest().delete({
98+
tableName: 'testTable',
99+
where: {
100+
conditions: ['field = ?1', 'id = ?2'],
101+
params: ['test', 123],
102+
},
103+
returning: ['id', 'field'],
104+
limit: 10000
105+
})
106+
107+
expect(result.query).toEqual('DELETE FROM testTable WHERE field = ?1 AND id = ?2 LIMIT 10000 RETURNING id, field')
108+
expect(result.arguments).toEqual(['test', 123])
109+
expect(result.fetchType).toEqual('ALL')
110+
})
83111
})

0 commit comments

Comments
 (0)