TypeORM

TypeORM - data-mapper ORM for TypeScript and JavaScript (ES7, ES6, ES5). Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL databases. Works in Node.JS and Browser.

Connection Manager

ConnectionManager is used to store and manage all these different connections. You can get any connection using connection manager. It also provides useful factory methods to simplify connection creation. But for creation in most of time you only need use createConnection method from "typeorm" namespace.

Accessing ConnectionManager

ConnectionManager is a singleton and you can access it anywhere from your app by using getConnectionManager() function from typeorm package. For example:


import {getConnectionManager} from "typeorm";
const connectionManager = getConnectionManager();
        

If you are using dependency injection container (we are recommend to use typedi, but you can use any DI container with TypeORM), then you can access (or inject) connection manager from anywhere in your app. For example:


import {ConnectionManager} from "typeorm";
import {Container} from "typedi";
const connectionManager = Container.get(ConnectionManager);
        

More about using DI container in TypeORM [here].

Creating a new connection using ConnectionManager

There are several methods in the ConnectionManager you can use:

But the simplest way to create a connection is to use function from a typeorm module: createConnection(options: ConnectionOptions)

Accessing any connections

You can simply access other connection by using get(connectionName: string) method. For example you can do this:


import {getConnectionManager} from "typeorm";
const connectionManager = getConnectionManager();
const defaultConnection = getConnectionManager().get();
const secondaryConnection = getConnectionManager().get("secondary-connection");
        

Connection Manager API:

Here is a public API of ConnectionManager that you can use:

Fork me on GitHub