# Multiple connections, databases, schemas and replication setup

# Using multiple connections

The simplest way to use multiple databases is to create different connections:

import {createConnections} from "typeorm";

const connections = await createConnections([{
    name: "db1Connection",
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "admin",
    database: "db1",
    entities: [__dirname + "/entity/*{.js,.ts}"],
    synchronize: true
}, {
    name: "db2Connection",
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "admin",
    database: "db2",
    entities: [__dirname + "/entity/*{.js,.ts}"],
    synchronize: true
}]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

This approach allows you to connect to any number of databases you have and each database will have its own configuration, own entities and overall ORM scope and settings.

For each connection a new Connection instance will be created. You must specify a unique name for each connection you create.

The connection options can also be loaded from an ormconfig file. You can load all connections from the ormconfig file:

import {createConnections} from "typeorm";

const connections = await createConnections();
1
2
3

or you can specify which connection to create by name:

import {createConnection} from "typeorm";

const connection = await createConnection("db2Connection");
1
2
3

When working with connections you must specify a connection name to get a specific connection:

import {getConnection} from "typeorm";

const db1Connection = getConnection("db1Connection");
// you can work with "db1" database now...

const db2Connection = getConnection("db2Connection");
// you can work with "db2" database now...
1
2
3
4
5
6
7

Benefit of using this approach is that you can configure multiple connections with different login credentials, host, port and even database type itself. Downside for might be that you'll need to manage and work with multiple connection instances.

# Using multiple databases in a single connection

If you don't want to create multiple connections, but want to use multiple databases in a single connection, you can specify database name per-entity you use:

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity({ database: "secondDB" })
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity({ database: "thirdDB" })
export class Photo {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    url: string;

}
1
2
3
4
5
6
7
8
9
10
11
12

User entity will be created inside secondDB database and Photo entity inside thirdDB database. All other entities will be created in default connection database.

If you want to select data from a different database you only need to provide an entity:

const users = await connection
    .createQueryBuilder()
    .select()
    .from(User, "user")
    .addFrom(Photo, "photo")
    .andWhere("photo.userId = user.id")
    .getMany(); // userId is not a foreign key since its cross-database request
1
2
3
4
5
6
7

This code will produce following sql query (depend on database type):

SELECT * FROM "secondDB"."user" "user", "thirdDB"."photo" "photo" 
    WHERE "photo"."userId" = "user"."id"
1
2

You can also specify a table path instead of the entity:

const users = await connection
    .createQueryBuilder()
    .select()
    .from("secondDB.user", "user")
    .addFrom("thirdDB.photo", "photo")
    .andWhere("photo.userId = user.id")
    .getMany(); // userId is not a foreign key since its cross-database request
1
2
3
4
5
6
7

This feature is supported only in mysql and mssql databases.

# Using multiple schemas in a single connection

You can use multiple schemas in your applications, just set schema on each entity:

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity({ schema: "secondSchema" })
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity({ schema: "thirdSchema" })
export class Photo {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    url: string;

}
1
2
3
4
5
6
7
8
9
10
11
12

User entity will be created inside secondSchema schema and Photo entity inside thirdSchema schema. All other entities will be created in default connection schema.

If you want to select data from a different schema you only need to provide an entity:

const users = await connection
    .createQueryBuilder()
    .select()
    .from(User, "user")
    .addFrom(Photo, "photo")
    .andWhere("photo.userId = user.id")
    .getMany(); // userId is not a foreign key since its cross-database request
1
2
3
4
5
6
7

This code will produce following sql query (depend on database type):

SELECT * FROM "secondSchema"."question" "question", "thirdSchema"."photo" "photo" 
    WHERE "photo"."userId" = "user"."id"
1
2

You can also specify a table path instead of the entity:

const users = await connection
    .createQueryBuilder()
    .select()
    .from("secondSchema.user", "user") // in mssql you can even specify a database: secondDB.secondSchema.user
    .addFrom("thirdSchema.photo", "photo") // in mssql you can even specify a database: thirdDB.thirdSchema.photo
    .andWhere("photo.userId = user.id")
    .getMany();
1
2
3
4
5
6
7

This feature is supported only in postgres and mssql databases. In mssql you can also combine schemas and databases, for example:

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity({ database: "secondDB", schema: "public" })
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Replication

You can setup read/write replication using TypeORM. Example of replication connection settings:

{
  type: "mysql",
  logging: true,
  replication: {
    master: {
      host: "server1",
      port: 3306,
      username: "test",
      password: "test",
      database: "test"
    },
    slaves: [{
      host: "server2",
      port: 3306,
      username: "test",
      password: "test",
      database: "test"
    }, {
      host: "server3",
      port: 3306,
      username: "test",
      password: "test",
      database: "test"
    }]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

All schema update and write operations are performed using master server. All simple queries performed by find methods or select query builder are using a random slave instance. All queries performed by query method are performed using the master instance.

If you want to explicitly use master in SELECT created by query builder, you can use the following code:

const masterQueryRunner = connection.createQueryRunner("master");
try {
    const postsFromMaster = await connection.createQueryBuilder(Post, "post")
        .setQueryRunner(masterQueryRunner)
        .getMany();
} finally {
      await masterQueryRunner.release();
}
        
1
2
3
4
5
6
7
8
9

If you want to use slave in raw queries, you also need to explicitly specify the query runner.


const slaveQueryRunner = connection.createQueryRunner("slave");
try {
    const userFromSlave = await connection.query('SELECT * FROM users WHERE id = $1', [userId], slaveQueryRunner);
} finally {
    return slaveQueryRunner.release();
}
1
2
3
4
5
6
7

Note that connection created by a QueryRunner need to be explicitly released.

Replication is supported by mysql, postgres and sql server databases.

Mysql supports deep configuration:

{
  replication: {
    master: {
      host: "server1",
      port: 3306,
      username: "test",
      password: "test",
      database: "test"
    },
    slaves: [{
      host: "server2",
      port: 3306,
      username: "test",
      password: "test",
      database: "test"
    }, {
      host: "server3",
      port: 3306,
      username: "test",
      password: "test",
      database: "test"
    }],
    
    /**
    * If true, PoolCluster will attempt to reconnect when connection fails. (Default: true)
    */
    canRetry: true,

    /**
     * If connection fails, node's errorCount increases.
     * When errorCount is greater than removeNodeErrorCount, remove a node in the PoolCluster. (Default: 5)
     */
    removeNodeErrorCount: 5,

    /**
     * If connection fails, specifies the number of milliseconds before another connection attempt will be made.
     * If set to 0, then node will be removed instead and never re-used. (Default: 0)
     */
     restoreNodeTimeout: 0,

    /**
     * Determines how slaves are selected:
     * RR: Select one alternately (Round-Robin).
     * RANDOM: Select the node by random function.
     * ORDER: Select the first node available unconditionally.
     */
    selector: "RR"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49