Skip to content

Commit

Permalink
feat(typings): model.count with group by (#10763)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkojotic authored and sushantdhiman committed Apr 15, 2019
1 parent 7a6c60d commit 638b13b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions types/lib/model.d.ts
Expand Up @@ -527,6 +527,18 @@ export interface CountOptions extends Logging, Transactionable, Filterable, Proj
col?: string;
}

/**
* Options for Model.count when GROUP BY is used
*/
export interface CountWithOptions extends CountOptions {
/**
* GROUP BY in sql
* Used in conjunction with `attributes`.
* @see Projectable
*/
group: GroupOption;
}

export interface FindAndCountOptions extends CountOptions, FindOptions {}

/**
Expand Down Expand Up @@ -1743,6 +1755,11 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
options?: AggregateOptions<T>
): Promise<T>;

/**
* Count number of records if group by is used
*/
public static count(options: CountWithOptions): Promise<{ [key: string]: number }>;

/**
* Count the number of records matching the provided where clause.
*
Expand Down
7 changes: 7 additions & 0 deletions types/test/count.ts
@@ -0,0 +1,7 @@
import { Model, Promise } from 'sequelize';

class MyModel extends Model {}

const grouped: Promise<{ [key: string]: number }> = MyModel.count({ group: 'tag' });
const counted: Promise<number> = MyModel.count();
const countedDistinct: Promise<number> = MyModel.count({ col: 'tag', distinct: true });

0 comments on commit 638b13b

Please sign in to comment.