typeorm 0.3.6 -DB连接每个请求

发布于 2025-02-05 04:07:13 字数 788 浏览 2 评论 0原文

我从0.2。我不确定如何使用新数据源处理多租户连接。 我以前的实现是基于ConnectionManager,它看起来像这样:

{
    const connectionManager = getConnectionManager();

    // Check if tenant connection exists
    if (connectionManager.has(tenant.name)) {
      const connection = connectionManager.get(tenant.name);
      return Promise.resolve(
        connection.isConnected ? connection : connection.connect()
      );
    }

    // Create new tenant connection
    return createConnection({
      type: 'postgres',
      name: tenant.name,
      host: tenant.host,
      port: tenant.port,
      username: tenant.username,
      password: tenant.password,
      database: tenant.database,
      entities: [...TenantModule.entities],
    });
}

现在已贬低了连接Manger。保持自己的连接阵列对我来说听起来不正确。 关于如何正确处理的任何想法?

I’m migrating from 0.2.* to 0.3.6 version of typeorm. I’m not sure how to handle multi-tenant connections with new DataSource.
My previous implementations was based on connectionManager and it looked something like this:

{
    const connectionManager = getConnectionManager();

    // Check if tenant connection exists
    if (connectionManager.has(tenant.name)) {
      const connection = connectionManager.get(tenant.name);
      return Promise.resolve(
        connection.isConnected ? connection : connection.connect()
      );
    }

    // Create new tenant connection
    return createConnection({
      type: 'postgres',
      name: tenant.name,
      host: tenant.host,
      port: tenant.port,
      username: tenant.username,
      password: tenant.password,
      database: tenant.database,
      entities: [...TenantModule.entities],
    });
}

Connection manger is now deprecated. Maintaining my own array of connections doesn’t sound right to me.
Any ideas on how this should be handled the correct way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2025-02-12 04:07:13

使用typeorm:0.3.6 getConnectioncreateConnection等等。您可以在这里

创建新连接的新连接,您将必须使用dataSource如下:

import { DataSource, DataSourceOptions } from 'typeorm';

const dataSourceOptions: DataSourceOptions = {
  type: 'mysql',
  host,
  port,
  username,
  password,
  database,
  synchronize: false,
  logging: false,
  entities: ['src/database/entity/*.ts'],
  migrations: ['src/database/migration/*.ts'],
};

export const AppDataSource = new DataSource(dataSourceOptions);

其中new DataSource等于new ConnectiondataSource是相等的到getConnection

要检查您是否与数据库连接,您将使用AppDatasource

AppDataSource.initialize()
  .then(() => {
    // db initialized
  })
  .catch((err: Error) => {
    throw new Error(`'Database connection error: ${err}`);
  });

with typeorm: 0.3.6 getConnection, createConnection among others are deprecated. You can find migration guide in here

to create a new connection, you will have to use DataSource as follows:

import { DataSource, DataSourceOptions } from 'typeorm';

const dataSourceOptions: DataSourceOptions = {
  type: 'mysql',
  host,
  port,
  username,
  password,
  database,
  synchronize: false,
  logging: false,
  entities: ['src/database/entity/*.ts'],
  migrations: ['src/database/migration/*.ts'],
};

export const AppDataSource = new DataSource(dataSourceOptions);

where new DataSource is equivalent to new Connection and dataSource is equal to getConnection.

To check if you are connection to database, you will have utilize AppDataSource:

AppDataSource.initialize()
  .then(() => {
    // db initialized
  })
  .catch((err: Error) => {
    throw new Error(`'Database connection error: ${err}`);
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文