1- import { MikroORM , type Options } from '@mikro-orm/core' ;
1+ import { EntityManager , MikroORM , type Options } from '@mikro-orm/core' ;
22import { SqliteDriver } from '@mikro-orm/sqlite' ;
3- import { Controller , Get , Module , type INestApplication } from '@nestjs/common' ;
3+ import {
4+ Controller ,
5+ Get ,
6+ type INestApplication ,
7+ Injectable ,
8+ MiddlewareConsumer ,
9+ Module , type NestMiddleware ,
10+ NestModule ,
11+ } from '@nestjs/common' ;
412import { Test , type TestingModule } from '@nestjs/testing' ;
513import request from 'supertest' ;
6- import { InjectMikroORM , MikroOrmModule , MultipleMikroOrmModule } from '../src' ;
14+ import { InjectEntityManager , InjectMikroORM , MikroOrmModule } from '../src' ;
715import { Bar } from './entities/bar.entity' ;
816import { Foo } from './entities/foo.entity' ;
917
@@ -14,45 +22,82 @@ const testOptions: Options = {
1422 entities : [ 'entities' ] ,
1523} ;
1624
17- @Controller ( )
18- class TestController {
25+ @Controller ( '/foo' )
26+ export class FooController {
1927
20- constructor (
21- @InjectMikroORM ( 'database1' ) private database1 : MikroORM ,
22- @InjectMikroORM ( 'database2' ) private database2 : MikroORM ,
23- ) { }
28+ constructor ( @InjectMikroORM ( 'database-foo' ) private database1 : MikroORM ) { }
2429
25- @Get ( 'foo' )
30+ @Get ( )
2631 foo ( ) {
2732 return this . database1 . em !== this . database1 . em . getContext ( ) ;
2833 }
2934
30- @Get ( 'bar' )
35+ }
36+
37+ @Controller ( '/bar' )
38+ export class BarController {
39+
40+ constructor ( @InjectMikroORM ( 'database-bar' ) private database2 : MikroORM ) { }
41+
42+ @Get ( )
3143 bar ( ) {
3244 return this . database2 . em !== this . database2 . em . getContext ( ) ;
3345 }
3446
3547}
3648
49+ @Injectable ( )
50+ export class TestMiddleware implements NestMiddleware {
51+
52+ constructor ( @InjectEntityManager ( 'database-foo' ) private readonly em : EntityManager ) { }
53+
54+ use ( req : unknown , res : unknown , next : ( ...args : any [ ] ) => void ) {
55+ // Throws error "Using global EntityManager instance methods for context specific actions is disallowed"
56+ this . em . setFilterParams ( 'id' , { id : '1' } ) ;
57+
58+ return next ( ) ;
59+ }
60+
61+ }
62+
63+ @Module ( {
64+ imports : [ MikroOrmModule . forFeature ( [ Foo ] , 'database-foo' ) ] ,
65+ controllers : [ FooController ] ,
66+ } )
67+ class FooModule implements NestModule {
68+
69+ configure ( consumer : MiddlewareConsumer ) : void {
70+ consumer
71+ . apply ( TestMiddleware )
72+ . forRoutes ( '/' ) ;
73+ }
74+
75+ }
76+
77+ @Module ( {
78+ imports : [ MikroOrmModule . forFeature ( [ Bar ] , 'database-bar' ) ] ,
79+ controllers : [ BarController ] ,
80+ } )
81+ class BarModule { }
82+
3783@Module ( {
3884 imports : [
3985 MikroOrmModule . forRootAsync ( {
40- contextName : 'database1 ' ,
86+ contextName : 'database-foo ' ,
4187 useFactory : ( ) => ( {
4288 registerRequestContext : false ,
4389 ...testOptions ,
4490 } ) ,
4591 } ) ,
4692 MikroOrmModule . forRoot ( {
47- contextName : 'database2 ' ,
93+ contextName : 'database-bar ' ,
4894 registerRequestContext : false ,
4995 ...testOptions ,
5096 } ) ,
51- MultipleMikroOrmModule . forRoot ( ) ,
52- MikroOrmModule . forFeature ( [ Foo ] , 'database1' ) ,
53- MikroOrmModule . forFeature ( [ Bar ] , 'database2' ) ,
97+ MikroOrmModule . forMiddleware ( ) ,
98+ FooModule ,
99+ BarModule ,
54100 ] ,
55- controllers : [ TestController ] ,
56101} )
57102class TestModule { }
58103
@@ -69,12 +114,8 @@ describe('Middleware executes request context for all MikroORM registered', () =
69114 await app . init ( ) ;
70115 } ) ;
71116
72- it ( `forRoutes(/foo) should return 'true'` , ( ) => {
73- return request ( app . getHttpServer ( ) ) . get ( '/foo' ) . expect ( 200 , 'true' ) ;
74- } ) ;
75-
76- it ( `forRoutes(/bar) should return 'true'` , ( ) => {
77- return request ( app . getHttpServer ( ) ) . get ( '/foo' ) . expect ( 200 , 'true' ) ;
117+ it ( `forRoutes(/foo) should return error` , ( ) => {
118+ return request ( app . getHttpServer ( ) ) . get ( '/foo' ) . expect ( 500 ) ;
78119 } ) ;
79120
80121 afterAll ( async ( ) => {
0 commit comments