question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems working with repository.count

See original GitHub issue

Issue type:

[x] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [x] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

@Entity()
export abstract class EntityAbstract {
    @ObjectIdColumn()
    _id: ObjectID;

	@Column()
	name: string;

    @CreateDateColumn({type: 'timestamp', update: true})
    createdAt: Date;

    @UpdateDateColumn({type: 'timestamp'})
    updatedAt: Date;

    @Column({type: 'timestamp'})
    deletedAt: Date;
}
console.log(await this.repository.find({where: {deletedAt: {$exists: true}}})); // ok, return 2 record
console.log(await this.repository.count({where: {deletedAt: {$exists: true}}})); // opps, return 0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
danmanacommented, Aug 12, 2020

I ran also into this issue when Using Repository.

The problem is the typings.

The more generic Repository class has three signatures for count:

count(options?: FindManyOptions<Entity>): Promise<number>;
count(conditions?: FindConditions<Entity>): Promise<number>;
count(optionsOrConditions?: FindManyOptions<Entity>|FindConditions<Entity>): Promise<number>;

But MongoRepository implementation only has

count(query?: ObjectLiteral, options?: MongoCountPreferences): Promise<number>;

This is equivalent to the second signature, the one with FindConditions

So in case you are using Mongo you can only use the second signature from Repository

// good signature
repository.count({ myField: "myValue"});

// bad signature, even if the typing says it's valid !
repository.count({ where: { myField: "myValue"}});

You have two options:

  • you take care and always use just the second signature
  • or you switch the type of your repository to MongoRepository and get the correct typings
2reactions
playahatercommented, Sep 2, 2019

Perhaps not a best way to address the issue, but let me do it here for start and we can move it in a separate issue if needed.

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.18 (or put your version here)

Steps to reproduce or a small repository showing the problem:

I can reproduce the same issue with mysql driver. Seem like that repository.count is not returning correct count results in my case as well.

have a simple condition in it: repository.count({ ColumnA: valueA, ColumnB: ValueB });

Read more comments on GitHub >

github_iconTop Results From Across the Web

GitHub API: how efficiently get the total count of Issues per ...
Using Rest API, you can use the search api: GET https://api.github.com/search/issues?q=repo:bertrandmartel/tableau-scraping%20is:issue.
Read more >
Native Queries with Spring Data JPA
Add a Count Query to Enable Pagination. When working with a custom JPQL query, you can add a parameter of type Pageable to...
Read more >
Q & A
Working with Repositories - need for a blank (0.0.0.0/0) repository? Recently we have had issues where static IP asset groups would not display...
Read more >
Customizing the Result of JPA Queries with Aggregation ...
The result of the previous JPA query can't be loaded into an instance of Comment because the result is a different shape. The...
Read more >
Wrong repository count on profile : r/github
Just realized how to fix it. It seems it doesn't count the repositories for which you change the visibility until you create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found