mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-23 23:11:01 +00:00
fix(media-service): user query builder
For reasons, the typeorm 0.3 broke the find() method when using relations in the WHERE clause. This replaces the find method with a query builder. Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
3503a45078
commit
27a93a2895
2 changed files with 37 additions and 9 deletions
|
@ -263,9 +263,17 @@ describe('MediaService', () => {
|
|||
id: '123',
|
||||
} as Note),
|
||||
} as MediaUpload;
|
||||
const createQueryBuilder = {
|
||||
where: () => createQueryBuilder,
|
||||
getMany: async () => {
|
||||
return [mockMediaUploadEntry];
|
||||
},
|
||||
};
|
||||
jest
|
||||
.spyOn(mediaRepo, 'find')
|
||||
.mockResolvedValueOnce([mockMediaUploadEntry]);
|
||||
.spyOn(mediaRepo, 'createQueryBuilder')
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.mockImplementation(() => createQueryBuilder);
|
||||
const mediaList = await service.listUploadsByNote({
|
||||
id: '123',
|
||||
} as Note);
|
||||
|
@ -273,14 +281,34 @@ describe('MediaService', () => {
|
|||
});
|
||||
|
||||
it('without uploads to note', async () => {
|
||||
jest.spyOn(mediaRepo, 'find').mockResolvedValueOnce([]);
|
||||
const createQueryBuilder = {
|
||||
where: () => createQueryBuilder,
|
||||
getMany: async () => {
|
||||
return [];
|
||||
},
|
||||
};
|
||||
jest
|
||||
.spyOn(mediaRepo, 'createQueryBuilder')
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.mockImplementation(() => createQueryBuilder);
|
||||
const mediaList = await service.listUploadsByNote({
|
||||
id: '123',
|
||||
} as Note);
|
||||
expect(mediaList).toEqual([]);
|
||||
});
|
||||
it('with error (undefined as return value of find)', async () => {
|
||||
jest.spyOn(mediaRepo, 'find').mockResolvedValueOnce([]);
|
||||
it('with error (null as return value of find)', async () => {
|
||||
const createQueryBuilder = {
|
||||
where: () => createQueryBuilder,
|
||||
getMany: async () => {
|
||||
return null;
|
||||
},
|
||||
};
|
||||
jest
|
||||
.spyOn(mediaRepo, 'createQueryBuilder')
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.mockImplementation(() => createQueryBuilder);
|
||||
const mediaList = await service.listUploadsByNote({
|
||||
id: '123',
|
||||
} as Note);
|
||||
|
|
|
@ -164,10 +164,10 @@ export class MediaService {
|
|||
* @return {MediaUpload[]} arary of media uploads owned by the user
|
||||
*/
|
||||
async listUploadsByNote(note: Note): Promise<MediaUpload[]> {
|
||||
const mediaUploads = await this.mediaUploadRepository.find({
|
||||
where: { note: Equal(note) },
|
||||
relations: ['user', 'note'],
|
||||
});
|
||||
const mediaUploads = await this.mediaUploadRepository
|
||||
.createQueryBuilder('upload')
|
||||
.where('upload.note = :note', { note: note.id })
|
||||
.getMany();
|
||||
if (mediaUploads === null) {
|
||||
return [];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue