Krimson
Krimson

Reputation: 7674

How do I get an Entity's repository from the ApplicationContext

How do I get an Entity's repository from the 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)

Upvotes: 2

Views: 1122

Answers (1)

Krimson
Krimson

Reputation: 7674

Figured it out

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));

Upvotes: 5

Related Questions