From e97f9fe17438acd73433e139618e064a45231296 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Tue, 22 Sep 2020 17:32:35 +0200 Subject: [PATCH] NoteEntity: Lazy-load `revisions` relation Using a `Promise` type in a TypeORM entity automatically enables lazy-loading of that relation. See https://typeorm.io/#/eager-and-lazy-relations/lazy-relations Signed-off-by: David Mehren --- src/notes/note.entity.ts | 2 +- src/notes/notes.service.ts | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/notes/note.entity.ts b/src/notes/note.entity.ts index b89539e17..815e7c643 100644 --- a/src/notes/note.entity.ts +++ b/src/notes/note.entity.ts @@ -52,7 +52,7 @@ export class Note { revision => revision.note, { cascade: true }, ) - revisions: Revision[]; + revisions: Promise; @OneToMany( _ => AuthorColor, authorColor => authorColor.note, diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index ab283137c..f3d08f836 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -60,9 +60,10 @@ export class NotesService { alias?: NoteMetadataDto['alias'], owner?: User, ): Promise { - this.logger.warn('Using hardcoded data!'); const newNote = Note.create(); - newNote.revisions = [Revision.create(noteContent, noteContent)]; + newNote.revisions = Promise.resolve([ + Revision.create(noteContent, noteContent), + ]); if (alias) { newNote.alias = alias; } @@ -71,21 +72,21 @@ export class NotesService { } const savedNote = await this.noteRepository.save(newNote); return { - content: this.getCurrentContent(savedNote), - metadata: this.getMetadata(savedNote), + content: await this.getCurrentContent(savedNote), + metadata: await this.getMetadata(savedNote), editedByAtPosition: [], }; } - getCurrentContent(note: Note) { - return this.getLastRevision(note).content; + async getCurrentContent(note: Note) { + return (await this.getLastRevision(note)).content; } - getLastRevision(note: Note) { - return note.revisions[note.revisions.length - 1]; + async getLastRevision(note: Note): Promise { + return this.revisionsService.getLatestRevision(note.id); } - getMetadata(note: Note): NoteMetadataDto { + async getMetadata(note: Note): Promise { return { // TODO: Convert DB UUID to base64 id: note.id, @@ -108,7 +109,7 @@ export class NotesService { })), }, tags: NoteUtils.parseTags(note), - updateTime: this.getLastRevision(note).createdAt, + updateTime: (await this.getLastRevision(note)).createdAt, // TODO: Get actual updateUser updateUser: { displayName: 'Hardcoded User',