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 <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-09-22 17:32:35 +02:00
parent 74b03fc1fd
commit e97f9fe174
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 12 additions and 11 deletions

View file

@ -52,7 +52,7 @@ export class Note {
revision => revision.note, revision => revision.note,
{ cascade: true }, { cascade: true },
) )
revisions: Revision[]; revisions: Promise<Revision[]>;
@OneToMany( @OneToMany(
_ => AuthorColor, _ => AuthorColor,
authorColor => authorColor.note, authorColor => authorColor.note,

View file

@ -60,9 +60,10 @@ export class NotesService {
alias?: NoteMetadataDto['alias'], alias?: NoteMetadataDto['alias'],
owner?: User, owner?: User,
): Promise<NoteDto> { ): Promise<NoteDto> {
this.logger.warn('Using hardcoded data!');
const newNote = Note.create(); const newNote = Note.create();
newNote.revisions = [Revision.create(noteContent, noteContent)]; newNote.revisions = Promise.resolve([
Revision.create(noteContent, noteContent),
]);
if (alias) { if (alias) {
newNote.alias = alias; newNote.alias = alias;
} }
@ -71,21 +72,21 @@ export class NotesService {
} }
const savedNote = await this.noteRepository.save(newNote); const savedNote = await this.noteRepository.save(newNote);
return { return {
content: this.getCurrentContent(savedNote), content: await this.getCurrentContent(savedNote),
metadata: this.getMetadata(savedNote), metadata: await this.getMetadata(savedNote),
editedByAtPosition: [], editedByAtPosition: [],
}; };
} }
getCurrentContent(note: Note) { async getCurrentContent(note: Note) {
return this.getLastRevision(note).content; return (await this.getLastRevision(note)).content;
} }
getLastRevision(note: Note) { async getLastRevision(note: Note): Promise<Revision> {
return note.revisions[note.revisions.length - 1]; return this.revisionsService.getLatestRevision(note.id);
} }
getMetadata(note: Note): NoteMetadataDto { async getMetadata(note: Note): Promise<NoteMetadataDto> {
return { return {
// TODO: Convert DB UUID to base64 // TODO: Convert DB UUID to base64
id: note.id, id: note.id,
@ -108,7 +109,7 @@ export class NotesService {
})), })),
}, },
tags: NoteUtils.parseTags(note), tags: NoteUtils.parseTags(note),
updateTime: this.getLastRevision(note).createdAt, updateTime: (await this.getLastRevision(note)).createdAt,
// TODO: Get actual updateUser // TODO: Get actual updateUser
updateUser: { updateUser: {
displayName: 'Hardcoded User', displayName: 'Hardcoded User',