NestJS + TypeORM: Use two or more databases?

前端 未结 3 1454
野性不改
野性不改 2020-12-31 09:03

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

3条回答
  •  被撕碎了的回忆
    2020-12-31 09:25

    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: '',
        password: '',
        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: '',
        password: '',
        database: '',
        synchronize: false,
        entities: ['project2/*.entity.ts'],
        subscribers: ['project2/*.subscriber.ts'],
        migrations: ['project2/migrations/*.ts'],
        cli: { migrationsDir: 'project2/migrations' },
      })
    ]
    

提交回复
热议问题