Open jasonlimantoro opened 3 years ago
I am not actively maintaining this lib so won't implement this, but if someone comes up with a solution we can give it a go.
For other readers: if you don't need a custom repository then you can just use getRepository()
from TypeORM, eg:
import { getRepository } from 'typeorm';
import { Service } from 'typedi';
import { User } from 'your-code';
@Service()
export class UserResolver {
private readonly userRepository = getRepository(User);
}
can someone tell me is this looks good or not?. it works.
// base.repository.ts
import { EntityTarget, Repository as Repo } from 'typeorm';
import { dataSource } from '..';
export const Repository = <Entity>(target: EntityTarget<Entity>) => {
return class extends Repo<Entity> {
constructor() {
super(target, dataSource.createEntityManager());
}
};
};
// user.repository.ts
import { Service } from 'typedi';
import { User } from '../entities';
import { Repository } from './base.repository';
@Service()
export class UserRepository extends Repository(User) {
findByEmailOrFail(email: string) {
return this.findOneByOrFail({ email });
}
}
Apart from this i did't have to do anything. and i am not using this package.
Function
getRepository
from src/decorators/InjectRepository.ts seems to be problematic. I dealt with it like this:if you inject repository as:
then this should work:
Originally posted by @kajkal in https://github.com/typeorm/typeorm-typedi-extensions/issues/33#issuecomment-623199794
A better way of mocking is clearly needed. It seems that the selected solution above does not fully solve the issue since you need to also mock the entire
ConnectionManager
. In other words, with the above solution only, anygetConnection()
calls would fail as it is not mocked (not to mention any other methods).Better way means, you only mock what you need to mock. Pretty much like
would be ideal.