NestJS + TypeORM: Use two or more databases?

前端 未结 3 1439
野性不改
野性不改 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:14

    You need to explicitly pass the connection name at the same level inside TypeOrmModule.forRoot({ name: 'db1Connection' }) incase you are using multiple database connections.

    TypeOrmModule.forRootAsync({
      name: DB1_CONNECTION,
      imports: [ConfigModule],
      useClass: TypeormDb1ConfigService,
    }),
    
    TypeOrmModule.forRootAsync({
      name: DB2_CONNECTION,
      imports: [ConfigModule],
      useClass: TypeormDb2ConfigService,
    })
    
    0 讨论(0)
  • 2020-12-31 09:15

    For clarity and for other developers to come to this post:

    From NestJS documentation:

    If you don't set any name for a connection, its name is set to default. Please note that you shouldn't have multiple connections without a name, or with the same name, otherwise they simply get overridden.

    One of your connections must have one of the following:

    1. "name":"default"
    2. Without any name.

    I would recommend to declare all your connections in ormconfig.json and not declare it in code.

    An example to import the connections from ormconfig.json:

    @Module({
        imports: [TypeOrmModule.forFeature([Entity1, Entity2]), //This will use default connection
        TypeOrmModule.forRoot({name: 'con1'}), // This will register globaly con1
        TypeOrmModule.forRoot({name: 'con2'}), // This will register globaly con2
        controllers: [...],
        providers: [...],
        exports: [...]
    })
    

    in your module (not have to be the root module, only the modules you will need the connections).

    0 讨论(0)
  • 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: '<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' },
      })
    ]
    
    0 讨论(0)
提交回复
热议问题