Skip to content

Commit

Permalink
improvement: add returning method to all query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 23, 2021
1 parent bf8f83f commit 9b35e13
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
6 changes: 4 additions & 2 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare module '@ioc:Adonis/Lucid/Orm' {
Counter,
OneOrMany,
Aggregate,
Returning,
DialectContract,
ChainableContract,
QueryClientContract,
Expand Down Expand Up @@ -331,6 +332,7 @@ declare module '@ioc:Adonis/Lucid/Orm' {
extends ChainableContract,
ExcutableQueryBuilderContract<Result[]> {
model: Model
returning: Returning<this>

/**
* Define a callback to transform a row
Expand Down Expand Up @@ -393,8 +395,8 @@ declare module '@ioc:Adonis/Lucid/Orm' {
/**
* Perform delete operation
*/
del(): ModelQueryBuilderContract<Model, any>
delete(): ModelQueryBuilderContract<Model, any>
del(returning?: OneOrMany<string>): ModelQueryBuilderContract<Model, any>
delete(returning?: OneOrMany<string>): ModelQueryBuilderContract<Model, any>

/**
* A shorthand to define limit and offset based upon the
Expand Down
9 changes: 5 additions & 4 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, string>, returning?: string | string[]): Builder
(values: Dictionary<any, string>, returning?: OneOrMany<string>): Builder

/**
* Accepts a key-value pair to update.
*/
(column: string, value: any, returning?: string | string[]): Builder
(column: string, value: any, returning?: OneOrMany<string>): Builder
}

/**
Expand Down Expand Up @@ -696,6 +696,7 @@ declare module '@ioc:Adonis/Lucid/Database' {
extends ChainableContract,
ExcutableQueryBuilderContract<Result[]> {
client: QueryClientContract
returning: Returning<this>

/**
* Clone current query
Expand All @@ -715,8 +716,8 @@ declare module '@ioc:Adonis/Lucid/Database' {
/**
* Perform delete operation
*/
del(): this
delete(): this
del(returning?: OneOrMany<string>): this
delete(returning?: OneOrMany<string>): this

/**
* A shorthand to define limit and offset based upon the
Expand Down
10 changes: 5 additions & 5 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 9b35e13

Please sign in to comment.