tsed-example-typeorm
tsed-example-typeorm copied to clipboard
Can i inject TypeORM Repository in my injectable service ?
Hello,
I have TypeORM UserRepository
import { EntityRepository, Repository } from 'typeorm';
import { User } from '../entity/User';
@EntityRepository(User)
export class UserRepository extends Repository<User> {
}
And i would like to inject it in my UserService like this
import { Service } from '@tsed/di';
import { UserRepository } from '../repository/UserRepository';
@Service()
export class UserService {
constructor(private readonly userRepository: UserRepository) {}
public getAll() {
return this.userRepository.find();
}
}
Is that even possible? with @Service decorator on UserRepository Class, the value of injectable userRepository is undefined.
Currently inject a class from the TypeORM isn't supported.
But you can find different way to doing that here: https://github.com/TypedProject/ts-express-decorators/issues/611
http://tsed.io/tutorials/typeorm.html#entityrepository
It’s now supported by using EntityRepository from tsed instead of typeorm
This is totally doable. I just tried this and it works. However I have problems to Mock repository in my integration testing.