Skip to content

Commit

Permalink
refactor: moving aggregate functions to chainable query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 6, 2019
1 parent dbd179f commit ebf6807
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 104 deletions.
22 changes: 11 additions & 11 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,17 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {

skipLocked (): this
noWait (): this

/**
* Aggregates
*/
count: Aggregate<this>
countDistinct: Aggregate<this>
min: Aggregate<this>
max: Aggregate<this>
sum: Aggregate<this>
avg: Aggregate<this>
avgDistinct: Aggregate<this>
}

/**
Expand Down Expand Up @@ -605,17 +616,6 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
*/
first (): Promise<Result | null>

/**
* Aggregates
*/
count: Aggregate<this>
countDistinct: Aggregate<this>
min: Aggregate<this>
max: Aggregate<this>
sum: Aggregate<this>
avg: Aggregate<this>
avgDistinct: Aggregate<this>

/**
* Mutations
*/
Expand Down
93 changes: 93 additions & 0 deletions src/Database/QueryBuilder/Chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ export abstract class Chainable implements ChainableContract {
return [this.$transformValue(lhs), this.$transformValue(rhs)]
}

/**
* Normalizes the columns aggregates functions to something
* knex can process.
*/
private _normalizeAggregateColumns (columns: any, alias?: any): any {
if (columns.constructor === Object) {
return Object.keys(columns).reduce((result, key) => {
result[key] = this.$transformValue(columns[key])
return result
}, {})
}

if (!alias) {
return alias
}

return { [alias]: this.$transformValue(columns) }
}

/**
* Transforms the value to something that knex can internally understand and
* handle. It includes.
Expand Down Expand Up @@ -1020,4 +1039,78 @@ export abstract class Chainable implements ChainableContract {
this.$knexBuilder.as(alias)
return this
}

/**
* Count rows for the current query
*/
public count (columns: any, alias?: any): this {
this.$knexBuilder.count(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Count distinct rows for the current query
*/
public countDistinct (columns: any, alias?: any): this {
this.$knexBuilder.countDistinct(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `min` aggregate function
*/
public min (columns: any, alias?: any): this {
this.$knexBuilder.min(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `max` aggregate function
*/
public max (columns: any, alias?: any): this {
this.$knexBuilder.max(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `avg` aggregate function
*/
public avg (columns: any, alias?: any): this {
this.$knexBuilder.avg(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of distinct `avg` aggregate function
*/
public avgDistinct (columns: any, alias?: any): this {
this.$knexBuilder.avgDistinct(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `sum` aggregate function
*/
public sum (columns: any, alias?: any): this {
this.$knexBuilder.sum(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Perform update by incrementing value for a given column. Increments
* can be clubbed with `update` as well
*/
public increment (column: any, counter?: any): this {
this.$knexBuilder.increment(column, counter)
return this
}

/**
* Perform update by decrementing value for a given column. Decrements
* can be clubbed with `update` as well
*/
public decrement (column: any, counter?: any): this {
this.$knexBuilder.decrement(column, counter)
return this
}
}
93 changes: 0 additions & 93 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,99 +58,6 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
}
}

/**
* Normalizes the columns aggregates functions to something
* knex can process.
*/
private _normalizeAggregateColumns (columns: any, alias?: any): any {
if (columns.constructor === Object) {
return Object.keys(columns).reduce((result, key) => {
result[key] = this.$transformValue(columns[key])
return result
}, {})
}

if (!alias) {
return alias
}

return { [alias]: this.$transformValue(columns) }
}

/**
* Count rows for the current query
*/
public count (columns: any, alias?: any): this {
this.$knexBuilder.count(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Count distinct rows for the current query
*/
public countDistinct (columns: any, alias?: any): this {
this.$knexBuilder.countDistinct(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `min` aggregate function
*/
public min (columns: any, alias?: any): this {
this.$knexBuilder.min(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `max` aggregate function
*/
public max (columns: any, alias?: any): this {
this.$knexBuilder.max(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `avg` aggregate function
*/
public avg (columns: any, alias?: any): this {
this.$knexBuilder.avg(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of distinct `avg` aggregate function
*/
public avgDistinct (columns: any, alias?: any): this {
this.$knexBuilder.avgDistinct(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Make use of `sum` aggregate function
*/
public sum (columns: any, alias?: any): this {
this.$knexBuilder.sum(this._normalizeAggregateColumns(columns, alias))
return this
}

/**
* Perform update by incrementing value for a given column. Increments
* can be clubbed with `update` as well
*/
public increment (column: any, counter?: any): this {
this.$knexBuilder.increment(column, counter)
return this
}

/**
* Perform update by decrementing value for a given column. Decrements
* can be clubbed with `update` as well
*/
public decrement (column: any, counter?: any): this {
this.$knexBuilder.decrement(column, counter)
return this
}

/**
* Delete rows under the current query
*/
Expand Down

0 comments on commit ebf6807

Please sign in to comment.