Working with Connection
What is Connection
ConnectionYour interaction with the database is only possible once you setup a connection. TypeORM's Connection does not setup a database connection as it might seem, instead it sets up a connection pool. If you are interested in a real database connection, then refer to QueryRunner documentation. Each instance of QueryRunner is a separate isolated database connection. Connection pool setup is established once connect method of the Connection is called. connect method is called automatically if you setup your connection using createConnection function. Disconnection (closing all connections in the pool) is made when close is called. Generally, you must create connection only once in your application bootstrap, and close it after you completely finished working with the database. In practice, if you are building a backend for your site and your backend server always stays running - you never close a connection.
Creating a new connection
There are several ways how a connection can be created. The most simple and common way is to use createConnection and createConnections functions.
createConnection creates a single connection:
import {createConnection, Connection} from "typeorm";
const connection = await createConnection({
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test"
});A single url attribute, plus the type attribute, will work too.
createConnection({
type: 'postgres',
url: 'postgres://test:test@localhost/test'
})createConnections creates multiple connections:
Both these functions create Connection based on connection options you pass and call a connect method. You can create ormconfig.json file in the root of your project and connection options will be automatically read from this file by those methods. Root of your project is the same level where your node_modules directory is.
Different connections must have different names. By default, if connection name is not specified it's equal to default. Usually, you use multiple connections when you use multiple databases or multiple connection configurations.
Once you created a connection you can obtain it anywhere from your app, using getConnection function:
Avoid creating extra classes / services to store and manage your connections. This functionality is already embedded into TypeORM - you don't need to overengineer and create useless abstractions.
Using ConnectionManager
ConnectionManagerYou can create connection using ConnectionManager class. For example:
This is not a general way of creating a connection, but it may be useful for some users. For example, users who want to create connection and store its instance, but have to control when the actual "connection" will be established. Also you can create and maintain your own ConnectionManager:
But note, this way you won't be able to use getConnection() anymore - you'll need to store your connection manager instance and use connectionManager.get to get a connection you need.
Generally avoid this method and avoid unnecessary complications in your application, use ConnectionManager only if you really think you need it.
Working with connection
Once you set your connection up, you can use it anywhere in your app using getConnection function:
You can also use ConnectionManager#get to get a connection, but using getConnection() is enough in most cases.
Using Connection you execute database operations with your entities, particularly using connection's EntityManager and Repository. For more information about them see Entity Manager and Repository documentation.
But generally, you don't use Connection much. Most of the time you only create a connection and use getRepository() and getManager() to access your connection's manager and repositories without directly using connection object:
Last updated
Was this helpful?