import { NotFoundException } from '@nestjs/common' import { ObjectLiteral, Repository } from 'typeorm' import { PagerDto } from '~/common/dto/pager.dto' import { paginate } from '../paginate' import { Pagination } from '../paginate/pagination' export class BaseService = Repository> { constructor(private repository: R) { } async list({ page, pageSize, }: PagerDto): Promise> { return paginate(this.repository, { page, pageSize }) } async findOne(id: number): Promise { const item = await this.repository.createQueryBuilder().where({ id }).getOne() if (!item) throw new NotFoundException('未找到该记录') return item } async create(dto: any): Promise { return await this.repository.save(dto) } async update(id: number, dto: any): Promise { await this.repository.update(id, dto) } async delete(id: number): Promise { const item = await this.findOne(id) await this.repository.remove(item) } }