Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create connection pool TypeOrm

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',
    }),
  },
];
like image 600
prranay Avatar asked Oct 20 '17 06:10

prranay


People also ask

What is connection pool in TypeORM?

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.


Video Answer


2 Answers

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.

like image 110
Sagar Chilukuri Avatar answered Oct 19 '22 21:10

Sagar Chilukuri


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.

like image 5
pleerock Avatar answered Oct 19 '22 21:10

pleerock