refactor(user): lazy-load relations

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-12-05 22:03:41 +01:00
parent 4483d2b898
commit 977ed4b9fa
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
4 changed files with 17 additions and 17 deletions

View file

@ -166,7 +166,7 @@ describe('AuthService', () => {
const accessTokenHash = await hashPassword(token); const accessTokenHash = await hashPassword(token);
jest.spyOn(userRepo, 'findOne').mockResolvedValueOnce({ jest.spyOn(userRepo, 'findOne').mockResolvedValueOnce({
...user, ...user,
authTokens: [authToken], authTokens: Promise.resolve([authToken]),
}); });
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({ jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({
...authToken, ...authToken,
@ -183,7 +183,7 @@ describe('AuthService', () => {
); );
expect(userByToken).toEqual({ expect(userByToken).toEqual({
...user, ...user,
authTokens: [authToken], authTokens: Promise.resolve([authToken]),
}); });
}); });
describe('fails:', () => { describe('fails:', () => {

View file

@ -65,9 +65,9 @@ export class AuthService {
identifier: string, identifier: string,
validUntil: TimestampMillis, validUntil: TimestampMillis,
): Promise<AuthTokenWithSecretDto> { ): Promise<AuthTokenWithSecretDto> {
user.authTokens = await this.getTokensByUser(user); user.authTokens = this.getTokensByUser(user);
if (user.authTokens.length >= 200) { if ((await user.authTokens).length >= 200) {
// This is a very high ceiling unlikely to hinder legitimate usage, // This is a very high ceiling unlikely to hinder legitimate usage,
// but should prevent possible attack vectors // but should prevent possible attack vectors
throw new TooManyTokensError( throw new TooManyTokensError(

View file

@ -78,7 +78,7 @@ createConnection({
notes[i].revisions = Promise.all([revision]); notes[i].revisions = Promise.all([revision]);
notes[i].userPermissions = Promise.resolve([]); notes[i].userPermissions = Promise.resolve([]);
notes[i].groupPermissions = Promise.resolve([]); notes[i].groupPermissions = Promise.resolve([]);
user.ownedNotes = [notes[i]]; user.ownedNotes = Promise.resolve([notes[i]]);
await connection.manager.save([ await connection.manager.save([
notes[i], notes[i],
user, user,

View file

@ -53,25 +53,25 @@ export class User {
email: string | null; email: string | null;
@OneToMany((_) => Note, (note) => note.owner) @OneToMany((_) => Note, (note) => note.owner)
ownedNotes: Note[]; ownedNotes: Promise<Note[]>;
@OneToMany((_) => AuthToken, (authToken) => authToken.user) @OneToMany((_) => AuthToken, (authToken) => authToken.user)
authTokens: AuthToken[]; authTokens: Promise<AuthToken[]>;
@OneToMany((_) => Identity, (identity) => identity.user) @OneToMany((_) => Identity, (identity) => identity.user)
identities: Promise<Identity[]>; identities: Promise<Identity[]>;
@ManyToMany((_) => Group, (group) => group.members) @ManyToMany((_) => Group, (group) => group.members)
groups: Group[]; groups: Promise<Group[]>;
@OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user) @OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user)
historyEntries: HistoryEntry[]; historyEntries: Promise<HistoryEntry[]>;
@OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.user) @OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.user)
mediaUploads: MediaUpload[]; mediaUploads: Promise<MediaUpload[]>;
@OneToMany(() => Author, (author) => author.user) @OneToMany(() => Author, (author) => author.user)
authors: Author[]; authors: Promise<Author[]>;
// eslint-disable-next-line @typescript-eslint/no-empty-function // eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {} private constructor() {}
@ -85,13 +85,13 @@ export class User {
newUser.displayName = displayName; newUser.displayName = displayName;
newUser.photo = null; newUser.photo = null;
newUser.email = null; newUser.email = null;
newUser.ownedNotes = []; newUser.ownedNotes = Promise.resolve([]);
newUser.authTokens = []; newUser.authTokens = Promise.resolve([]);
newUser.identities = Promise.resolve([]); newUser.identities = Promise.resolve([]);
newUser.groups = []; newUser.groups = Promise.resolve([]);
newUser.historyEntries = []; newUser.historyEntries = Promise.resolve([]);
newUser.mediaUploads = []; newUser.mediaUploads = Promise.resolve([]);
newUser.authors = []; newUser.authors = Promise.resolve([]);
return newUser; return newUser;
} }
} }