refactor(note): lazy-load relations

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-11-30 16:46:07 +01:00
parent d761ff7f4f
commit 235e4f647c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
23 changed files with 343 additions and 284 deletions

View file

@ -159,9 +159,9 @@ describe('HistoryService', () => {
Note.create(user, alias) as Note,
user,
);
expect(createHistoryEntry.note.aliases).toHaveLength(1);
expect(createHistoryEntry.note.aliases[0].name).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(await createHistoryEntry.note.aliases).toHaveLength(1);
expect((await createHistoryEntry.note.aliases)[0].name).toEqual(alias);
expect(await createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(false);
});
@ -177,9 +177,9 @@ describe('HistoryService', () => {
Note.create(user, alias) as Note,
user,
);
expect(createHistoryEntry.note.aliases).toHaveLength(1);
expect(createHistoryEntry.note.aliases[0].name).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user);
expect(await createHistoryEntry.note.aliases).toHaveLength(1);
expect((await createHistoryEntry.note.aliases)[0].name).toEqual(alias);
expect(await createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(false);
expect(createHistoryEntry.updatedAt.getTime()).toBeGreaterThanOrEqual(
@ -223,9 +223,9 @@ describe('HistoryService', () => {
pinStatus: true,
},
);
expect(updatedHistoryEntry.note.aliases).toHaveLength(1);
expect(updatedHistoryEntry.note.aliases[0].name).toEqual(alias);
expect(updatedHistoryEntry.note.owner).toEqual(user);
expect(await updatedHistoryEntry.note.aliases).toHaveLength(1);
expect((await updatedHistoryEntry.note.aliases)[0].name).toEqual(alias);
expect(await updatedHistoryEntry.note.owner).toEqual(user);
expect(updatedHistoryEntry.user).toEqual(user);
expect(updatedHistoryEntry.pinStatus).toEqual(true);
});
@ -371,11 +371,13 @@ describe('HistoryService', () => {
const mockedManager = {
find: jest.fn().mockResolvedValueOnce([historyEntry]),
createQueryBuilder: () => createQueryBuilder,
remove: jest.fn().mockImplementationOnce((entry: HistoryEntry) => {
expect(entry.note.aliases).toHaveLength(1);
expect(entry.note.aliases[0].name).toEqual(alias);
expect(entry.pinStatus).toEqual(false);
}),
remove: jest
.fn()
.mockImplementationOnce(async (entry: HistoryEntry) => {
expect(await entry.note.aliases).toHaveLength(1);
expect((await entry.note.aliases)[0].name).toEqual(alias);
expect(entry.pinStatus).toEqual(false);
}),
save: jest.fn().mockImplementationOnce((entry: HistoryEntry) => {
expect(entry.note.aliases).toEqual(
newlyCreatedHistoryEntry.note.aliases,
@ -402,11 +404,13 @@ describe('HistoryService', () => {
const tags = ['tag1', 'tag2'];
const note = Note.create(user, alias) as Note;
note.title = title;
note.tags = tags.map((tag) => {
const newTag = new Tag();
newTag.name = tag;
return newTag;
});
note.tags = Promise.resolve(
tags.map((tag) => {
const newTag = new Tag();
newTag.name = tag;
return newTag;
}),
);
const historyEntry = HistoryEntry.create(user, note) as HistoryEntry;
historyEntry.pinStatus = true;
const createQueryBuilder = {
@ -420,7 +424,7 @@ describe('HistoryService', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementation(() => createQueryBuilder);
const historyEntryDto = service.toHistoryEntryDto(historyEntry);
const historyEntryDto = await service.toHistoryEntryDto(historyEntry);
expect(historyEntryDto.pinStatus).toEqual(true);
expect(historyEntryDto.identifier).toEqual(alias);
expect(historyEntryDto.tags).toEqual(tags);

View file

@ -186,11 +186,11 @@ export class HistoryService {
* @param {HistoryEntry} entry - the history entry to use
* @return {HistoryEntryDto} the built HistoryEntryDto
*/
toHistoryEntryDto(entry: HistoryEntry): HistoryEntryDto {
async toHistoryEntryDto(entry: HistoryEntry): Promise<HistoryEntryDto> {
return {
identifier: getIdentifier(entry),
identifier: await getIdentifier(entry),
lastVisited: entry.updatedAt,
tags: this.notesService.toTagList(entry.note),
tags: await this.notesService.toTagList(entry.note),
title: entry.note.title ?? '',
pinStatus: entry.pinStatus,
};

View file

@ -18,19 +18,19 @@ describe('getIdentifier', () => {
note = Note.create(user, alias) as Note;
entry = HistoryEntry.create(user, note) as HistoryEntry;
});
it('returns the publicId if there are no aliases', () => {
note.aliases = undefined as unknown as Alias[];
expect(getIdentifier(entry)).toEqual(note.publicId);
it('returns the publicId if there are no aliases', async () => {
note.aliases = Promise.resolve(undefined as unknown as Alias[]);
expect(await getIdentifier(entry)).toEqual(note.publicId);
});
it('returns the publicId, if the alias array is empty', () => {
note.aliases = [];
expect(getIdentifier(entry)).toEqual(note.publicId);
it('returns the publicId, if the alias array is empty', async () => {
note.aliases = Promise.resolve([]);
expect(await getIdentifier(entry)).toEqual(note.publicId);
});
it('returns the publicId, if the only alias is not primary', () => {
note.aliases[0].primary = false;
expect(getIdentifier(entry)).toEqual(note.publicId);
it('returns the publicId, if the only alias is not primary', async () => {
(await note.aliases)[0].primary = false;
expect(await getIdentifier(entry)).toEqual(note.publicId);
});
it('returns the primary alias, if one exists', () => {
expect(getIdentifier(entry)).toEqual(note.aliases[0].name);
it('returns the primary alias, if one exists', async () => {
expect(await getIdentifier(entry)).toEqual((await note.aliases)[0].name);
});
});

View file

@ -6,11 +6,12 @@
import { getPrimaryAlias } from '../notes/utils';
import { HistoryEntry } from './history-entry.entity';
export function getIdentifier(entry: HistoryEntry): string {
if (!entry.note.aliases || entry.note.aliases.length === 0) {
export async function getIdentifier(entry: HistoryEntry): Promise<string> {
const aliases = await entry.note.aliases;
if (!aliases || aliases.length === 0) {
return entry.note.publicId;
}
const primaryAlias = getPrimaryAlias(entry.note);
const primaryAlias = await getPrimaryAlias(entry.note);
if (primaryAlias === undefined) {
return entry.note.publicId;
}