From 979154b8a50aba9e2eaa4341504308bbc13996f7 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Wed, 17 Mar 2021 10:26:42 +0100 Subject: [PATCH] Permissions: Add cascade This makes it possible to create permissions by setting them in the note entity and delete them when either the user or note is deleted. Signed-off-by: Philip Molares --- src/notes/note.entity.ts | 7 ++++++- src/permissions/note-group-permission.entity.ts | 10 ++++++++-- src/permissions/note-user-permission.entity.ts | 10 ++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/notes/note.entity.ts b/src/notes/note.entity.ts index 1adcef9cb..538af568b 100644 --- a/src/notes/note.entity.ts +++ b/src/notes/note.entity.ts @@ -39,9 +39,14 @@ export class Note { @OneToMany( (_) => NoteGroupPermission, (groupPermission) => groupPermission.note, + { cascade: true }, // This ensures that embedded NoteGroupPermissions are automatically saved to the database ) groupPermissions: NoteGroupPermission[]; - @OneToMany((_) => NoteUserPermission, (userPermission) => userPermission.note) + @OneToMany( + (_) => NoteUserPermission, + (userPermission) => userPermission.note, + { cascade: true }, // This ensures that embedded NoteUserPermission are automatically saved to the database + ) userPermissions: NoteUserPermission[]; @Column({ nullable: false, diff --git a/src/permissions/note-group-permission.entity.ts b/src/permissions/note-group-permission.entity.ts index d1e94112c..364c2991d 100644 --- a/src/permissions/note-group-permission.entity.ts +++ b/src/permissions/note-group-permission.entity.ts @@ -10,10 +10,16 @@ import { Note } from '../notes/note.entity'; @Entity() export class NoteGroupPermission { - @ManyToOne((_) => Group, { primary: true }) + @ManyToOne((_) => Group, { + primary: true, + onDelete: 'CASCADE', // This deletes the NoteGroupPermission, when the associated Group is deleted + }) group: Group; - @ManyToOne((_) => Note, (note) => note.groupPermissions, { primary: true }) + @ManyToOne((_) => Note, (note) => note.groupPermissions, { + primary: true, + onDelete: 'CASCADE', // This deletes the NoteGroupPermission, when the associated Note is deleted + }) note: Note; @Column() diff --git a/src/permissions/note-user-permission.entity.ts b/src/permissions/note-user-permission.entity.ts index 0bad0d41d..06e94d0af 100644 --- a/src/permissions/note-user-permission.entity.ts +++ b/src/permissions/note-user-permission.entity.ts @@ -10,10 +10,16 @@ import { User } from '../users/user.entity'; @Entity() export class NoteUserPermission { - @ManyToOne((_) => User, { primary: true }) + @ManyToOne((_) => User, { + primary: true, + onDelete: 'CASCADE', // This deletes the NoteUserPermission, when the associated Note is deleted + }) user: User; - @ManyToOne((_) => Note, (note) => note.userPermissions, { primary: true }) + @ManyToOne((_) => Note, (note) => note.userPermissions, { + primary: true, + onDelete: 'CASCADE', // This deletes the NoteUserPermission, when the associated Note is deleted + }) note: Note; @Column()