import CheckConstraint from "@entity-access/entity-access/dist/decorators/CheckConstraint.js"; import Column from "@entity-access/entity-access/dist/decorators/Column.js"; import { RelateTo } from "@entity-access/entity-access/dist/decorators/Relate.js"; import Table from "@entity-access/entity-access/dist/decorators/Table.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import FileContent from "./FileContent.js"; import Index from "@entity-access/entity-access/dist/decorators/Index.js"; /** * The reason this class exists is, we cannot rely on Disk Cache as, * creating FileContent block is an expensive operation. * * This class provides a temporary folder for creating large operations * in a relatively smaller time as entire folder can be swapped easily. * * 1. These files will be deleted automatically within 24 hours without * polluting AppFile's primary key. * 2. These files will hold FileContent class and it will prevent it from * deletion. * 3. As opposed to AppFile, path will hold a complete named path. So there * is a size restriction. * 4. The reason root exists here as we are not going to check for any security. * In AppFile, person can have access to a folder and not root. */ @Table("AppTempFiles") @CheckConstraint({ name: "CC_AppTempFile_Root", filter: (x) => !Sql.text.includes(x.name, "/") && ((x.rootID === null && x.path === "/") || (x.rootID !== null)) }) @Index({ name: "UX_AppTempFile_Path", unique: true, columns: [ { name: (x) => x.rootID, descending: false }, { name: (x) => x.path , descending: false } ], filter: (x) => x.rootID !== null }) export default class AppTempFile { @Column({ dataType: "BigInt", key: true, generated: "identity"}) tempFileID: number; @Column({ dataType: "Char", length: 900, default: () => "/"}) path: string; @Column({ dataType: "Char", length: 200 }) name: string; @Column({ dataType: "Char", length: 200 }) contentType: string; @Column( {dataType: "Boolean", default: () => false }) isFolder: boolean; @Column({ dataType: "BigInt", default: () => 0}) public fileSize: number; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(AppTempFile, { property: (x) => x.root, inverseProperty: (x) => x.descendants, foreignKeyConstraint: { name: "FC_AppTempFile_rootID", onDelete: "restrict" } }) rootID: number; /** For folder, fileContentID will be null */ @Column({ dataType: "BigInt", nullable: true }) @RelateTo(FileContent, { property: (x) => x.fileContent, inverseProperty: (x) => x.tempFiles }) fileContentID: number; @Column({ dataType: "DateTime", default: () => Sql.date.now()}) dateCreated: DateTime; root: AppTempFile; descendants: AppTempFile[]; fileContent: FileContent; }