TypeORM - Working with Entity Manager



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.

Entity Manager API

We can access EntityManager using getManager() method as specified below −

import { getManager } from "typeorm"; const entityManager = getManager();

Let us learn most important method of the EntityManager in this chapter.

connection

connection method returns database ORM connection to specific databases. The sample code is as follows −

const connection = manager.connection;

QueryRunner

queryRunner method returns custom query runner object and it is used for database operations by entity manager. The sample code is as follows −

const queryRunner = manager.queryRunner;

transaction

If multiple database requests are called, transaction will execute in a single database transaction. The sample code to get the transaction is as follows −

await manager.transaction(async manager => { 
});

query

query method executes sql queries. Simple insert query as shown below −

const qur = await manager.query(`insert into student(name,age) values('stud2',13)`);

insert

insert method is used to insert a new entity or array of entities to the database. The sample code is as follows −

await manager.insert(Student, { 
   Name: "Student3", 
   Age: 14 
});

update

update is used to update the existing records in the database.

await manager.update(User, 1, { Name: "Adam" });

This query works similar to the below SQL query,

UPDATE student SET Name = "Adam" WHERE id = 1

delete

delete method will delete the specified record from the table,

await manager.delete(Student, 1);

This will delete with id 1 of student record.

save

save is used to save the given entity into the database. Simple Student entity can be save as shown below −

import {Student} from "./entity/Student";

createConnection().then(async connection => {   
   console.log("Inserting a new record into the student database..."); 
   const stud = new Student(); stud.Name = "student1"; 
   stud.age = 12; 
   await connection.manager.save(stud); 
}

This will add new student record into the database. save method will insert the student, if the given student does not exist in the database. Otherwise, save will update existing student record in the database.

remove

remove is used to delete the given entity from the database. Simple Student entity can be deleted as shown below −

await manager.remove(stud);

count

count method will return the number of records available in the table and you can use it pagination purposes. The sample code is as follows −

const cnt = await manager.count(Student, { age: 12 });

find

find method is used for searching purposes. It fetches all the record from database as shown below −

console.log("Loading users from the database..."); 
const students = await connection.manager.find(Student); console.log("Loaded users: ", students);

findOne

Similar to find method, but returns the first matched record. The sample code is as follows −

const stud = await manager.findOne(Student, 1);

clear

clear method clears all the data from the table. The sample code is as follows −

await manager.clear(Student);
Advertisements