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 16c9d6c011
commit 3539216cf3
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
4 changed files with 17 additions and 17 deletions

View file

@ -174,7 +174,7 @@ describe('AuthService', () => {
.digest('hex');
jest.spyOn(userRepo, 'findOne').mockResolvedValueOnce({
...user,
authTokens: [authToken],
authTokens: Promise.resolve([authToken]),
});
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({
...authToken,
@ -191,7 +191,7 @@ describe('AuthService', () => {
);
expect(userByToken).toEqual({
...user,
authTokens: [authToken],
authTokens: Promise.resolve([authToken]),
});
});
describe('fails:', () => {

View file

@ -58,9 +58,9 @@ export class AuthService {
identifier: string,
validUntil: TimestampMillis,
): 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,
// but should prevent possible attack vectors
throw new TooManyTokensError(

View file

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

View file

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