tsed-example-typeorm icon indicating copy to clipboard operation
tsed-example-typeorm copied to clipboard

Can i inject TypeORM Repository in my injectable service ?

Open amourlanne opened this issue 5 years ago • 5 comments

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.

amourlanne avatar Dec 17 '19 17:12 amourlanne

Currently inject a class from the TypeORM isn't supported.

Romakita avatar Dec 18 '19 08:12 Romakita

But you can find different way to doing that here: https://github.com/TypedProject/ts-express-decorators/issues/611

Romakita avatar Dec 18 '19 08:12 Romakita

http://tsed.io/tutorials/typeorm.html#entityrepository

chiqui3d avatar Jan 16 '20 19:01 chiqui3d

It’s now supported by using EntityRepository from tsed instead of typeorm

Romakita avatar Jan 17 '20 06:01 Romakita

This is totally doable. I just tried this and it works. However I have problems to Mock repository in my integration testing.

panbhatt avatar Jun 11 '20 17:06 panbhatt