import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; import { Files } from "@prisma/client"; import { File } from "le-coffre-resources/dist/SuperAdmin"; @Service() export default class FilesRepository extends BaseRepository { constructor(private database: Database) { super(); } protected get model() { return this.database.getClient().files; } protected get instanceDb() { return this.database.getClient(); } /** * @description : Find many files */ public async findMany(query: any): Promise { query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); return this.model.findMany(query); } /** * @description : Create a file linked to a document */ public async create(file: File): Promise { return this.model.create({ data: { document: { connect: { uid: file.document.uid, }, }, file_path: file.file_path, }, }); } /** * @description : Update data of a file */ public async update(uid: string, file: File): Promise { return this.model.update({ where: { uid: uid, }, data: { file_path: file.file_path, }, }); } /** * @description : Delete a file */ public async delete(uid: string): Promise { return this.model.delete({ where: { uid: uid, }, }); } /** * @description : Find unique file */ public async findOneByUid(uid: string): Promise { const fileEntity = await this.model.findUnique({ where: { uid: uid, }, }); if (!fileEntity) { throw new Error("File not found"); } return fileEntity; } }