NestJs:如何将一个存储库导入另一个存储库?
很抱歉,这可能是一个非常业余的问题。来自拉雷维尔,这仍然让我很困惑,我只是不明白需要什么 我有一个NestJs:如何将一个存储库导入另一个存储库?,nestjs,Nestjs,很抱歉,这可能是一个非常业余的问题。来自拉雷维尔,这仍然让我很困惑,我只是不明白需要什么 我有一个user.repository.ts文件和一个location.repository.ts文件。每个都有自己的模块、控制器和服务。我已经成功地为每个实体创建了CRUD操作,但现在我正在努力建立多对多关系 在我的user.repository.ts文件中,我试图保存一个相关的(多对多)存储库: // user.repository.ts user.locations = await this.loc
user.repository.ts
文件和一个location.repository.ts
文件。每个都有自己的模块、控制器和服务。我已经成功地为每个实体创建了CRUD操作,但现在我正在努力建立多对多关系
在我的user.repository.ts
文件中,我试图保存一个相关的(多对多)存储库:
// user.repository.ts
user.locations = await this.locationRepository.findByIds(locations);
...
await user.save();
我不确定如何插入或导入location.repository.ts
文件。我尝试过将服务导入每个模块的多种方法。或将每个模块导入另一个模块。我尝试过不同的版本:
@EntityRepository(User)
@EntityRepository(Location)
或者将LocationService
导入UserService
在Laravel中,这就像$model->sync($relationship)一样“简单”代码>
如何将locationRepository
导入/注入我的userRepository
?谢谢你的建议 我假设这个问题与您的相关,最简单的实现方法是将位置
实体添加到您的用户模块
@Module({
imports:[TypeOrmModule.forFeature([UserRepository,LocationRepository])], // or just Location entity [UserRepository,Location] if you didn't setup a custom LocationRepository
在那之后,像你为userRepo所做的那样将它注入到你的。。。服务
constructor(
@InjectRepository(LocationRepository)
private locationRepository: LocationRepository,
// or just private locationRepository: Repository<Location>,
) {}
如果您还有其他问题,请尽管提非常感谢!你说得对,这两个问题是相关的;我希望能帮助解决一些问题来帮助其他人。
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