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.

Relationship between `UserRepository` and `TypeormRepositoryBase`

See original GitHub issue

While working through your database implementation, I was wondering why UserRepository does not use TypeormRepositoryBase.findOne?

https://github.com/Sairyss/domain-driven-hexagon/blob/0b55c3c290f197c920cb563add62e04f83faae96/src/modules/user/database/user.repository.ts#L35-L43

Shouldn’t we do this instead:

const emailVO = new Email(email);
const user = await this.findOne({ email: emailVO });
return user;

And a second question: It seems like UserRepository.prepareQuery removes all query parameters except for id? Why?

https://github.com/Sairyss/domain-driven-hexagon/blob/0b55c3c290f197c920cb563add62e04f83faae96/src/modules/user/database/user.repository.ts#L61-L70

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Sairysscommented, Jun 10, 2021

That’s right, you have to adjust either a type to accept only the parameters specified in prepareQuery, or modify Repository to your liking. I’ll think how to improve it in the future so it’s less confusing.

0reactions
estrehlecommented, Jun 10, 2021

Ah, I see! Now I understand why one would want a more flexible way of preparing queries.

My worry, however, is that this implementation suggests a generality that is not there. The repository exposes a method

findOne(params: QueryParams<EntityProps>)

where

type QueryParams<EntityProps> = DeepPartial<BaseEntityProps & EntityProps>

To anyone who has not studied prepareQuery in detail, this looks like a method that allows one to search the repository using arbitrary combinations of entity properties.

But when I call, say, UserRepository.findOne({ country: 'Germany' }) then the country param will be silently deleted and the result will be the same as if I had called UserRepository.findOne({ }). That could lead to nasty bugs.

My “solution” was to remove the find... methods (except for findById, which works the same for every entity) from TypeormRepositoryBase and resort to custom implementations in each repository, in the spirit of UserRepository.findOneByCountry(country: string).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom repositories - typeorm - GitBook
You can create a custom repository which should contain methods to work with your database. For example, let's say we want to have...
Read more >
Typeorm: what is the difference between entity.find() and ...
The Entity is actually the repository of a resource i.e the interface that interacts with the Database and gives you methods to perform ......
Read more >
TypeORM - Working with Repository - Tutorialspoint
Repository is specific to an entity. In other words, each entity will have its own, build-in repository and it can be accessed using...
Read more >
How can I extend Repository in a generic way? #2097 - GitHub
I want to exend base repository class to have a method named saveAndReturn() which first calls a save() and the returns findOneById() to...
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
You work with entities everywhere in TypeORM. You can load/insert/update/remove and perform other operations with them.
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