【问题标题】:NestJS/TypeORM: Can custom repository extend from another custom repository which is inside another project?NestJS/TypeORM:自定义存储库可以从另一个项目内的另一个自定义存储库扩展吗?
【发布时间】:2022-03-08 23:56:06
【问题描述】:

错误

RepositoryNotFoundError:找不到“UserQueryRepository”的存储库。看起来这个实体没有在当前的“默认”连接中注册?

版本

  • @nestjs/typeorm - 7.1.5
  • @nestjs/common - 7.6.15
  • @nestjs/core - 7.6.15
  • nodejs - 12.21.0
  • typeorm - 0.2.34

问题描述

我有 2 个项目:common & microservice-1

Common 包有 QueryService 和 QueryRepository 并且 microservice-1 应该从这些文件扩展。

所以在 microservice-1 中,我有从 QueryService 扩展的 UserQueryService 和从 QueryRepository 扩展的 UserQueryRepository。

query.repository.ts

import { FindManyOptions, Repository } from 'typeorm';

export class QueryRepository<Entity> extends Repository<Entity> {
  findAll(options?: FindManyOptions<Entity>): Promise<Entity[]> {
    return this.find(options);
  }
}

query.service.ts

import { FindManyOptions } from 'typeorm';
import { QueryRepository } from '../repository';

export class QueryService<Entity> {
  constructor(protected readonly repository: QueryRepository<Entity>) {}

  findById(id: string): Promise<Entity> {
    return this.repository.findOne(id);
  }

  findAll(options?: FindManyOptions<Entity>): Promise<Entity[]> {
    return this.repository.findAll(options);
  }
}

user.query.repository.ts

import { QueryRepository } from '@project/common';
import { EntityRepository, Repository } from 'typeorm';
import { UserEntity } from '../entity';

@EntityRepository(UserEntity)
export class UserQueryRepository
  extends QueryRepository<UserEntity> { // -> throws error
  // extends Repository<UserEntity> { // -> works without extending from common
}

user.query.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { QueryService } from '@project/common';
import { UserEntity } from '../entity';
import { UserQueryRepository } from '../repository';

@Injectable()
export class UserQueryService extends QueryService<UserEntity> {
  constructor(
    @InjectRepository(UserQueryRepository)
    repository: UserQueryRepository,
  ) {
    super(repository);
  }

  getAllUsers(): Promise<UserEntity[]> {
    return this.findAll();
  }
}

user.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserController } from '../controller';
import { UserQueryRepository } from '../repository';
import { UserQueryService } from '../service';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'user',
      password: 'pass',
      database: 'dbname',
      autoLoadEntities: true,
      synchronize: true,
    }),
    TypeOrmModule.forFeature([UserQueryRepository]),
  ],
  controllers: [UserController],
  providers: [UserQueryService],
})
export class UserModule {}

【问题讨论】:

    标签: microservices nestjs typeorm npm-package


    【解决方案1】:

    我认为您缺少添加实体。

    TypeOrmModule.forRoot({
      type: 'postgres',
      url: 'postgres://user:pass@localhost:5432/dbname',
      autoLoadEntities: true,
      synchronize: true,
      entities: [__dirname + '/../**/entity/*.entity{.ts,.js}'],
    }),
    

    PS:路径来自dist 而不是src

    例如:__dirname + '/../user/entity/*.entity{.ts,.js}'

    【讨论】:

    • 实体只在这个例子中丢失,在实际应用中没关系。当我们使用具有基本存储库的通用包并尝试在不同的微服务中扩展此通用存储库时,就会出现问题。我们决定切换到也解决了这个问题的 monorepo 方法,因为有了 monorepo,我们共享了公共模块的依赖关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2021-01-07
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多