Skip to content

Commit

Permalink
Create new async request context in uc
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed Jan 12, 2024
1 parent c6027ea commit 336ba02
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RabbitMQWrapperModule } from '@infra/rabbitmq';
import { S3ClientModule } from '@infra/s3-client';
import { Dictionary, IPrimaryKey } from '@mikro-orm/core';
import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
import { Module, NotFoundException, Scope } from '@nestjs/common';
import { Module, NotFoundException } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ALL_ENTITIES } from '@shared/domain/entity';
import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config';
Expand Down Expand Up @@ -49,8 +49,6 @@ const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
password: DB_PASSWORD,
user: DB_USERNAME,
entities: [...ALL_ENTITIES, FileRecord, FileRecordSecurityCheck],
registerRequestContext: false, // disable the automatic context
scope: Scope.REQUEST, // use nestjs request scopes instead

// debug: true, // use it for locally debugging of querys
}),
Expand Down
10 changes: 8 additions & 2 deletions apps/server/src/modules/files-storage/uc/files-storage.uc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EntityManager, RequestContext } from '@mikro-orm/core';
import { AuthorizationContext } from '@modules/authorization';
import { AuthorizationReferenceService } from '@modules/authorization/domain';
import { HttpService } from '@nestjs/axios';
Expand Down Expand Up @@ -36,7 +37,8 @@ export class FilesStorageUC {
private readonly authorizationReferenceService: AuthorizationReferenceService,
private readonly httpService: HttpService,
private readonly filesStorageService: FilesStorageService,
private readonly previewService: PreviewService
private readonly previewService: PreviewService,
private readonly em: EntityManager
) {
this.logger.setContext(FilesStorageUC.name);
}
Expand Down Expand Up @@ -69,7 +71,11 @@ export class FilesStorageUC {
const fileDto = FileDtoBuilder.buildFromRequest(info, file);

try {
const result = await this.filesStorageService.uploadFile(userId, params, fileDto);
const result = await RequestContext.createAsync(this.em, async () => {
const record = await this.filesStorageService.uploadFile(userId, params, fileDto);

return record;
});
resolve(result);
} catch (error) {
this.logger.error({ message: 'could not upload file', error: error as Error });
Expand Down

0 comments on commit 336ba02

Please sign in to comment.