Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)
* feat: scoped container for modules
This commit is contained in:
committed by
GitHub
parent
8e78c533c4
commit
77d46220c2
19
packages/utils/src/decorators/context-parameter.ts
Normal file
19
packages/utils/src/decorators/context-parameter.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export function MedusaContext() {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string | symbol,
|
||||
parameterIndex: number
|
||||
) {
|
||||
if (!target.MedusaContext_) {
|
||||
target.MedusaContext_ = {}
|
||||
}
|
||||
|
||||
if (propertyKey in target.MedusaContext_) {
|
||||
throw new Error(
|
||||
`Only one MedusaContext is allowed on method "${String(propertyKey)}".`
|
||||
)
|
||||
}
|
||||
|
||||
target.MedusaContext_[propertyKey] = parameterIndex
|
||||
}
|
||||
}
|
||||
2
packages/utils/src/decorators/index.ts
Normal file
2
packages/utils/src/decorators/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./context-parameter"
|
||||
export * from "./inject-entity-manager"
|
||||
37
packages/utils/src/decorators/inject-entity-manager.ts
Normal file
37
packages/utils/src/decorators/inject-entity-manager.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { SharedContext } from "@medusajs/types"
|
||||
|
||||
export function InjectEntityManager(
|
||||
managerProperty = "manager_"
|
||||
): MethodDecorator {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string | symbol,
|
||||
descriptor: any
|
||||
): void {
|
||||
if (!target.MedusaContext_) {
|
||||
throw new Error(
|
||||
`To apply @InjectEntityManager you have to flag a parameter using @MedusaContext`
|
||||
)
|
||||
}
|
||||
|
||||
const originalMethod = descriptor.value
|
||||
|
||||
const argIndex = target.MedusaContext_[propertyKey]
|
||||
descriptor.value = async function (...args: any[]) {
|
||||
const context: SharedContext = args[argIndex] ?? {}
|
||||
|
||||
if (context?.transactionManager) {
|
||||
return await originalMethod.apply(this, args)
|
||||
}
|
||||
|
||||
return await this[managerProperty].transaction(
|
||||
async (transactionManager) => {
|
||||
args[argIndex] = args[argIndex] ?? {}
|
||||
args[argIndex].transactionManager = transactionManager
|
||||
|
||||
return await originalMethod.apply(this, args)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
1
packages/utils/src/index.ts
Normal file
1
packages/utils/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./decorators"
|
||||
Reference in New Issue
Block a user