src/Database/MonkeyPatch.js
function generateAggregate (aggregateOp, defaultColumnName = undefined) {
  let funcName = `get${_.upperFirst(aggregateOp)}`

  /**
   * Do not re-add the method if exists
   */
  if (KnexQueryBuilder.prototype[funcName]) {
    return
  }

  KnexQueryBuilder.prototype[funcName] = async function (columnName = defaultColumnName) {
    if (!columnName) {
      throw new Error(`'${funcName}' requires a column name.`)
    }

    const wrapper = new this.constructor(this.client)
    const query = wrapper.from(this.as('__lucid'))

    /**
     * Copy events from the original query
     */
    query._events = this._events

    /**
     * Executing query chain
     */
    const results = await query[aggregateOp](`${columnName} as __lucid_aggregate`)

    return results[0].__lucid_aggregate
  }
}
function generateAggregate (aggregateOp, defaultColumnName = undefined) {
  let funcName = `get${_.upperFirst(aggregateOp)}`

  /**
   * Do not re-add the method if exists
   */
  if (KnexQueryBuilder.prototype[funcName]) {
    return
  }

  KnexQueryBuilder.prototype[funcName] = async function (columnName = defaultColumnName) {
    if (!columnName) {
      throw new Error(`'${funcName}' requires a column name.`)
    }

    const wrapper = new this.constructor(this.client)
    const query = wrapper.from(this.as('__lucid'))

    /**
     * Copy events from the original query
     */
    query._events = this._events

    /**
     * Executing query chain
     */
    const results = await query[aggregateOp](`${columnName} as __lucid_aggregate`)

    return results[0].__lucid_aggregate
  }
}
function generateAggregate ( aggregateOp , defaultColumnName = undefined ) { let funcName = ` ${ _ . upperFirst ( aggregateOp ) } ` /**
   * Do not re-add the method if exists
   */ if ( KnexQueryBuilder . prototype [ funcName ] ) { return } KnexQueryBuilder . prototype [ funcName ] = async function ( columnName = defaultColumnName ) { if ( ! columnName ) { throw new Error ( ` ${ funcName } ` ) } const wrapper = new this . constructor ( this . client ) const query = wrapper . from ( this . as ( '__lucid' ) ) /**
     * Copy events from the original query
     */ query . _events = this . _events /**
     * Executing query chain
     */ const results = await query [ aggregateOp ] ( ` ${ columnName } ` ) return results [ 0 ] . __lucid_aggregate } }
Generates an aggregate function, that returns the aggregated result
as a number.

@method generateAggregate

@param  {String}          aggregateOp
@param  {String}          defaultColumnName

@return {Number}

@example
```js
generateAggregate('count')
```
Generates an aggregate function that returns the aggregated result as a number .