让我们来看看我得到了一个根模块,如:
@Module({
imports: [MikroOrmModule.forRoot({
metadataProvider: TsMorphMetadataProvider,
entities: ['dist/**/*.entity.js'],
entitiesTs: ['src/**/*.entity.ts'],
type: 'postgresql' as const,
baseDir: path.resolve(__dirname, '..'),
...
})],
})
export class AppModule {}
字符串
使用此设置,我可以注入EntityManager
,但@InjectRepository(User)
无法找到提供程序。
@Injectable()
export class SomeService {
constructor(@InjectRepository(user) userRepository: EntityRepository<User>) {}
}
型
如果我也将MikroOrmModule.forFeature([User])
插入到根模块中,它就不起作用了。如果我将实体移动到一个子模块中,并在那里使用.forFeature
(就像文档中的例子所说的那样),它就起作用了。
有没有可能让@InjectRepository()
在不需要子模块和手动引用的情况下工作?因为我已经找到了带有blob的实体,再次手动引用它们似乎违背了blob模式的目的。
2条答案
按热度按时间vohkndzv1#
blob模式是告诉ORM所有实体都有哪些,以及要连接到什么。
forFeature
模式是向Nest注册所有实体,以便Nest可以为它们创建注入令牌。由于它们是独立的进程,因此无法只使用其中一个。除非MikroOrmModule
实现类似Nest的autoLoadEntities
的东西(在TypeOrmModule和SequelizeModule上),没有办法只使用globs。lxkprmvk2#
是的
模块
字符串
服务
型
然后你就可以把私有的只读em:multityManager注入到控制器中