且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NestJs:如何将一个存储库导入另一个存储库?

更新时间:2023-01-08 10:13:16

我认为这个问题与您的 最后一个问题,最简单的实现方法,将Location实体添加到你的UserModule

I assume this question is related to your last question, the simplest way to implement it, Add Locationentity to your UserModule

@Module({
imports:[TypeOrmModule.forFeature([UserRepository,LocationRepository])], // or just Location entity [UserRepository,Location] if you didn't setup a custom LocationRepository

之后,将它注入到你的 userRepo... 服务中

After that, inject it in your as what you did for userRepo... service

constructor(
    @InjectRepository(LocationRepository)
    private locationRepository: LocationRepository,
    // or just  private locationRepository: Repository<Location>,
  ) {}

在创建方法服务中获取您的位置:

In the create method service get your locations:

 async  createUser(createUserDto: CreateUserDto) { // usersSrvice
let locations = await this.locationRepository.findByIds(createUserDto.locations);
await this.userRepository.createMethodeName(createUserDto,locations) // add secand 
params for your locations 

如果您有任何其他问题,请随时提出