ptitspas-ynov-back/src/common/base.service.ts

28 lines
832 B
TypeScript

import { DeepPartial, ObjectLiteral, Repository } from "typeorm";
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity.js";
export class BaseService<T extends ObjectLiteral> {
constructor(protected readonly repo: Repository<T>) { }
findAll(relations: string[] = []) {
return this.repo.find({ relations });
}
findOne(id: string, relations: string[] = []) {
return this.repo.findOne({ where: { id } as any, relations });
}
create(data: DeepPartial<T>) {
const entity = this.repo.create(data);
return this.repo.save(entity);
}
async update(id: string, data: QueryDeepPartialEntity<T>) {
await this.repo.update(id, data);
return this.findOne(id);
}
delete(id: string) {
return this.repo.delete(id);
}
}