refactor(edit): lazy-load relations

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-12-05 19:58:01 +01:00
parent 7f7886c5a7
commit 4e70044a2c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
5 changed files with 26 additions and 20 deletions

View file

@ -14,10 +14,14 @@ import {
Param, Param,
Post, Post,
UseGuards, UseGuards,
UseInterceptors UseInterceptors,
} from '@nestjs/common'; } from '@nestjs/common';
import { AlreadyInDBError, ForbiddenIdError, NotInDBError } from '../../../errors/errors'; import {
AlreadyInDBError,
ForbiddenIdError,
NotInDBError,
} from '../../../errors/errors';
import { HistoryService } from '../../../history/history.service'; import { HistoryService } from '../../../history/history.service';
import { SessionGuard } from '../../../identity/session.guard'; import { SessionGuard } from '../../../identity/session.guard';
import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../../logger/console-logger.service';

View file

@ -55,11 +55,11 @@ export class PermissionsGuard implements CanActivate {
const note = await getNote(this.noteService, noteIdOrAlias); const note = await getNote(this.noteService, noteIdOrAlias);
switch (permissions[0]) { switch (permissions[0]) {
case Permission.READ: case Permission.READ:
return this.permissionsService.mayRead(user, note); return await this.permissionsService.mayRead(user, note);
case Permission.WRITE: case Permission.WRITE:
return this.permissionsService.mayWrite(user, note); return await this.permissionsService.mayWrite(user, note);
case Permission.OWNER: case Permission.OWNER:
return this.permissionsService.isOwner(user, note); return await this.permissionsService.isOwner(user, note);
} }
return false; return false;
} }

View file

@ -713,18 +713,18 @@ describe('NotesService', () => {
const revisions = await note.revisions; const revisions = await note.revisions;
revisions[0].edits = [ revisions[0].edits = [
{ {
revisions: revisions, revisions: Promise.resolve(revisions),
startPos: 0, startPos: 0,
endPos: 1, endPos: 1,
updatedAt: new Date(1549312452000), updatedAt: new Date(1549312452000),
author: author, author: Promise.resolve(author),
} as Edit, } as Edit,
{ {
revisions: revisions, revisions: Promise.resolve(revisions),
startPos: 0, startPos: 0,
endPos: 1, endPos: 1,
updatedAt: new Date(1549312452001), updatedAt: new Date(1549312452001),
author: author, author: Promise.resolve(author),
} as Edit, } as Edit,
]; ];
revisions[0].createdAt = new Date(1549312452000); revisions[0].createdAt = new Date(1549312452000);
@ -812,18 +812,18 @@ describe('NotesService', () => {
const revisions = await note.revisions; const revisions = await note.revisions;
revisions[0].edits = [ revisions[0].edits = [
{ {
revisions: revisions, revisions: Promise.resolve(revisions),
startPos: 0, startPos: 0,
endPos: 1, endPos: 1,
updatedAt: new Date(1549312452000), updatedAt: new Date(1549312452000),
author: author, author: Promise.resolve(author),
} as Edit, } as Edit,
{ {
revisions: revisions, revisions: Promise.resolve(revisions),
startPos: 0, startPos: 0,
endPos: 1, endPos: 1,
updatedAt: new Date(1549312452001), updatedAt: new Date(1549312452001),
author: author, author: Promise.resolve(author),
} as Edit, } as Edit,
]; ];
revisions[0].createdAt = new Date(1549312452000); revisions[0].createdAt = new Date(1549312452000);

View file

@ -343,9 +343,11 @@ export class NotesService {
if (lastRevision && lastRevision.edits) { if (lastRevision && lastRevision.edits) {
// Sort the last Revisions Edits by their updatedAt Date to get the latest one // Sort the last Revisions Edits by their updatedAt Date to get the latest one
// the user of that Edit is the updateUser // the user of that Edit is the updateUser
return await lastRevision.edits.sort( return await (
(a, b) => b.updatedAt.getTime() - a.updatedAt.getTime(), await lastRevision.edits.sort(
)[0].author.user; (a, b) => b.updatedAt.getTime() - a.updatedAt.getTime(),
)[0].author
).user;
} }
// If there are no Edits, the owner is the updateUser // If there are no Edits, the owner is the updateUser
return await note.owner; return await note.owner;

View file

@ -28,13 +28,13 @@ export class Edit {
* Revisions this edit appears in * Revisions this edit appears in
*/ */
@ManyToMany((_) => Revision, (revision) => revision.edits) @ManyToMany((_) => Revision, (revision) => revision.edits)
revisions: Revision[]; revisions: Promise<Revision[]>;
/** /**
* Author that created the change * Author that created the change
*/ */
@ManyToOne(() => Author, (author) => author.edits) @ManyToOne(() => Author, (author) => author.edits)
author: Author; author: Promise<Author>;
@Column() @Column()
startPos: number; startPos: number;
@ -57,8 +57,8 @@ export class Edit {
endPos: number, endPos: number,
): Omit<Edit, 'id' | 'createdAt' | 'updatedAt'> { ): Omit<Edit, 'id' | 'createdAt' | 'updatedAt'> {
const newEdit = new Edit(); const newEdit = new Edit();
newEdit.revisions = []; newEdit.revisions = Promise.resolve([]);
newEdit.author = author; newEdit.author = Promise.resolve(author);
newEdit.startPos = startPos; newEdit.startPos = startPos;
newEdit.endPos = endPos; newEdit.endPos = endPos;
return newEdit; return newEdit;