aboutsummaryrefslogtreecommitdiffstats
path: root/src/file/entity/file.entity.ts
blob: 49c0eccc762cc2a548d10276c6ebbc375210ec9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { nanoid } from 'nanoid';
import { Session } from '../../session/entity/session.entity';
import {
  Entity,
  Column,
  BeforeInsert,
  CreateDateColumn,
  PrimaryGeneratedColumn,
  ManyToOne,
  Index,
  OneToOne,
} from 'typeorm';
import { FilePurpose } from './purpose.entity';
import { StoredFile } from 'src/storage/entity/stored-file.entity';

@Entity()
export class FileMetadata {
  @PrimaryGeneratedColumn()
  id: number;

  @Index('filekey-idx')
  @Column({ unique: true, nullable: false })
  key: string;

  @ManyToOne(() => FilePurpose, { nullable: false })
  purpose: FilePurpose;

  @CreateDateColumn()
  creationDate: Date;

  @Column()
  validUntil: Date;

  @Column({ default: false })
  used: boolean;

  @ManyToOne(() => Session, (session) => session.files, { nullable: false })
  session: Session;

  @OneToOne(() => StoredFile, (file) => file.fileMetadata, { nullable: true })
  contents: StoredFile;

  @BeforeInsert()
  private beforeInsert() {
    this.key = nanoid();
  }
}