I want to create a service that receives an entity and creates a repository.
This is my attempt:
export class MyService<T extends ObjectLiteral> {
protected repo: Repository<T>;
constructor(entity: T) {
let token = getRepositoryToken(entity);
this.repo = InjectRepository(entity)(this, 'repo');
}
// methods
}
Usage:
let service = new MyService(MyEntity)
service.repo.findmany()
4
this is what i use:
import { BaseEntity } from 'typeorm';
export class MyEntity extends BaseEntity {
//your columns...
}
and your service when get a query
export class MyService<T extends ObjectLiteral> {
async getAll(){
const results = await MyEntity.find();
//...
}
}