From 9b35e1399790d7d20dab5c49ae1cd0e1a170f7c8 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 23 Sep 2021 07:34:07 +0530 Subject: [PATCH] improvement: add returning method to all query builder --- adonis-typings/model.ts | 6 ++++-- adonis-typings/querybuilder.ts | 9 +++++---- src/Database/QueryBuilder/Database.ts | 10 +++++----- src/Orm/QueryBuilder/index.ts | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/adonis-typings/model.ts b/adonis-typings/model.ts index 14d7afb4..165f8a5c 100644 --- a/adonis-typings/model.ts +++ b/adonis-typings/model.ts @@ -16,6 +16,7 @@ declare module '@ioc:Adonis/Lucid/Orm' { Counter, OneOrMany, Aggregate, + Returning, DialectContract, ChainableContract, QueryClientContract, @@ -331,6 +332,7 @@ declare module '@ioc:Adonis/Lucid/Orm' { extends ChainableContract, ExcutableQueryBuilderContract { model: Model + returning: Returning /** * Define a callback to transform a row @@ -393,8 +395,8 @@ declare module '@ioc:Adonis/Lucid/Orm' { /** * Perform delete operation */ - del(): ModelQueryBuilderContract - delete(): ModelQueryBuilderContract + del(returning?: OneOrMany): ModelQueryBuilderContract + delete(returning?: OneOrMany): ModelQueryBuilderContract /** * A shorthand to define limit and offset based upon the diff --git a/adonis-typings/querybuilder.ts b/adonis-typings/querybuilder.ts index 3d8b6ecc..934eafd8 100644 --- a/adonis-typings/querybuilder.ts +++ b/adonis-typings/querybuilder.ts @@ -403,12 +403,12 @@ declare module '@ioc:Adonis/Lucid/Database' { * Accepts an array of object of named key/value pair and returns an array * of Generic return columns. */ - (values: Dictionary, returning?: string | string[]): Builder + (values: Dictionary, returning?: OneOrMany): Builder /** * Accepts a key-value pair to update. */ - (column: string, value: any, returning?: string | string[]): Builder + (column: string, value: any, returning?: OneOrMany): Builder } /** @@ -696,6 +696,7 @@ declare module '@ioc:Adonis/Lucid/Database' { extends ChainableContract, ExcutableQueryBuilderContract { client: QueryClientContract + returning: Returning /** * Clone current query @@ -715,8 +716,8 @@ declare module '@ioc:Adonis/Lucid/Database' { /** * Perform delete operation */ - del(): this - delete(): this + del(returning?: OneOrMany): this + delete(returning?: OneOrMany): this /** * A shorthand to define limit and offset based upon the diff --git a/src/Database/QueryBuilder/Database.ts b/src/Database/QueryBuilder/Database.ts index 087769da..48c3be14 100644 --- a/src/Database/QueryBuilder/Database.ts +++ b/src/Database/QueryBuilder/Database.ts @@ -108,17 +108,17 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil /** * Delete rows under the current query */ - public del(): this { + public del(returning?: string | string[]): this { this.ensureCanPerformWrites() - this.knexQuery.del() + returning ? this.knexQuery.del(returning) : this.knexQuery.del() return this } /** * Alias for [[del]] */ - public delete(): this { - return this.del() + public delete(returning?: string | string[]): this { + return this.del(returning) } /** @@ -174,7 +174,7 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil /** * Perform update */ - public update(column: any, value?: any, returning?: string[]): this { + public update(column: any, value?: any, returning?: string | string[]): this { this.ensureCanPerformWrites() if (value === undefined && returning === undefined) { diff --git a/src/Orm/QueryBuilder/index.ts b/src/Orm/QueryBuilder/index.ts index 802a4082..63aa1c10 100644 --- a/src/Orm/QueryBuilder/index.ts +++ b/src/Orm/QueryBuilder/index.ts @@ -355,6 +355,25 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon return clonedQuery } + /** + * Define returning columns + */ + public returning(columns: any): this { + /** + * Do not chain `returning` in sqlite3 to avoid knex warnings + */ + if (this.client && ['sqlite3', 'mysql'].includes(this.client.dialect.name)) { + return this + } + + columns = Array.isArray(columns) + ? columns.map((column) => this.resolveKey(column)) + : this.resolveKey(columns) + + this.knexQuery.returning(columns) + return this + } + /** * Define a query to constraint to be defined when condition is truthy */