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,
{ cascade: true },
)
revisions: Revision[];
revisions: Promise<Revision[]>;
@OneToMany(
_ => AuthorColor,
authorColor => authorColor.note,

View file

@ -60,9 +60,10 @@ export class NotesService {
alias?: NoteMetadataDto['alias'],
owner?: User,
): Promise<NoteDto> {
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<Revision> {
return this.revisionsService.getLatestRevision(note.id);
}
getMetadata(note: Note): NoteMetadataDto {
async getMetadata(note: Note): Promise<NoteMetadataDto> {
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',