NestJS + TypeORM:使用两个或多个数据库? [英] NestJS + TypeORM: Use two or more databases?

查看:1271
本文介绍了NestJS + TypeORM:使用两个或多个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自2天以来我一直在尝试解决此问题,也许我只是在这里遗漏了要点.

我的目标是编写一个NestJS应用程序(包括TypeORM),该应用程序为我的2个或3个小项目提供RestAPI,而不是为每个项目都编写一个NestJS-App.

到目前为止,该应用程序已经准备就绪,可以与单个项目(与它们的实体,控制器,服务,模块一起位于子文件夹中)一起很好地工作,但是我无法使其与所有项目一起运行. /p>

重点似乎是配置,我正在使用ormconfig.json:

[ {
    "name": "Project1",
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "<username>",
    "password": "<pwd>",
    "database": "<database>",
    "synchronize": false,
    "entities": ["project1/*.entity.ts"],
    "subscribers": ["project1/*.subscriber.ts"],
    "migrations": ["project1/migrations/*.ts"],
    "cli": { "migrationsDir": "project1/migrations" }
}, {
    "name": "project2",
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "<another-username>",
    "password": "<another-pwd>",
    "database": "<another-database>",
    "synchronize": false,
    "entities": ["project2/*.entity.ts"],
    "subscribers": ["project2/*.subscriber.ts"],
    "migrations": ["project2/migrations/*.ts"],
    "cli": { "migrationsDir": "project2/migrations"
    } ]

错误消息显示:

[ExceptionHandler]找不到连接默认值,因为它没有在任何orm配置文件中定义

当然找不到默认",因为我提供了两个配置,它们的唯一名称不同于默认".

ApplicationModule 中,我可以提供连接的名称,如下所示:

TypeOrmModule.forRoot( { name: "project1" } ),

但是它仅适用于一个项目.

我可以将所有内容混合在一个配置中,但随后我会将所有内容都存储在一个数据库中,所有用户都由同一用户使用,并且可能混合实体...

有人可以给我提示如何解决这个问题吗? 也许每个模块中都带有getConnection(<name>),但是然后如何启动ApplicationModule?

亲切的问候,
萨格罗伯特

解决方案

我刚刚尝试使用多个数据库和一个ormconfig.json设置TypeORM,但对我来说根本不起作用.它似乎总是使用default连接,当未找到默认连接(=没有显式名称)时,它将引发相应的错误.

但是当我在app.module.ts中定义连接时确实起作用了(我删除了ormconfig.json):

imports: [
  ...,
  TypeOrmModule.forRoot({
    name: 'Project1',
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: '<username>',
    password: '<pwd>',
    database: '<database>',
    synchronize: false,
    entities: ['project1/*.entity.ts'],
    subscribers: ['project1/*.subscriber.ts'],
    migrations: ['project1/migrations/*.ts'],
    cli: { migrationsDir: 'project1/migrations' },
  }),
  TypeOrmModule.forRoot({
    name: 'project2',
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: '<another-username>',
    password: '<another-pwd>',
    database: '<another-database>',
    synchronize: false,
    entities: ['project2/*.entity.ts'],
    subscribers: ['project2/*.subscriber.ts'],
    migrations: ['project2/migrations/*.ts'],
    cli: { migrationsDir: 'project2/migrations' },
  })
]

I'm trying since 2 days to solve this, perhaps I'm simply missing the point here.

My goal was to write a NestJS app (with TypeORM included) which serves a RestAPI for 2 or 3 of my little projects, instead of writing a NestJS-App for every single one of them.

So far so good, the app is ready, works well with the single projects (which resides in subfolders with their entities, controllers, services, modules), but I can't get it to run with all of them.

The point seems to be the configuration, I'm using ormconfig.json:

[ {
    "name": "Project1",
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "<username>",
    "password": "<pwd>",
    "database": "<database>",
    "synchronize": false,
    "entities": ["project1/*.entity.ts"],
    "subscribers": ["project1/*.subscriber.ts"],
    "migrations": ["project1/migrations/*.ts"],
    "cli": { "migrationsDir": "project1/migrations" }
}, {
    "name": "project2",
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "<another-username>",
    "password": "<another-pwd>",
    "database": "<another-database>",
    "synchronize": false,
    "entities": ["project2/*.entity.ts"],
    "subscribers": ["project2/*.subscriber.ts"],
    "migrations": ["project2/migrations/*.ts"],
    "cli": { "migrationsDir": "project2/migrations"
    } ]

The error message says:

[ExceptionHandler] Cannot find connection default because its not defined in any orm configuration files

Of course "default" couldn't be found, because I'm providing two configs with unique names different to "default".

In ApplicationModule I could provide the name of the connection, like this:

TypeOrmModule.forRoot( { name: "project1" } ),

but then it would work only for one project.

I could mix all in one config, but then I would have everything in one database, same user for all and perhaps mix up the entities...

Can someone give me a hint how to solve this? Perhaps with getConnection(<name>) in every module, but how to start the ApplicationModule then?

Kind regards,
sagerobert

解决方案

I just tried setting up TypeORM with multiple databases and a ormconfig.json and it did not work for me at all. It seemed to always use the default connection and when no default (= without explicit name) connection was found it threw the corresponding error.

It did work though when I defined the connections in the app.module.ts instead (I removed ormconfig.json):

imports: [
  ...,
  TypeOrmModule.forRoot({
    name: 'Project1',
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: '<username>',
    password: '<pwd>',
    database: '<database>',
    synchronize: false,
    entities: ['project1/*.entity.ts'],
    subscribers: ['project1/*.subscriber.ts'],
    migrations: ['project1/migrations/*.ts'],
    cli: { migrationsDir: 'project1/migrations' },
  }),
  TypeOrmModule.forRoot({
    name: 'project2',
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: '<another-username>',
    password: '<another-pwd>',
    database: '<another-database>',
    synchronize: false,
    entities: ['project2/*.entity.ts'],
    subscribers: ['project2/*.subscriber.ts'],
    migrations: ['project2/migrations/*.ts'],
    cli: { migrationsDir: 'project2/migrations' },
  })
]

这篇关于NestJS + TypeORM:使用两个或多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆