0
votes

Connecting to two databases using TypeORM and NestJS throws a ConnectionNotFoundError when a custom repository is registered using the connection (UserConnection & PhotoConnection) one for each database. I have created a minimal repo here https://github.com/masonridge/connissuewithndb. The TypeOrm registration is done in the AppModule of NestJS

(app.module.ts)

@Module({
  imports: [

    TypeOrmModule.forRootAsync({
      name: 'PhotoConnection',
      useFactory: async () => {
        return {
          type: 'sqlite',
          synchronize: true,
          database: 'TestPhoto.sqlite',
          entities: [Photo],
        } as SqliteConnectionOptions;
      },
    }),
    TypeOrmModule.forRootAsync({
      name: 'UserConnection',
      useFactory: async () => {
        return {
          type: 'sqlite',
          synchronize: true,
          database: 'TestUser.sqlite',
          entities: [User],
        } as SqliteConnectionOptions;
      },
    }),
    // TypeOrmModule.forFeature([User, UserRepository], 'UserConnection'),
    // TypeOrmModule.forFeature([Photo, PhotoRepository], 'PhotoConnection'),
    PhotoModule, UserModule,
  ],

(photo.module.ts) DB connection1 - PhotoConnection is registered here

@Module({
  imports: [    
    TypeOrmModule.forFeature([PhotoRepository], 'PhotoConnection'),
  ],
  providers: [
    PhotoService,
  ],
  controllers: [PhotoController],
  exports: [TypeOrmModule],
})
export class PhotoModule {}

(user.module.ts) DB connection2 - UserConnection is registered here

@Module({
  imports: [
    TypeOrmModule.forFeature([UserRepository], 'UserConnection'),
  ],
  providers: [
    UserService,
  ],
  exports: [TypeOrmModule],
  controllers: [UserController],
})
export class UserModule {}

(user.repository.ts) Custom repository

@EntityRepository(User)
export class UserRepository extends Repository<User> {
  async createUser(name: string): Promise<string> {
    const user = this.create();
    user.username = name;
    user.salt = 'salt';
    user.password = 'xxkdkdk';
    user.save();
    return name;
  }
}

(photo.repository.ts)

@EntityRepository(Photo)
export class PhotoRepository extends Repository<Photo> {
  async createPhoto(name: string): Promise<string> {
    const photo = this.create();
    photo.name = name;
    photo.save();
    return name;
  }
}

The repo is injected into the service using the connection (PhotoConnection) (photo.service.ts)

export class PhotoService {
  constructor(
    @InjectRepository(Photo, 'PhotoConnection')
        private readonly photoRepository: PhotoRepository,
  ) {}

and here using UserConnection (user.service.ts)

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User, 'UserConnection')
    private readonly userRepository: UserRepository,
  ) {}

The application starts fine but on a POST request it throws a ConnectionNotFoundError error

(node:190812) UnhandledPromiseRejectionWarning: ConnectionNotFoundError: Connection "default" was not found. at new ConnectionNotFoundError (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\error\ConnectionNotFoundError.js:10:28) at ConnectionManager.get (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\connection\ConnectionManager.js:38:19) at Object.getConnection (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\index.js:244:35) at Function.BaseEntity.getRepository (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\repository\BaseEntity.js:67:57) at Photo.BaseEntity.save (C:\nestjs\type-orm-dbscratch\node_modules\typeorm\repository\BaseEntity.js:27:33) at PhotoRepository.createPhoto (C:\nestjs\type-orm-dbscratch\dist\db\photo\photo.repository.js:15:15) at PhotoService.createPhoto (C:\nestjs\type-orm-dbscratch\dist\db\photo\photo.service.js:24:43) at PhotoController.addSetting (C:\nestjs\type-orm-dbscratch\dist\db\photo\photo.controller.js:22:27) at C:\nestjs\type-orm-dbscratch\node_modules@nestjs\core\router\router-execution-context.js:37:29 at process._tickCallback (internal/process/next_tick.js:68:7)

I would like to know if there is an issue with the registration. Any help would be appreciated.

1

1 Answers

4
votes

It looks like the issue is between using the TypeORM approach of DataMapper and ActiveRecord. It seems using the ActiveRecord approach does not support named repositories with NestJS, and as such was looking for connection named 'default'. If you remove the extends BaseEntity and use the repository's save method instead this.save(user) vs user.save() you'll get a successful save. I didn't see an immediate way to set the connection of the ActiveRecord but it may be an option somewhere if that is still the approach you would like to follow.