From 06c39ea7590fce540afd43508be8faafbc75bb71 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 17 Apr 2022 22:00:04 +0200 Subject: [PATCH] refactor: adapt for typeorm 0.3 Signed-off-by: David Mehren --- src/history/history.service.spec.ts | 14 +-- src/history/history.service.ts | 1 + src/identity/identity.service.ts | 2 +- src/notes/notes.service.spec.ts | 4 + src/seed.ts | 144 +++++++++++++++------------- 5 files changed, 89 insertions(+), 76 deletions(-) diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index 9169e3866..cdcf68004 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -5,8 +5,8 @@ */ import { ConfigModule } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; -import { getConnectionToken, getRepositoryToken } from '@nestjs/typeorm'; -import { Connection, Repository } from 'typeorm'; +import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm'; +import { DataSource, Repository } from 'typeorm'; import { AuthToken } from '../auth/auth-token.entity'; import { Author } from '../authors/author.entity'; @@ -34,7 +34,7 @@ import { HistoryService } from './history.service'; describe('HistoryService', () => { let service: HistoryService; let historyRepo: Repository; - let connection; + let dataSource: DataSource; let noteRepo: Repository; type MockConnection = { @@ -52,7 +52,7 @@ describe('HistoryService', () => { providers: [ HistoryService, { - provide: getConnectionToken(), + provide: getDataSourceToken(), useFactory: mockConnection, }, { @@ -106,7 +106,7 @@ describe('HistoryService', () => { historyRepo = module.get>( getRepositoryToken(HistoryEntry), ); - connection = module.get(Connection); + dataSource = module.get(DataSource); noteRepo = module.get>(getRepositoryToken(Note)); }); @@ -467,8 +467,10 @@ describe('HistoryService', () => { expect(entry.updatedAt).toEqual(newlyCreatedHistoryEntry.updatedAt); }), }; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore // eslint-disable-next-line @typescript-eslint/no-unsafe-call - connection.transaction.mockImplementation((cb) => { + dataSource.transaction.mockImplementation((cb) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-call cb(mockedManager); }); diff --git a/src/history/history.service.ts b/src/history/history.service.ts index 55b965c09..aee6ad83a 100644 --- a/src/history/history.service.ts +++ b/src/history/history.service.ts @@ -59,6 +59,7 @@ export class HistoryService { .where('entry.note = :note', { note: note.id }) .andWhere('entry.user = :user', { user: user.id }) .leftJoinAndSelect('entry.note', 'note') + .leftJoinAndSelect('entry.user', 'user') .getOne(); if (!entry) { throw new NotInDBError( diff --git a/src/identity/identity.service.ts b/src/identity/identity.service.ts index 8e4faa7b6..647f92590 100644 --- a/src/identity/identity.service.ts +++ b/src/identity/identity.service.ts @@ -49,7 +49,7 @@ export class IdentityService { }, relations: ['user'], }); - if (identity === undefined) { + if (identity === null) { throw new NotInDBError(`Identity for user id '${userId}' not found`); } return identity; diff --git a/src/notes/notes.service.spec.ts b/src/notes/notes.service.spec.ts index f99e458a9..833c97b23 100644 --- a/src/notes/notes.service.spec.ts +++ b/src/notes/notes.service.spec.ts @@ -91,14 +91,18 @@ describe('NotesService', () => { note.owner = Promise.resolve(user); note.userPermissions = Promise.resolve([ { + noteId: note.id, note: note, + userId: user.id, user: user, canEdit: true, }, ]); note.groupPermissions = Promise.resolve([ { + noteId: note.id, note: note, + groupId: group.id, group: group, canEdit: true, }, diff --git a/src/seed.ts b/src/seed.ts index acb9a7ae7..64ece419e 100644 --- a/src/seed.ts +++ b/src/seed.ts @@ -51,79 +51,85 @@ const dataSource = new DataSource({ dropSchema: true, }); -dataSource.initialize().then(async () => { - const password = 'test_password'; - const users = []; - users.push(User.create('hardcoded', 'Test User 1')); - users.push(User.create('hardcoded_2', 'Test User 2')); - users.push(User.create('hardcoded_3', 'Test User 3')); - const notes: Note[] = []; - notes.push(Note.create(null, 'test') as Note); - notes.push(Note.create(null, 'test2') as Note); - notes.push(Note.create(null, 'test3') as Note); +dataSource + .initialize() + .then(async () => { + const password = 'test_password'; + const users = []; + users.push(User.create('hardcoded', 'Test User 1')); + users.push(User.create('hardcoded_2', 'Test User 2')); + users.push(User.create('hardcoded_3', 'Test User 3')); + const notes: Note[] = []; + notes.push(Note.create(null, 'test') as Note); + notes.push(Note.create(null, 'test2') as Note); + notes.push(Note.create(null, 'test3') as Note); - for (let i = 0; i < 3; i++) { - const author = await dataSource.manager.save(dataSource.manager.create(Author, Author.create(1))) as Author; - const user = await dataSource.manager.save(dataSource.manager.create(User, users[i])) as User; - const identity = Identity.create(user, ProviderType.LOCAL, false); - identity.passwordHash = await hashPassword(password); - dataSource.manager.create(Identity, identity); - author.user = (dataSource.manager.save(user) as Promise); - const revision = Revision.create( - 'This is a test note', - 'This is a test note', - notes[i], - ) as Revision; - const edit = Edit.create(author, 1, 42) as Edit; - revision.edits = Promise.resolve([edit]); - notes[i].revisions = Promise.all([revision]); - notes[i].userPermissions = Promise.resolve([]); - notes[i].groupPermissions = Promise.resolve([]); - user.ownedNotes = Promise.resolve([notes[i]]); - await dataSource.manager.save([ - notes[i], - user, - revision, - edit, - author, - identity, - ]); - } - const foundUsers = await dataSource.manager.find(User); - if (!foundUsers) { - throw new Error('Could not find freshly seeded users. Aborting.'); - } - const foundNotes = await dataSource.manager.find(Note, { - relations: ['aliases'], - }); - if (!foundNotes) { - throw new Error('Could not find freshly seeded notes. Aborting.'); - } - for (const note of foundNotes) { - if (!(await note.aliases)[0]) { - throw new Error( - 'Could not find alias of freshly seeded notes. Aborting.', - ); + for (let i = 0; i < 3; i++) { + const author = (await dataSource.manager.save( + dataSource.manager.create(Author, Author.create(1)), + )) as Author; + const user = (await dataSource.manager.save( + dataSource.manager.create(User, users[i]), + )) as User; + const identity = Identity.create(user, ProviderType.LOCAL, false); + identity.passwordHash = await hashPassword(password); + dataSource.manager.create(Identity, identity); + author.user = dataSource.manager.save(user); + const revision = Revision.create( + 'This is a test note', + 'This is a test note', + notes[i], + ) as Revision; + const edit = Edit.create(author, 1, 42) as Edit; + revision.edits = Promise.resolve([edit]); + notes[i].revisions = Promise.all([revision]); + notes[i].userPermissions = Promise.resolve([]); + notes[i].groupPermissions = Promise.resolve([]); + user.ownedNotes = Promise.resolve([notes[i]]); + await dataSource.manager.save([ + notes[i], + user, + revision, + edit, + author, + identity, + ]); + } + const foundUsers = await dataSource.manager.find(User); + if (!foundUsers) { + throw new Error('Could not find freshly seeded users. Aborting.'); + } + const foundNotes = await dataSource.manager.find(Note, { + relations: ['aliases'], + }); + if (!foundNotes) { + throw new Error('Could not find freshly seeded notes. Aborting.'); } - } - for (const user of foundUsers) { - console.log( - `Created User '${user.username}' with password '${password}'`, - ); - } - for (const note of foundNotes) { - console.log(`Created Note '${(await note.aliases)[0].name ?? ''}'`); - } - for (const user of foundUsers) { for (const note of foundNotes) { - const historyEntry = HistoryEntry.create(user, note); - await dataSource.manager.save(historyEntry); + if (!(await note.aliases)[0]) { + throw new Error( + 'Could not find alias of freshly seeded notes. Aborting.', + ); + } + } + for (const user of foundUsers) { console.log( - `Created HistoryEntry for user '${user.username}' and note '${ - (await note.aliases)[0].name ?? '' - }'`, + `Created User '${user.username}' with password '${password}'`, ); } - } -}) + for (const note of foundNotes) { + console.log(`Created Note '${(await note.aliases)[0].name ?? ''}'`); + } + for (const user of foundUsers) { + for (const note of foundNotes) { + const historyEntry = HistoryEntry.create(user, note); + await dataSource.manager.save(historyEntry); + console.log( + `Created HistoryEntry for user '${user.username}' and note '${ + (await note.aliases)[0].name ?? '' + }'`, + ); + } + } + }) .catch((error) => console.log(error));