Skip to content

Commit

Permalink
Add offset and order by to delete queries (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Sep 24, 2024
1 parent e900a48 commit 6f2c41e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 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.4",
"version": "1.6.5",
"description": "Zero dependencies Query Builder for Cloudflare Workers",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
4 changes: 3 additions & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,10 @@ export class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
`DELETE
FROM ${params.tableName}` +
this._where(params.where) +
this._returning(params.returning) +
this._orderBy(params.orderBy) +
this._limit(params.limit) +
this._returning(params.returning)
this._offset(params.offset)
)
}

Expand Down
4 changes: 3 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ 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>
orderBy?: string | Array<string> | Record<string, string | OrderTypes>
limit?: number
offset?: number
}

export type DeleteReturning = Omit<Delete, 'returning'> & {
Expand Down
32 changes: 31 additions & 1 deletion tests/builder/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ describe('Delete Builder', () => {
expect(result.fetchType).toEqual('ALL')
})

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

expect(result.query).toEqual('DELETE FROM testTable WHERE field = false LIMIT 10 OFFSET 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 @@ -104,7 +117,24 @@ describe('Delete Builder', () => {
limit: 10000
})

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

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

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

0 comments on commit 6f2c41e

Please sign in to comment.