我如何从ApplicationContext获取实体的存储库

时间:2019-04-08 01:38:18

标签: typescript nestjs

如何从ApplicationContext获取实体的存储库

const ctx = await NestFactory.createApplicationContext(AppModule)
const repository = ctx.get<Repository<UserEntity>>(Repository);

// Error: Nest cannot find given element (it does not exist in current context)

1 个答案:

答案 0 :(得分:0)

想通了

import ...
import { UserEntity } from './user.entity';
import { getRepositoryToken } from '@nestjs/typeorm';

const ctx = await NestFactory.createApplicationContext(AppModule)
const repository: Repository<UserEntity> = ctx.get(getRepositoryToken(UserEntity));