getConnection() - Gets connection which was created by using createConnection method.
import {getConnection} from"typeorm";constconnection=getConnection();// if you have named connection you can specify its name:constsecondaryConnection=getConnection("secondary-connection");
getEntityManager() - Gets EntityManager from connection.
Connection name can be specified to indicate what connection's entity manager should be taken.
import {getEntityManager} from"typeorm";constmanager=getEntityManager();// you can use manager methods nowconstsecondaryManager=getEntityManager("secondary-connection");// you can use secondary connection manager methods
getRepository() - Gets Repository for given entity from connection.
Connection name can be specified to indicate what connection's entity manager should be taken.
import {getRepository} from"typeorm";constuserRepository=getRepository(User);// you can use repository methods nowconstblogRepository=getRepository(Blog,"secondary-connection");// you can use secondary connection repository methods
getTreeRepository() - Gets TreeRepository for given entity from connection.
Connection name can be specified to indicate what connection's entity manager should be taken.
import {getTreeRepository} from"typeorm";constuserRepository=getTreeRepository(User);// you can use repository methods nowconstblogRepository=getTreeRepository(Blog,"secondary-connection");// you can use secondary connection repository methods
getMongoRepository() - Gets MongoRepository for given entity from connection.
Connection name can be specified to indicate what connection's entity manager should be taken.
import {getMongoRepository} from"typeorm";constuserRepository=getMongoRepository(User);// you can use repository methods nowconstblogRepository=getMongoRepository(Blog,"secondary-connection");// you can use secondary connection repository methods
Connection API
name - Connection name. If you created nameless connection then it's equal to "default".
You use this name when you work with multiple connections and call getConnection(connectionName: string)
constconnectionName:string=connection.name;
options - Connection options used to create this connection.
constconnectionOptions:ConnectionOptions=connection.options;// you can cast connectionOptions to MysqlConnectionOptions// or any other xxxConnectionOptions depending on the database driver you use
isConnected - Indicates if a real connection to the database is established.
constisConnected:boolean=connection.isConnected;
driver - Underlying database driver used in this connection.
constdriver:Driver=connection.driver;// you can cast connectionOptions to MysqlDriver// or any other xxxDriver depending on the database driver you use
manager - EntityManager used to work with connection entities.
constmanager:EntityManager=connection.manager;// you can call manager methods, for example find:constuser=awaitmanager.findOne(1);
mongoManager - MongoEntityManager used to work with connection entities in mongodb connections.
For more information about MongoEntityManager see MongoDB documentation.
constmanager:MongoEntityManager=connection.mongoManager;// you can call manager or mongodb-manager specific methods, for example find:constuser=awaitmanager.findOne(1);
connect - Performs connection to the database.
When you use createConnection it automatically calls connect and you don't need to call it yourself.
awaitconnection.connect();
close - Closes connection with the database.
Usually, you call this method when your application is shutting down.
awaitconnection.close();
synchronize - Synchronizes database schema. When synchronize: true is set in connection options it calls this method.
Usually, you call this method when your application is starting.
awaitconnection.synchronize();
dropDatabase - Drops the database and all its data.
Be careful with this method on production since this method will erase all your database tables and their data.
Can be used only after connection to the database is established.
awaitconnection.dropDatabase();
runMigrations - Runs all pending migrations.
awaitconnection.runMigrations();
undoLastMigration - Reverts last executed migration.
awaitconnection.undoLastMigration();
hasMetadata - Checks if metadata for a given entity is registered.
constrepository=connection.getTreeRepository(Category);// now you can call tree repository methods, for example findTrees:constcategories=awaitrepository.findTrees();
getMongoRepository - Gets MongoRepository of the given entity.
This repository is used for entities in MongoDB connection.
constrepository=connection.getMongoRepository(User);// now you can call mongodb-specific repository methods, for example createEntityCursor:constcategoryCursor=repository.createEntityCursor();constcategory1=awaitcategoryCursor.next();constcategory2=awaitcategoryCursor.next();
getCustomRepository - Gets custom defined repository.
constuserRepository=connection.getCustomRepository(UserRepository);// now you can call methods inside your custom repository - UserRepository classconstcrazyUsers=awaituserRepository.findCrazyUsers();
transaction - Provides a single transaction where multiple database requests will be executed in a single database transaction.
awaitconnection.transaction(async manager => {// NOTE: you must perform all database operations using given manager instance// its a special instance of EntityManager working with this transaction// and don't forget to await things here});
query - Executes a raw SQL query.
constrawData=awaitconnection.query(`SELECT * FROM USERS`);
createQueryBuilder - Creates a query builder, which can be used to build queries.
constqueryRunner=connection.createQueryRunner();// you can use its methods only after you call connect// which performs real database connectionawaitqueryRunner.connect();// .. now you can work with query runner and call its methods// very important - don't forget to release query runner once you finished working with itawaitqueryRunner.release();
ConnectionManager API
create - Creates a new connection and register it in the manager.