-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): deprecate
UseRequestContext
decorator (#4744)
- Loading branch information
Showing
4 changed files
with
95 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { MikroORM } from '../MikroORM'; | ||
import { RequestContext } from '../utils/RequestContext'; | ||
|
||
export function CreateRequestContext<T>(getContext?: MikroORM | ((type?: T) => MikroORM)): MethodDecorator { | ||
return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) { | ||
const originalMethod = descriptor.value; | ||
descriptor.value = async function (this: T, ...args: any[]) { | ||
/* istanbul ignore next */ | ||
const orm = getContext instanceof MikroORM ? getContext : (getContext?.(this) ?? (this as any).orm); | ||
|
||
if (!(orm as unknown instanceof MikroORM)) { | ||
throw new Error('@CreateRequestContext() decorator can only be applied to methods of classes with `orm: MikroORM` property, or with a callback parameter like `@CreateRequestContext(() => orm)`'); | ||
} | ||
|
||
return await RequestContext.createAsync(orm.em, async () => { | ||
return originalMethod.apply(this, args); | ||
}); | ||
}; | ||
|
||
return descriptor; | ||
}; | ||
} |
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