To interact with database, we need a connection object to the database. We need to create a connection object before doing the database operation and has to terminate it once thee database operations are done. Let us learn about Connection API provided by TypeORM in this section.
Before creating a new connection, we need to configure database connection details in the ormconfig.json configuration file. A sample connection details is shown below −
ormconfig.json
{ name: "firstconnection", type: "mysql", host: "localhost", port: 3306, username: "root", password: "root", database: "firstDB" }
Here,
CreateConnection method is provided by TypeORM to create a new connection. It is defined as below,
import { createConnection, Connection } from "typeorm"; const connection = await createConnection({ });
Here, createConnection will use the configuration details specified in the ormconfig.json file.
Alternatively, you can define the connection URL as arguments to the createConnection method as specified follows −
const connection = createConnection({ type: 'mysql', url: 'localhost:8888/firstDB' })
Here,
createConnection returns an object, which can be used to open / close the connection to the database.
TypeORM provides an option to create multiple database connection as well. First, the configuration file ormconfig.json can be used to specify the details of the multiple database connection. Let us configure multiple databases in ormconfig.json as specified follows,
ormconfig.json
{ name: "firstconnection", type: "mysql", host: "localhost", port: 3306, username: "root", password: "root", database: "firstDB" }, { name: "secondconnection", type: "mysql", host: "localhost", port: 3306, username: "root", password: "root", database: "secondDB" }, { name: "thirdconnection", type: "mysql", host: "localhost", port: 3306, username: "root", password: "root", database: "thirdDB" }
Now, we can use the argument provided by the createConnection method to specify the name of the connection to create connection object as mentioned below −
const firstconnection: Connection = await createConnection("firstconnection");
Here,
createConnection will use the configuration details of the firstconnection specified in the ormconfig.json file to create the connection object.
TypeORM also provides yet another API, createConnections to create multiple connection as once and then, use it whenever necessary as specified below −
import { createConnections, Connection } from "typeorm"; const connections: Connection[] = await createConnections([ ]);
Here,
connections hold all the connection objects as an array.
TypeORM also provides an another API, connectionManager to create connection. It is defined below −
import {getConnectionManager, ConnectionManager, Connection} from "typeorm"; const connectionManager = getConnectionManager(); const connection = connectionManager.create({ }); await connection.connect();
TypeORM prefers the usage of createConnection over ConnectionManager to create connection objects.