41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { FindOneOptions, Repository, SelectQueryBuilder } from 'typeorm';
|
|
import { BaseFilterEntity } from '../../domain/entities/base-filter.entity';
|
|
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
|
|
|
|
export abstract class BaseReadService<Entity> {
|
|
constructor(private repository: Repository<Entity>) {}
|
|
|
|
getRepository(): Repository<Entity> {
|
|
return this.repository;
|
|
}
|
|
|
|
async getIndex(
|
|
queryBuilder: SelectQueryBuilder<Entity>,
|
|
params: BaseFilterEntity,
|
|
): Promise<PaginationResponse<Entity>> {
|
|
const [data, total] = await queryBuilder
|
|
.take(+params.limit)
|
|
.skip(+params.limit * +params.page - +params.limit)
|
|
.getManyAndCount();
|
|
|
|
return {
|
|
data,
|
|
total,
|
|
};
|
|
}
|
|
|
|
async getOneByOptions(findOneOptions): Promise<Entity> {
|
|
return await this.repository.findOne(findOneOptions);
|
|
}
|
|
|
|
async getOneOrFailByOptions(
|
|
findOneOptions: FindOneOptions<Entity>,
|
|
): Promise<Entity> {
|
|
return await this.repository.findOneOrFail(findOneOptions);
|
|
}
|
|
|
|
async getManyByOptions(findManyOptions): Promise<Entity[]> {
|
|
return await this.repository.find(findManyOptions);
|
|
}
|
|
}
|