Skip to content

Commit ff2b056

Browse files
committed
add additional test for middleware changes
1 parent 046af6d commit ff2b056

File tree

3 files changed

+194
-24
lines changed

3 files changed

+194
-24
lines changed

tests/entities/foo.entity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { PrimaryKey, Entity } from '@mikro-orm/core';
1+
import { PrimaryKey, Entity, Filter } from '@mikro-orm/core';
22

33
@Entity()
4+
@Filter({ name: 'id', cond: args => ({ id: args.id }) })
45
export class Foo {
56

67
@PrimaryKey()

tests/mikro-orm.middleware.test.ts

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import { MikroORM, type Options } from '@mikro-orm/core';
1+
import { EntityManager, MikroORM, type Options } from '@mikro-orm/core';
22
import { 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';
412
import { Test, type TestingModule } from '@nestjs/testing';
513
import request from 'supertest';
6-
import { InjectMikroORM, MikroOrmModule, MultipleMikroOrmModule } from '../src';
14+
import { InjectEntityManager, InjectMikroORM, MikroOrmModule } from '../src';
715
import { Bar } from './entities/bar.entity';
816
import { 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
})
57102
class 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 () => {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { EntityManager, MikroORM, type Options } from '@mikro-orm/core';
2+
import { SqliteDriver } from '@mikro-orm/sqlite';
3+
import {
4+
Controller,
5+
Get,
6+
Module,
7+
type INestApplication,
8+
Injectable,
9+
type NestMiddleware,
10+
MiddlewareConsumer,
11+
NestModule,
12+
} from '@nestjs/common';
13+
import { Test, type TestingModule } from '@nestjs/testing';
14+
import request from 'supertest';
15+
import { InjectEntityManager, InjectMikroORM, MikroOrmModule, MultipleMikroOrmModule } from '../src';
16+
import { Bar } from './entities/bar.entity';
17+
import { Foo } from './entities/foo.entity';
18+
19+
const testOptions: Options = {
20+
dbName: ':memory:',
21+
driver: SqliteDriver,
22+
baseDir: __dirname,
23+
entities: ['entities'],
24+
};
25+
26+
@Controller('/foo')
27+
class FooController {
28+
29+
constructor(@InjectMikroORM('database-multi-foo') private database1: MikroORM) {}
30+
31+
@Get()
32+
foo() {
33+
return this.database1.em !== this.database1.em.getContext();
34+
}
35+
36+
}
37+
38+
@Controller('/bar')
39+
class BarController {
40+
41+
constructor(@InjectMikroORM('database-multi-bar') private database2: MikroORM) {}
42+
43+
@Get()
44+
bar() {
45+
return this.database2.em !== this.database2.em.getContext();
46+
}
47+
48+
}
49+
50+
@Injectable()
51+
export class TestMiddleware implements NestMiddleware {
52+
53+
constructor(@InjectEntityManager('database-multi-foo') private readonly em: EntityManager) {}
54+
55+
use(req: unknown, res: unknown, next: (...args: any[]) => void) {
56+
this.em.setFilterParams('id', { id: '1' });
57+
58+
return next();
59+
}
60+
61+
}
62+
63+
@Module({
64+
imports: [MikroOrmModule.forFeature([Foo], 'database-multi-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-multi-bar')],
79+
controllers: [BarController],
80+
})
81+
class BarModule {}
82+
83+
@Module({
84+
imports: [
85+
MikroOrmModule.forRootAsync({
86+
contextName: 'database-multi-foo',
87+
useFactory: () => ({
88+
registerRequestContext: false,
89+
...testOptions,
90+
}),
91+
}),
92+
MikroOrmModule.forRoot({
93+
contextName: 'database-multi-bar',
94+
registerRequestContext: false,
95+
...testOptions,
96+
}),
97+
MultipleMikroOrmModule.forRoot(),
98+
FooModule,
99+
BarModule,
100+
],
101+
})
102+
class TestModule {}
103+
104+
describe('Multiple Middleware executes request context for all MikroORM registered', () => {
105+
let app: INestApplication;
106+
107+
beforeAll(async () => {
108+
const moduleFixture: TestingModule = await Test.createTestingModule({
109+
imports: [TestModule],
110+
}).compile();
111+
112+
app = moduleFixture.createNestApplication();
113+
114+
await app.init();
115+
});
116+
117+
it(`forRoutes(/foo) should return 'true'`, () => {
118+
return request(app.getHttpServer()).get('/foo').expect(200, 'true');
119+
});
120+
121+
it(`forRoutes(/bar) should return 'true'`, () => {
122+
return request(app.getHttpServer()).get('/bar').expect(200, 'true');
123+
});
124+
125+
afterAll(async () => {
126+
await app.close();
127+
});
128+
});

0 commit comments

Comments
 (0)