Search code examples
node.jstypeorm

Differences between entity manager and repository typeorm


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.


Solution

  • 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.