Home > Enterprise >  Typeorm: what is the difference between entity.find() and repository.find()?
Typeorm: what is the difference between entity.find() and repository.find()?

Time:10-11

In the doc (https://orkhan.gitbook.io/typeorm/docs/find-options), it says:

All repository and manager .find* methods accept special options you can use to query data you need without using QueryBuilder:

select - indicates which properties of the main object must be selected

userRepository.find({ select: { firstName: true, lastName: true, }, })

will execute following query:

SELECT "firstName", "lastName" FROM "user"

Now, what is the difference between userRepository.find() and using the entity User.find()? :

userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
})

vs

User.find({
    select: {
        firstName: true,
        lastName: true,
    },
})

?

CodePudding user response:

There is actually no difference. You have confused the named repository with being something different than the Entity. The Entity is actually the repository of a resource i.e the interface that interacts with the Database and gives you methods to perform operations on that model of the resource. I believe it is just a difference between the naming convention used by your team and the documentation team.

  • Related