From 1a3842db40dfef165f0b21029ca4bbbcbeb8bf7f Mon Sep 17 00:00:00 2001 From: anirudh1713 Date: Sat, 4 Feb 2023 23:10:27 +0530 Subject: [PATCH] feat: add `deleteQueryBuilder.returningAll(table)` overload (#268) --- src/query-builder/delete-query-builder.ts | 8 ++- src/query-builder/returning-interface.ts | 2 + test/node/src/delete.test.ts | 51 +++++++++++++++++++ .../test-d/delete-query-builder.test-d.ts | 19 ++++++- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/query-builder/delete-query-builder.ts b/src/query-builder/delete-query-builder.ts index 510aa460a..284679d11 100644 --- a/src/query-builder/delete-query-builder.ts +++ b/src/query-builder/delete-query-builder.ts @@ -498,12 +498,16 @@ export class DeleteQueryBuilder }) } - returningAll(): DeleteQueryBuilder> { + returningAll(): DeleteQueryBuilder> + + returningAll(table: T): DeleteQueryBuilder + + returningAll(table?: any): any { return new DeleteQueryBuilder({ ...this.#props, queryNode: QueryNode.cloneWithReturning( this.#props.queryNode, - parseSelectAll() + parseSelectAll(table) ), }) } diff --git a/src/query-builder/returning-interface.ts b/src/query-builder/returning-interface.ts index bc508bc11..427a057b1 100644 --- a/src/query-builder/returning-interface.ts +++ b/src/query-builder/returning-interface.ts @@ -75,4 +75,6 @@ export interface ReturningInterface { * that support `returning` such as PostgreSQL. */ returningAll(): ReturningInterface> + + returningAll(table: T): ReturningInterface> } diff --git a/test/node/src/delete.test.ts b/test/node/src/delete.test.ts index c266e224f..3bd43bbad 100644 --- a/test/node/src/delete.test.ts +++ b/test/node/src/delete.test.ts @@ -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') { diff --git a/test/typings/test-d/delete-query-builder.test-d.ts b/test/typings/test-d/delete-query-builder.test-d.ts index 028dcdb9f..edf4b9c6c 100644 --- a/test/typings/test-d/delete-query-builder.test-d.ts +++ b/test/typings/test-d/delete-query-builder.test-d.ts @@ -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) { const r1 = await db.deleteFrom('pet').where('id', '=', '1').executeTakeFirst() @@ -38,4 +38,21 @@ async function testDelete(db: Kysely) { .orWhere('toy.price', '=', 0) .executeTakeFirstOrThrow() expectType(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(r6) + + const r7 = await db + .deleteFrom('pet') + .where('pet.species', '=', 'cat') + .returningAll('pet') + .execute() + expectType(r7) }