Merge pull request #28276 from spacejack/knex-aggregate-params

Add overloads to aggregate function signatures
This commit is contained in:
Armando Aguirre
2018-08-28 13:13:30 -07:00
committed by GitHub
2 changed files with 65 additions and 8 deletions

21
types/knex/index.d.ts vendored
View File

@@ -138,14 +138,19 @@ declare namespace Knex {
limit(limit: number): QueryBuilder;
// Aggregation
count(columnName?: string): QueryBuilder;
countDistinct(columnName?: string): QueryBuilder;
min(columnName: string): QueryBuilder;
max(columnName: string): QueryBuilder;
sum(columnName: string): QueryBuilder;
sumDistinct(columnName: string): QueryBuilder;
avg(columnName: string): QueryBuilder;
avgDistinct(columnName: string): QueryBuilder;
count(...columnNames: string[]): QueryBuilder;
count(columnName: Record<string, string | string[] | Knex.Raw> | Knex.Raw): QueryBuilder;
countDistinct(columnName: string | Record<string, string | Knex.Raw> | Knex.Raw): QueryBuilder;
min(columnName: string, ...columnNames: string[]): QueryBuilder;
min(columnName: Record<string, string | string[] | Knex.Raw> | Knex.Raw): QueryBuilder;
max(columnName: string, ...columnNames: string[]): QueryBuilder;
max(columnName: Record<string, string | string[] | Knex.Raw> | Knex.Raw): QueryBuilder;
sum(columnName: string, ...columnNames: string[]): QueryBuilder;
sum(columnName: Record<string, string | string[] | Knex.Raw> | Knex.Raw): QueryBuilder;
sumDistinct(columnName: string | Record<string, string | Knex.Raw> | Knex.Raw): QueryBuilder;
avg(columnName: string, ...columnNames: string[]): QueryBuilder;
avg(columnName: Record<string, string | string[] | Knex.Raw> | Knex.Raw): QueryBuilder;
avgDistinct(columnName: string | Record<string, string | Knex.Raw> | Knex.Raw): QueryBuilder;
increment(columnName: string, amount?: number): QueryBuilder;
decrement(columnName: string, amount?: number): QueryBuilder;

View File

@@ -251,6 +251,58 @@ knex('users').whereRaw('id = ?', [1]);
knex('users').whereRaw('id = :id', { id: 1 });
knex('users').whereRaw('id = :id', { id: knex('users').select('id').limit(1) });
// Aggregate functions can use string/object parameters
knex('users').count();
knex('users').count('*');
knex('users').count('id', 'votes');
knex('users').count({count: '*'});
knex('users').count({count: ['id', 'votes']});
knex('users').count({count: knex.raw('*')});
knex('users').count(knex.raw('id'));
knex('users').countDistinct('votes');
knex('users').countDistinct(knex.raw('votes'));
knex('users').countDistinct({votes: 'votes'});
knex('users').countDistinct({votes: knex.raw('votes')});
knex('users').avg('id');
knex('users').avg('id', 'votes');
knex('users').avg({avg: 'id'});
knex('users').avg({avg: ['id', 'votes']});
knex('users').avg({ab: knex.raw('a + b')});
knex('users').avg(knex.raw('votes'));
knex('users').avgDistinct('votes');
knex('users').avgDistinct(knex.raw('votes'));
knex('users').avgDistinct({votes: 'votes'});
knex('users').avgDistinct({votes: knex.raw('votes')});
knex('users').max('id');
knex('users').max('id', 'votes');
knex('users').max({max: 'id'});
knex('users').max({max: ['id', 'votes']});
knex('users').max({ab: knex.raw('a + b')});
knex('users').max(knex.raw('votes'));
knex('users').min('id');
knex('users').min('id', 'votes');
knex('users').min({min: 'id'});
knex('users').min({min: ['id', 'votes']});
knex('users').min({ab: knex.raw('a + b')});
knex('users').min(knex.raw('votes'));
knex('users').sum('id');
knex('users').sum('id', 'votes');
knex('users').sum({sum: 'id'});
knex('users').sum({sum: ['id', 'votes']});
knex('users').sum({ab: knex.raw('a + b')});
knex('users').sum(knex.raw('votes'));
knex('users').sumDistinct('votes');
knex('users').sumDistinct(knex.raw('votes'));
knex('users').sumDistinct({votes: 'votes'});
knex('users').sumDistinct({votes: knex.raw('votes')});
// Join methods
knex('users')
.join('contacts', 'users.id', '=', 'contacts.user_id')