Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.count doesn't always return a Promise<number> #10757

Closed
mjdavidson opened this issue Apr 12, 2019 · 6 comments
Closed

model.count doesn't always return a Promise<number> #10757

mjdavidson opened this issue Apr 12, 2019 · 6 comments
Labels
type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.

Comments

@mjdavidson
Copy link

public static count(options?: CountOptions): Promise<number>;

if you use group/attributes it will return an array of objects

@SimonSchick SimonSchick added the type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. label Apr 12, 2019
@SimonSchick
Copy link
Contributor

Could you add a specific overload that when group is defined it yields { [key: string]: number }?

You'd have to create an interface like interface CountWithOptions extends CountOptions { group:/*note the lack of ?*/ GroupOptions/*or whatever it was called*/ }

@mjdavidson
Copy link
Author

mjdavidson commented Apr 16, 2019

The fix is incorrect. It should be { [key:string]: string }[],
also if you group by multiple columns this should be accounted for,

eg:

const observations = await Observation.count({
  include: [
    {
      model: User,
      association: 'createdBy',
      attributes: ['id', 'name', 'avatar']
    },
  ],
  attributes: ['createdBy.name', 'createdBy.color', 'createdBy.id'],
  group: ['createdBy.name', 'createdBy.color', 'createdBy.id']
});

returns

[ { "name": "Matt Davidson",                                                   
    "color": "#0000",                                                           
    "id": "aa6776b8-d402-4817-8335-d12257d13df4",                                
    "count": "1" } ]      

Note that count is not a number it's actually a string

@mjdavidson
Copy link
Author

@sushantdhiman

@DG-Wangtao
Copy link

Maybe it should be

Promise<{ [key: string]: object }>

@papb
Copy link
Member

papb commented Jul 26, 2019

@mjdavidson Is this still an issue?

@mjdavidson
Copy link
Author

don't think so,
https://github.com/sequelize/sequelize/blob/v5.10.3/types/lib/model.d.ts#L1811
stopped using sequelize with typescript... was 'too hard basket'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants