Skip to content

Commit

Permalink
feat: add deleteQueryBuilder.returningAll(table) overload (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudh1713 committed Feb 4, 2023
1 parent 3cd81c3 commit 1a3842d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/query-builder/delete-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,16 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
})
}

returningAll(): DeleteQueryBuilder<DB, TB, Selectable<DB[TB]>> {
returningAll(): DeleteQueryBuilder<DB, TB, Selectable<DB[TB]>>

returningAll<T extends TB>(table: T): DeleteQueryBuilder<DB, TB, DB[T]>

returningAll(table?: any): any {
return new DeleteQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithReturning(
this.#props.queryNode,
parseSelectAll()
parseSelectAll(table)
),
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/query-builder/returning-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ export interface ReturningInterface<DB, TB extends keyof DB, O> {
* that support `returning` such as PostgreSQL.
*/
returningAll(): ReturningInterface<DB, TB, Selectable<DB[TB]>>

returningAll<T extends TB>(table: T): ReturningInterface<DB, TB, Selectable<DB[T]>>
}
51 changes: 51 additions & 0 deletions test/node/src/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,57 @@ for (const dialect of BUILT_IN_DIALECTS) {

await query.execute()
})

it('should delete from t1 using t2, t3 returning all t2 columns', async () => {
const query = ctx.db
.deleteFrom('toy')
.using(['pet', 'person'])
.whereRef('toy.pet_id', '=', 'pet.id')
.whereRef('pet.owner_id', '=', 'person.id')
.where('person.first_name', '=', 'Bob')
.returningAll('pet')

testSql(query, dialect, {
postgres: {
sql: [
'delete from "toy"',
'using "pet", "person"',
'where "toy"."pet_id" = "pet"."id"',
'and "pet"."owner_id" = "person"."id"',
'and "person"."first_name" = $1',
'returning "pet".*',
],
parameters: ['Bob'],
},
mysql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await query.execute()
})

it('should return all columns when `returningAll` is used', async () => {
const query = ctx.db
.deleteFrom('person')
.where('gender', '=', 'male')
.returningAll('person')

testSql(query, dialect, {
postgres: {
sql: [
'delete from "person"',
'where "gender" = $1',
'returning "person".*'
],
parameters: ['male'],
},
mysql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await query.execute()
})

}

if (dialect === 'mysql') {
Expand Down
19 changes: 18 additions & 1 deletion test/typings/test-d/delete-query-builder.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectType } from 'tsd'
import { Kysely, DeleteResult } from '..'
import { Database } from '../shared'
import { Database, Person, Pet } from '../shared'

async function testDelete(db: Kysely<Database>) {
const r1 = await db.deleteFrom('pet').where('id', '=', '1').executeTakeFirst()
Expand Down Expand Up @@ -38,4 +38,21 @@ async function testDelete(db: Kysely<Database>) {
.orWhere('toy.price', '=', 0)
.executeTakeFirstOrThrow()
expectType<DeleteResult>(r5)

const r6 = await db
.deleteFrom('person')
.using(['person', 'pet'])
.leftJoin('toy', 'toy.pet_id', 'pet.id')
.where('pet.species', '=', 'cat')
.orWhere('toy.price', '=', 0)
.returningAll('person')
.execute()
expectType<Person[]>(r6)

const r7 = await db
.deleteFrom('pet')
.where('pet.species', '=', 'cat')
.returningAll('pet')
.execute()
expectType<Pet[]>(r7)
}

0 comments on commit 1a3842d

Please sign in to comment.