Skip to content

Commit

Permalink
feat: add support for aggregates in query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent bc5da41 commit 034ea08
Show file tree
Hide file tree
Showing 5 changed files with 1,205 additions and 94 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
DB=pg
DB_NAME=lucid

MYSQL_HOST=mysql
MYSQL_HOST=0.0.0.0
MYSQL_PORT=3306
MYSQL_USER=virk
MYSQL_PASSWORD=password

PG_HOST=pg
PG_HOST=0.0.0.0
PG_PORT=5432
PG_USER=virk
PG_PASSWORD=password
66 changes: 65 additions & 1 deletion adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ declare module '@ioc:Adonis/Addons/DatabaseQueryBuilder' {
import { Dictionary } from 'ts-essentials'
import { JoinCallback, Sql, Raw } from 'knex'

export interface Registery {
Count: number,
}

type OneOrMany<T extends any> = T | T[]

/**
* A known set of values allowed when defining values for different
* clauses
Expand Down Expand Up @@ -359,6 +365,56 @@ declare module '@ioc:Adonis/Addons/DatabaseQueryBuilder' {
interface GroupByRaw<Builder extends ChainableContract> extends RawQueryBuilderContract<Builder> {
}

/**
* Possible signatures for aggregate functions. It will append
* to the pre existing result
*/
interface Aggregate <
Record extends Dictionary<StrictValues, string>,
Result extends any = Record,
> {
/**
* Accepting a typed column with the alias for the count. Unlike knex
* we enforce the alias, otherwise the output highly varies based
* upon the driver in use
*/
<K extends keyof Record, Alias extends string>(
column: OneOrMany<K>,
alias: Alias,
): DatabaseQueryBuilderContract<Record, (Result & Dictionary<Registery['Count'], Alias>)>

/**
* Accepting an object for multiple counts in a single query. Again
* aliases are enforced for consistency.
*/
<
K extends keyof Record,
Alias extends string,
Columns extends Dictionary<OneOrMany<K>, Alias>,
>(
columns: Columns,
): DatabaseQueryBuilderContract<Record, (Result & { [AliasKey in keyof Columns]: Registery['Count'] })>

/**
* Accepting an un typed column with the alias for the count.
*/
<Alias extends string>(
column: OneOrMany<ValueWithSubQueries<string>>,
alias: Alias,
): DatabaseQueryBuilderContract<Record, (Result & Dictionary<Registery['Count'], Alias>)>

/**
* Accepting an object for multiple counts in a single query. Again
* aliases are enforced for consistency
*/
<
Alias extends string,
Columns extends Dictionary<OneOrMany<ValueWithSubQueries<string>>, Alias>,
>(
columns: Columns,
): DatabaseQueryBuilderContract<Record, (Result & { [AliasKey in keyof Columns]: Registery['Count'] })>
}

/**
* Possible signatures for orderBy method.
*/
Expand Down Expand Up @@ -720,9 +776,17 @@ declare module '@ioc:Adonis/Addons/DatabaseQueryBuilder' {
export interface DatabaseQueryBuilderContract <
Record extends Dictionary<StrictValues, string> = Dictionary<StrictValues, string>,
Result extends any = Record,
> extends ChainableContract<Record> {
> extends ChainableContract<Record>, Promise<Result[]> {
select: DatabaseQueryBuilderSelect<Record, Result>,

count: Aggregate<Record, Result>,
countDistinct: Aggregate<Record, Result>,
min: Aggregate<Record, Result>,
max: Aggregate<Record, Result>,
sum: Aggregate<Record, Result>,
avg: Aggregate<Record, Result>,
avgDistinct: Aggregate<Record, Result>,

/**
* Returns the first row
*/
Expand Down
Loading

0 comments on commit 034ea08

Please sign in to comment.