Skip to content

Commit

Permalink
feat: add middleware for mikroORM context on express and koa
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Russo committed Aug 3, 2022
1 parent 1008858 commit 18c2da1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/Shared/Application/Http/AppExpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import VerifyTokenExpressMiddleware from '../../../Auth/Presentation/Middlewares
import IApp from './IApp';
import IAppConfig from './IAppConfig';
import MainConfig from '../../../Config/MainConfig';
import { RequestContext } from '@mikro-orm/core';
import { orm } from '../../Infrastructure/Database/CreateMikroORMConnection';

import LoggerExpressMiddleware from '../../Presentation/Middlewares/LoggerExpressMiddleware';
import { createRequestContext, getRequestContext } from '../../Presentation/Shared/RequestContext';
import Logger from '../Logger/Logger';
import ContextMikroORMExpressMiddleware from '../../Presentation/Middlewares/ContextMikroORMExpressMiddleware';

class AppExpress implements IApp
{
Expand Down Expand Up @@ -59,11 +59,9 @@ class AppExpress implements IApp

if (MainConfig.getInstance().getConfig().dbConfig.default === 'MikroORM')
{
app.use((req, res, next) =>
{
RequestContext.create(orm.em, next);
});
app.use(ContextMikroORMExpressMiddleware);
}

app.use(async(req: any, res, next) =>
{
req.container = newContainer.createChildContainer();
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/Application/Http/AppKoa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import IAppConfig from './IAppConfig';
import WhiteListKoaHandler from '../../Tests/WhiteListKoaHandler';
import { ErrorKoaHandler } from './ErrorKoaHandler';
import MainConfig from '../../../Config/MainConfig';
import { RequestContext } from '@mikro-orm/core';
import { orm } from '../../Infrastructure/Database/CreateMikroORMConnection';

import LoggerKoaMiddleware from '../../Presentation/Middlewares/LoggerKoaMiddleware';
import container from '../../../register';
import { createRequestContext, getRequestContext } from '../../Presentation/Shared/RequestContext';
import Logger from '../Logger/Logger';
import ContextMikroORMKoaMiddleware from '../../Presentation/Middlewares/ContextMikroORMKoaMiddleware';

class AppKoa implements IApp
{
Expand Down Expand Up @@ -56,7 +56,7 @@ class AppKoa implements IApp

if (MainConfig.getInstance().getConfig().dbConfig.default === 'MikroORM')
{
this.app.use((ctx, next) => RequestContext.createAsync(orm.em, next));
this.app.use(ContextMikroORMKoaMiddleware);
}

this.app.use(async(ctx, next) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RequestContext } from '@mikro-orm/core';
import { orm } from '../../Infrastructure/Database/CreateMikroORMConnection';

const ContextMikroORMExpressMiddleware = async(req: any, res: any, next: any) => RequestContext.createAsync(orm.em, next);

export default ContextMikroORMExpressMiddleware;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ParameterizedContext, Next } from 'koa';
import { RequestContext } from '@mikro-orm/core';
import { orm } from '../../Infrastructure/Database/CreateMikroORMConnection';

const ContextMikroORMKoaMiddleware = async(ctx: ParameterizedContext, next: Next) => RequestContext.createAsync(orm.em, next);

export default ContextMikroORMKoaMiddleware;

0 comments on commit 18c2da1

Please sign in to comment.