”;
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.
Creating a new connection
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,
- name − Name of the database connection.
- type − Database type.
- host − Hostname of the database server.
- port − Database server port.
- username − Account name having access to the database.
- password − Password of the above mentioned account.
- database − Name of the database to connect.
createConnection
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.
Multiple connections
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.
ConnectionManager
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.
”;