Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)

* feat: scoped container for modules
This commit is contained in:
Carlos R. L. Rodrigues
2023-03-15 12:09:45 -03:00
committed by GitHub
parent 8e78c533c4
commit 77d46220c2
65 changed files with 1116 additions and 1011 deletions

View 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
}
}

View File

@@ -0,0 +1,2 @@
export * from "./context-parameter"
export * from "./inject-entity-manager"

View 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)
}
)
}
}
}

View File

@@ -0,0 +1 @@
export * from "./decorators"