I don't understand the differences between Entity manager and Repository in typeorm. They seem to do the same thing. If it's the same, why two different API exists. If not what are the differences and when we use them.
Repository is specific to an entity. In other words, each entity will have its own, build-in repository and it can be accessed using getRepository() method of connection object as specified below − const studRepository = manager. getRepository(Student);
Advertisements. EntityManager is similar to Repository and used to manage database operations such as insert, update, delete and load data. While Repository handles single entity, EntityManager is common to all entities and able to do operations on all entities.
An Entity Manager handles all entities, while Repository handles a single entity. This means that when using an Entity Manager you have to specify the Entity you are working with for each method call.
Here is an example of a create method from the Entity Manager and Repository documentation for comparison:
const manager = getManager();
// ...
const user = manager.create(User); // same as const user = new User();
const repository = connection.getRepository(User);
// ...
const user = repository.create(); // same as const user = new User();
Both are valid and you can choose whichever you prefer to work with.
It does exactly the same thing just an alias
either you do
Option 1:
const manager = getManager();
manager.find(Methodology);
manager.find(Infrastructure);
manager.find(Safety);
Option 2:
getRepository(Methodology).find();
getRepository(Infrastructure).find();
getRepository(Safety).find();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With