Skip to content

Commit

Permalink
Add limit to delete queries (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Sep 24, 2024
1 parent 364d18c commit e900a48
Show file tree
Hide file tree
Showing 5 changed files with 33 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.3",
"version": "1.6.4",
"description": "Zero dependencies Query Builder for Cloudflare Workers",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
1 change: 1 addition & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
`DELETE
FROM ${params.tableName}` +
this._where(params.where) +
this._limit(params.limit) +
this._returning(params.returning)
)
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export type UpdateWithoutReturning = Omit<Update, 'returning'>
export type Delete = {
tableName: string
where: Where // This field is optional, but is kept required in type to warn users of delete without where
limit?: number
returning?: string | Array<string>
}

Expand Down
28 changes: 28 additions & 0 deletions tests/builder/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ describe('Delete Builder', () => {
expect(result.fetchType).toEqual('ALL')
})

test('delete with limit', async () => {
const result = new QuerybuilderTest().delete({
tableName: 'testTable',
where: 'field = false',
limit: 10,
})

expect(result.query).toEqual('DELETE FROM testTable WHERE field = false LIMIT 10')
expect(result.arguments).toEqual(undefined)
expect(result.fetchType).toEqual('ALL')
})

test('delete with simplified where list', async () => {
const result = new QuerybuilderTest().delete({
tableName: 'testTable',
Expand Down Expand Up @@ -80,4 +92,20 @@ describe('Delete Builder', () => {
expect(result.arguments).toEqual(['test', 123])
expect(result.fetchType).toEqual('ALL')
})

test('delete with multiple where with multiple returning and limit', async () => {
const result = new QuerybuilderTest().delete({
tableName: 'testTable',
where: {
conditions: ['field = ?1', 'id = ?2'],
params: ['test', 123],
},
returning: ['id', 'field'],
limit: 10000
})

expect(result.query).toEqual('DELETE FROM testTable WHERE field = ?1 AND id = ?2 LIMIT 10000 RETURNING id, field')
expect(result.arguments).toEqual(['test', 123])
expect(result.fetchType).toEqual('ALL')
})
})

0 comments on commit e900a48

Please sign in to comment.