How to create a connection pool using TypeOrm? While exploring TypeOrm, I wanted to create pool of connections for working with MySql
Below is the code snippet :
import { createConnection } from 'typeorm';
export const databaseProviders = [
{
provide: 'DbConnectionToken',
useFactory: async () => await createConnection({
type: 'mysql',
host: 'localhost',
port: 8889,
username: 'root',
password: 'root',
database: 'typeorm_test',
entities: [
__dirname + '/../**/**.entity{.ts,.js}',
],
autoSchemaSync: true,
logging: 'all',
}),
},
];
TypeORM by default uses a connection pool which defaults to 10 connections. If you want to have custom pooling limit (advisable), the same can be mentioned for connectionLimit
under extra
options which are passed to the underlying MySQL driver.
[
{
"name": "default",
"type": "mysql",
"host": "mysql.db",
"port": 3306,
"username": "appUser",
"password": "appRandomPassword",
"database": "entity_schema",
"entities": [
"dist/models/entities/**/*.js"
],
"logging": [
"error"
],
"extra": {
"connectionLimit": 5
}
}
]
MySQL Connection pooling options which can be passed under extra
, if required.