feat: consolidate entities create

This was done to give better typings to the function signatures of entities `create` methods.
It also ensures that each field that should be set to `null` is set to `null` and doesn't leave that up to the typeorm handlers.

See: #1641
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-09-25 11:50:28 +02:00
parent bb7561b9ad
commit b896f954b9
14 changed files with 89 additions and 40 deletions

View file

@ -54,10 +54,11 @@ export class Alias {
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
static create(name: string, primary = false): Alias {
static create(name: string, note: Note, primary = false): Omit<Alias, 'id'> {
const alias = new Alias();
alias.name = name;
alias.primary = primary;
alias.note = note;
return alias;
}
}

View file

@ -27,40 +27,49 @@ import { generatePublicId } from './utils';
export class Note {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'text' })
publicId: string;
@OneToMany(
(_) => Alias,
(alias) => alias.note,
{ cascade: true }, // This ensures that embedded Aliases are automatically saved to the database
)
aliases: Alias[];
@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,
{ cascade: true }, // This ensures that embedded NoteUserPermission are automatically saved to the database
)
userPermissions: NoteUserPermission[];
@Column({
nullable: false,
default: 0,
})
viewCount: number;
@ManyToOne((_) => User, (user) => user.ownedNotes, {
onDelete: 'CASCADE', // This deletes the Note, when the associated User is deleted
nullable: true,
})
owner: User | null;
@OneToMany((_) => Revision, (revision) => revision.note, { cascade: true })
revisions: Promise<Revision[]>;
@OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user)
historyEntries: HistoryEntry[];
@OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.note)
mediaUploads: MediaUpload[];
@ -69,6 +78,7 @@ export class Note {
type: 'text',
})
description: string | null;
@Column({
nullable: true,
type: 'text',
@ -82,15 +92,19 @@ export class Note {
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static create(owner?: User, alias?: string): Note {
public static create(owner?: User, alias?: string): Omit<Note, 'id'> {
const newNote = new Note();
newNote.publicId = generatePublicId();
newNote.aliases = alias ? [Alias.create(alias, true)] : [];
newNote.viewCount = 0;
newNote.owner = owner ?? null;
newNote.aliases = alias
? [Alias.create(alias, newNote, true) as Alias]
: [];
newNote.userPermissions = [];
newNote.groupPermissions = [];
newNote.revisions = Promise.resolve([]) as Promise<Revision[]>;
newNote.viewCount = 0;
newNote.owner = owner ?? null;
newNote.revisions = Promise.resolve([]);
newNote.historyEntries = [];
newNote.mediaUploads = [];
newNote.description = null;
newNote.title = null;
newNote.tags = [];