我实现了以下通用类,这可能会引起问题,
import { Logger } from '@nestjs/common';
import { PaginationOptionsInterface, Pagination } from './paginate';
import { Repository } from 'typeorm';
export class EntityService<T> {
private repository: Repository<T>;
constructor(repository) {
this.repository = repository;
}
async getEntityWithPagination(
options: PaginationOptionsInterface,
): Promise<Pagination<T>> {
const [results, total] = await this.repository.findAndCount({
take: options.limit,
skip: (options.page - 1) * options.limit,
});
return new Pagination<T>({ results, total });
}
}
Run Code Online (Sandbox Code Playgroud)
并与其他实体服务一起使用,例如
@Injectable()
export class CarService extends EntityService<CarEntity> {
constructor(
@InjectRepository(CarEntity)
private carRepository: Repository<CarEntity>,
) {
super(carRepository);
} …Run Code Online (Sandbox Code Playgroud)