Daniel Santos
Daniel Santos

Reputation: 15798

TypeORM RelationCount alternative

I'm working with TypeORM for a while into a project. We have an Entity that holds the followings properties

@RelationCount("sentences")
public sentencesCount?: number;

@OneToMany(() => Sentence, e => e.job)
public sentences?: Sentence[];

However I noticed that RelationCount is depreciated

@RelationCount, deprecated Do not use this decorator, it may be removed in the future versions

I look for a substitution in TypeORM official readme but I found nothing but creating a custom QueryBuilder with a native SQL SELECT COUNT(*) statement.

In this project we use a lot of simple statement like myRepo.find(). I rather not to replace all these simple calls for QueryBuilders.

What should I use instead of @RelationCount in our project?

Should I ignore this "depreciate" warning. ?

Upvotes: 3

Views: 2930

Answers (1)

Rinat Garifullin
Rinat Garifullin

Reputation: 31

You need use loadRelationCountAndMap methods

yourRepository
  .createQueryBuilder("entity")
  .leftJoin('entity.sentences', 'sentences')
  .loadRelationCountAndMap('entity.sentencesCount', 'entity.sentences');

And Delete @RelationCount("sentences") public sentencesCount?: number; from your entity class.

Upvotes: 3

Related Questions