Entities: Add onDelete CASCADE to entities

To better handle deletion of entities, all necessary other entities got the option onDelete CASCADE set. So everything that does not make any sense if something else is deleted will be deleted along side of it.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-03-11 15:03:23 +01:00 committed by David Mehren
parent 56a46f57fc
commit e90e2a8e19
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
6 changed files with 22 additions and 6 deletions

View file

@ -21,7 +21,9 @@ export class AuthToken {
@Column({ unique: true }) @Column({ unique: true })
keyId: string; keyId: string;
@ManyToOne((_) => User, (user) => user.authTokens) @ManyToOne((_) => User, (user) => user.authTokens, {
onDelete: 'CASCADE', // This deletes the AuthToken, when the associated User is deleted
})
user: User; user: User;
@Column() @Column()

View file

@ -36,7 +36,6 @@ export class Group {
@ManyToMany((_) => User, (user) => user.groups, { @ManyToMany((_) => User, (user) => user.groups, {
eager: true, eager: true,
cascade: true,
}) })
@JoinTable() @JoinTable()
members: User[]; members: User[];

View file

@ -23,10 +23,14 @@ export class MediaUpload {
@PrimaryColumn() @PrimaryColumn()
id: string; id: string;
@ManyToOne((_) => Note, { nullable: false }) @ManyToOne((_) => Note, (note) => note.mediaUploads, {
nullable: false,
})
note: Note; note: Note;
@ManyToOne((_) => User, { nullable: false }) @ManyToOne((_) => User, (user) => user.mediaUploads, {
nullable: false,
})
user: User; user: User;
@Column({ @Column({

View file

@ -21,6 +21,7 @@ import { User } from '../users/user.entity';
import { AuthorColor } from './author-color.entity'; import { AuthorColor } from './author-color.entity';
import { Tag } from './tag.entity'; import { Tag } from './tag.entity';
import { HistoryEntry } from '../history/history-entry.entity'; import { HistoryEntry } from '../history/history-entry.entity';
import { MediaUpload } from '../media/media-upload.entity';
@Entity() @Entity()
export class Note { export class Note {
@ -53,7 +54,9 @@ export class Note {
default: 0, default: 0,
}) })
viewCount: number; viewCount: number;
@ManyToOne((_) => User, (user) => user.ownedNotes, { onDelete: 'CASCADE' }) @ManyToOne((_) => User, (user) => user.ownedNotes, {
onDelete: 'CASCADE', // This deletes the Note, when the associated User is deleted
})
owner: User; owner: User;
@OneToMany((_) => Revision, (revision) => revision.note, { cascade: true }) @OneToMany((_) => Revision, (revision) => revision.note, { cascade: true })
revisions: Promise<Revision[]>; revisions: Promise<Revision[]>;
@ -61,6 +64,8 @@ export class Note {
authorColors: AuthorColor[]; authorColors: AuthorColor[];
@OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user) @OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user)
historyEntries: HistoryEntry[]; historyEntries: HistoryEntry[];
@OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.note)
mediaUploads: MediaUpload[];
@Column({ @Column({
nullable: true, nullable: true,

View file

@ -19,7 +19,9 @@ export class Identity {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ManyToOne((_) => User, (user) => user.identities) @ManyToOne((_) => User, (user) => user.identities, {
onDelete: 'CASCADE', // This deletes the Identity, when the associated User is deleted
})
user: User; user: User;
@Column() @Column()

View file

@ -17,6 +17,7 @@ import { AuthToken } from '../auth/auth-token.entity';
import { Identity } from './identity.entity'; import { Identity } from './identity.entity';
import { Group } from '../groups/group.entity'; import { Group } from '../groups/group.entity';
import { HistoryEntry } from '../history/history-entry.entity'; import { HistoryEntry } from '../history/history-entry.entity';
import { MediaUpload } from '../media/media-upload.entity';
@Entity() @Entity()
export class User { export class User {
@ -62,6 +63,9 @@ export class User {
@OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user) @OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user)
historyEntries: HistoryEntry[]; historyEntries: HistoryEntry[];
@OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.user)
mediaUploads: MediaUpload[];
// eslint-disable-next-line @typescript-eslint/no-empty-function // eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {} private constructor() {}