本文介绍了创建连接池 TypeOrm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 TypeOrm 创建连接池?在探索 TypeOrm 时,我想创建连接池以使用 MySql

以下是代码片段:

import { createConnection } from 'typeorm';导出 const databaseProviders = [{提供:'DbConnectionToken',useFactory: async() =>等待创建连接({类型:'mysql',主机:'本地主机',端口:8889,用户名:'root',密码:'root',数据库:'typeorm_test',实体:[__dirname + '/../**/**.entity{.ts,.js}',],autoSchemaSync:真,日志记录:'全部',}),},];
解决方案

TypeORM 默认使用默认为 10 个连接的连接池.如果您想要自定义池限制(建议),可以在传递给底层 MySQL 驱动程序的 extra 选项下对 connectionLimit 提及相同的内容.

[{"name": "默认","type": "mysql","host": "mysql.db",端口":3306,"用户名": "appUser","password": "appRandomPassword","数据库": "entity_schema",实体":[dist/models/entities/**/*.js"],记录":[错误"],额外的": {连接限制":5}}]

TypeORM 文档

MySQL 连接池选项,可以在 extra,如果需要.

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
        }
    }
]

TypeORM Docs

MySQL Connection pooling options which can be passed under extra, if required.

这篇关于创建连接池 TypeOrm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 22:49