Skip to content

Commit

Permalink
Cleanup orm initialization and make entity map global. Add process exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Nov 23, 2023
1 parent cc275eb commit 063eb1c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 32 deletions.
7 changes: 2 additions & 5 deletions apps/server/src/apps/h5p-library-management.app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* istanbul ignore file */
/* eslint-disable no-console */
import { NestFactory } from '@nestjs/core';

// register source-map-support for debugging
import { install as sourceMapInstall } from 'source-map-support';

// application imports
import { LegacyLogger } from '@src/core/logger';
import { H5PLibraryManagementModule, H5PLibraryManagementService } from '@modules/h5p-library-management';

Expand All @@ -23,12 +19,13 @@ async function bootstrap() {
console.log(`##### Start H5P Library Management ######`);
console.log('#########################################');

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
// to execute it on this place for the ORM the allowGlobalContext: true must be set, but to executed in this way is a hack
await nestApp.get(H5PLibraryManagementService).run();
// TODO: properly close app (there is some issue with the logger)
console.log('#########################################');
console.log(`##### Close H5P Library Management ######`);
console.log('#########################################');
await nestApp.close();
process.exit(0);
}
void bootstrap();
1 change: 1 addition & 0 deletions apps/server/src/modules/h5p-editor/h5p-editor.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const imports = [
password: DB_PASSWORD,
user: DB_USERNAME,
// Needs ALL_ENTITIES for authorization
allowGlobalContext: true,
entities: [...ALL_ENTITIES, H5PContent, H5pEditorTempFile, InstalledLibrary],
}),
ConfigModule.forRoot(createConfigModuleOptions(config)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
import { Dictionary, IPrimaryKey } from '@mikro-orm/core';
import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
import { Module, NotFoundException } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ALL_ENTITIES } from '@shared/domain';
import { RabbitMQWrapperModule } from '@infra/rabbitmq';
import { S3ClientModule } from '@infra/s3-client';
import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config';
import { createConfigModuleOptions } from '@src/config';
import { CoreModule } from '@src/core';
import { Logger } from '@src/core/logger';
import { H5PEditorModule, s3ConfigContent, s3ConfigLibraries } from '@modules/h5p-editor';
// This export is always invalid. It is not allow to export repos or entities.
// In context of no domain object exists maybe it is ok. But i think it is not needed.
// All this database / mikroORM Stuff is done in H5PEditorModule.
// If yes remove exports from modules/h5p-editor/index.ts
// import { H5PEditorModule, s3ConfigContent, s3ConfigLibraries } from '@modules/h5p-editor';
import { H5PContent, InstalledLibrary } from '@modules/h5p-editor/entity';
import { H5PLibraryManagementService, h5PLibraryManagementConfig } from './service';

const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) =>
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
new NotFoundException(`The requested ${entityName}: ${where} has not been found.`),
};

const imports = [
ConfigModule.forRoot(createConfigModuleOptions(h5PLibraryManagementConfig)),
CoreModule,
H5PEditorModule,
RabbitMQWrapperModule,
MikroOrmModule.forRoot({
...defaultMikroOrmOptions,
type: 'mongo',
// TODO add mongoose options as mongo options (see database.js)
clientUrl: DB_URL,
password: DB_PASSWORD,
user: DB_USERNAME,
// allowGlobalContext: true, Please not
entities: [...ALL_ENTITIES, H5PContent, InstalledLibrary],
}),
S3ClientModule.register([s3ConfigContent, s3ConfigLibraries]),
];

Expand Down

0 comments on commit 063eb1c

Please sign in to comment.