-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple database connections (#56)
Co-authored-by: Martin Adámek <banan23@gmail.com>
- Loading branch information
Showing
15 changed files
with
1,214 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './mikro-orm.module'; | ||
export * from './mikro-orm.common'; | ||
export * from './mikro-orm.middleware'; | ||
export * from './multiple-mikro-orm.middleware'; | ||
export * from './typings'; |
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,15 @@ | ||
import type { MikroOrmMiddlewareModuleOptions, NestMiddlewareConsumer } from './typings'; | ||
import type { MiddlewareConsumer } from '@nestjs/common'; | ||
|
||
export function forRoutesPath(options: MikroOrmMiddlewareModuleOptions, consumer: MiddlewareConsumer) { | ||
const isNestMiddleware = (consumer: MiddlewareConsumer): consumer is NestMiddlewareConsumer => { | ||
return typeof (consumer as any).httpAdapter === 'object'; | ||
}; | ||
|
||
const usingFastify = (consumer: NestMiddlewareConsumer) => { | ||
return consumer.httpAdapter.constructor.name.toLowerCase().startsWith('fastify'); | ||
}; | ||
|
||
return options.forRoutesPath ?? | ||
(isNestMiddleware(consumer) && usingFastify(consumer) ? '(.*)' : '*'); | ||
} |
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,41 @@ | ||
import type { MiddlewareConsumer } from '@nestjs/common'; | ||
import { Global, Inject, Module, RequestMethod } from '@nestjs/common'; | ||
|
||
import { CONTEXT_NAMES, getMikroORMToken, MIKRO_ORM_MODULE_OPTIONS } from './mikro-orm.common'; | ||
import { MultipleMikroOrmMiddleware } from './multiple-mikro-orm.middleware'; | ||
import { MikroOrmMiddlewareModuleOptions } from './typings'; | ||
import type { MikroORM } from '@mikro-orm/core'; | ||
import { forRoutesPath } from './middleware.helper'; | ||
|
||
@Global() | ||
@Module({}) | ||
export class MikroOrmMiddlewareModule { | ||
|
||
constructor(@Inject(MIKRO_ORM_MODULE_OPTIONS) | ||
private readonly options: MikroOrmMiddlewareModuleOptions) { } | ||
|
||
static forMiddleware(options?: MikroOrmMiddlewareModuleOptions) { | ||
// Work around due to nestjs not supporting the ability to register multiple types | ||
// https://github.com/nestjs/nest/issues/770 | ||
// https://github.com/nestjs/nest/issues/4786#issuecomment-755032258 - workaround suggestion | ||
const inject = CONTEXT_NAMES.map(name => getMikroORMToken(name)); | ||
return { | ||
module: MikroOrmMiddlewareModule, | ||
providers: [ | ||
{ provide: MIKRO_ORM_MODULE_OPTIONS, useValue: options || {} }, | ||
{ | ||
provide: 'MikroORMs', | ||
useFactory: (...args: MikroORM[]) => args, | ||
inject, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
configure(consumer: MiddlewareConsumer): void { | ||
consumer | ||
.apply(MultipleMikroOrmMiddleware) | ||
.forRoutes({ path: forRoutesPath(this.options, consumer), method: RequestMethod.ALL }); | ||
} | ||
|
||
} |
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
Oops, something went wrong.