From 39410ab9c88a442da893b22ce279b0252d7287a2 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 19 Sep 2020 16:00:29 +0200 Subject: [PATCH] NoteEntity: Move constructor-code to create() method TypeORM does not like having application code in the constructor (https://github.com/typeorm/typeorm/issues/1772#issuecomment-514787854), therefore that is moved into a new `create() static method. Additionally, the constructor is now `private`, which enforces the use of the new method. Signed-off-by: David Mehren --- src/notes/note.entity.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/notes/note.entity.ts b/src/notes/note.entity.ts index 670460797..70e828738 100644 --- a/src/notes/note.entity.ts +++ b/src/notes/note.entity.ts @@ -59,14 +59,18 @@ export class Note { ) authorColors: AuthorColor[]; - constructor(shortid: string, alias: string, owner: User) { - if (shortid) { - this.shortid = shortid; - } else { - // eslint-disable-next-line @typescript-eslint/no-unsafe-call - this.shortid = shortIdGenerate() as string; + // eslint-disable-next-line @typescript-eslint/no-empty-function + private constructor() {} + + public static create(owner?: User, alias?: string, shortid?: string) { + if (!shortid) { + shortid = shortIdGenerate(); } - this.alias = alias; - this.owner = owner; + const newNote = new Note(); + newNote.shortid = shortid; + newNote.alias = alias; + newNote.viewcount = 0; + newNote.owner = owner; + return newNote; } }