TypeORM - Working with Repository



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);

Once the student repository object is created, it can be used to do all database operation of student object.

Repository types

Repository is classified into four categories. They are as follows −

Repository

Default repository of an entity and it can be accessed using getRepository() method as specified below −

const studRepository = manager.getRepository(Student);

Now, studRepository can be used to query student table

TreeRepository

Used for tree like structure entities and it can be accessed using getTreeRepository() method as specified below −

const studcaRepository = manager.getTreeRepository(Student);

MongoRepository

Used inside mongoDB operation entities and it can be accessed using getMongoRepository() method as specified below −

const detailsRepository = manager.getMongoRepository(Details);

CustomRepository

Used to customize the repository and it can be accessed using getCustomRepository() method as specified below,

const myUserRepository = manager.getCustomRepository(UserRepository);

Repository API

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

manager

We can access EntityManager using manager method as specified below −

const manager = repository.manager;

queryRunner

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

const queryRunner = repository.queryRunner;

metadata

metadata returns repository’s metadata. The sample code is as follows −

const metadata = repository.metadata;

query

query method executes SQL queries. Simple select query as shown below −

const qur = await repository.query(`select * from students`);

insert

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

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

The above query is equivalent to,

insert into student(Name,age) values("Student3",14)

update

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

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

This query works similar to the one mentioned below −

update student SET Name = "Adam" where id = 1

delete

delete method will delete the specified record from the table,

await repository.delete(Student, 1);

This will delete student with id 1 from the student table. It is equivalent to,

delete from student where id=1;

If you want to delete by name then use the below query,

await repository.delete({ Name: "Student1" });

This query will delete all the student having name, Student1

** softDelete and restore **

It is used to soft delete the data and you can restore the record based on the id of the student. The sample code is as follows −

await repository.softDelete(1);

You can restore the student record using below command −

await repository.restore(1);

An alternative option to delete and restore is to use softRemove and recover methods. The sample code is as follows −

//find the entities const enty = await repository.find(); 

//soft removed entity const entySoftRemove = await repository.softRemove(enty);

And, you can recover them using recover method as specified below,

await repository.recover(entySoftRemove);

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 repository.save(stud);

This will add new student record into the database.

remove

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

await repository.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 repository.count(Student, { age: 12 });

find

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

const result = await repository.find({ id: 1 });

findOne

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

const result = await repository.findOne({ id: 1 });

clear

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

await repository.clear();
Advertisements