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.
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
}
}
]
TypeORM Docs
MySQL Connection pooling options which can be passed under extra
, if required.
TypeORM always creates you a connection pool out of the box, you don't need to setup anything. It uses one connection from the pool per one request to repository/entity manager method, or per one transaction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With