-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve issue with ordering of middleware being applied (#189)
When setting up the nestjs modules with MikroORM there is a chance it can throw Using global EntityManager instance methods for context specific actions is disallowed. when interacting with the EM within another middleware. This fix applies to both single and multi database set-ups.
- Loading branch information
Showing
9 changed files
with
208 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { EntityManager, MikroORM, type Options } from '@mikro-orm/core'; | ||
import { SqliteDriver } from '@mikro-orm/sqlite'; | ||
import { | ||
Controller, | ||
Get, | ||
Module, | ||
type INestApplication, | ||
Injectable, | ||
type MiddlewareConsumer, | ||
type NestMiddleware, | ||
type NestModule, | ||
} from '@nestjs/common'; | ||
import { Test, type TestingModule } from '@nestjs/testing'; | ||
import request from 'supertest'; | ||
import { MikroOrmModule } from '../src'; | ||
import { Foo } from './entities/foo.entity'; | ||
|
||
const testOptions: Options = { | ||
dbName: ':memory:', | ||
driver: SqliteDriver, | ||
baseDir: __dirname, | ||
entities: ['entities'], | ||
}; | ||
|
||
@Controller('/foo') | ||
class FooController { | ||
|
||
constructor(private database1: MikroORM) {} | ||
|
||
@Get() | ||
foo() { | ||
return this.database1.em !== this.database1.em.getContext(); | ||
} | ||
|
||
} | ||
|
||
@Injectable() | ||
export class TestMiddleware implements NestMiddleware { | ||
|
||
constructor(private readonly em: EntityManager) {} | ||
|
||
use(req: unknown, res: unknown, next: (...args: any[]) => void) { | ||
this.em.setFilterParams('id', { id: '1' }); | ||
|
||
return next(); | ||
} | ||
|
||
} | ||
|
||
@Module({ | ||
imports: [MikroOrmModule.forFeature([Foo])], | ||
controllers: [FooController], | ||
}) | ||
class FooModule implements NestModule { | ||
|
||
configure(consumer: MiddlewareConsumer): void { | ||
consumer | ||
.apply(TestMiddleware) | ||
.forRoutes('/'); | ||
} | ||
|
||
} | ||
|
||
@Module({ | ||
imports: [ | ||
MikroOrmModule.forRootAsync({ | ||
useFactory: () => testOptions, | ||
}), | ||
FooModule, | ||
], | ||
}) | ||
class TestModule {} | ||
|
||
describe('Middleware executes request context', () => { | ||
let app: INestApplication; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [TestModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
|
||
await app.init(); | ||
}); | ||
|
||
it(`forRoutes(/foo) should return 'true'`, () => { | ||
return request(app.getHttpServer()).get('/foo').expect(200, 'true'); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
}); |
Oops, something went wrong.