chore(medusa, orchestration, utils, workflows-sdk): add transaction options support and cleanup (#6020)

* chore(medusa, orchestration, utils, workflows-sdk): add transaction options support and cleanup
This commit is contained in:
Adrien de Peretti
2024-01-08 14:07:12 +01:00
committed by GitHub
parent 0c858f7fd4
commit fbee006e51
9 changed files with 92 additions and 16 deletions

View File

@@ -1,2 +1,3 @@
export * from "./inject-transaction-manager"
export * from "./inject-manager"
export * from "./inject-shared-context"

View File

@@ -0,0 +1,25 @@
import { Context, SharedContext } from "@medusajs/types"
export function InjectSharedContext(): MethodDecorator {
return function (
target: any,
propertyKey: string | symbol,
descriptor: any
): void {
if (!target.MedusaContextIndex_) {
throw new Error(
`To apply @InjectSharedContext you have to flag a parameter using @MedusaContext`
)
}
const originalMethod = descriptor.value
const argIndex = target.MedusaContextIndex_[propertyKey]
descriptor.value = function (...args: any[]) {
const context: SharedContext | Context = { ...(args[argIndex] ?? {}) }
args[argIndex] = context
return originalMethod.apply(this, args)
}
}
}

View File

@@ -1,5 +1,5 @@
import { MedusaError } from "../common"
import { ModulesSdkTypes } from "@medusajs/types"
import { MedusaError } from "../common"
function getEnv(key: string, moduleName: string): string {
const value =
@@ -45,10 +45,12 @@ function getDatabaseUrl(
config: ModulesSdkTypes.ModuleServiceInitializeOptions
): string {
const { clientUrl, host, port, user, password, database } = config.database!
if (clientUrl) {
return clientUrl
if (host) {
return `postgres://${user}:${password}@${host}:${port}/${database}`
}
return `postgres://${user}:${password}@${host}:${port}/${database}`
return clientUrl!
}
/**
@@ -65,8 +67,9 @@ export function loadDatabaseConfig(
ModulesSdkTypes.ModuleServiceInitializeOptions["database"],
"clientUrl" | "schema" | "driverOptions" | "debug"
> {
const clientUrl = getEnv("POSTGRES_URL", moduleName)
const clientUrl =
options?.database?.clientUrl ?? getEnv("POSTGRES_URL", moduleName)
const database = {
clientUrl,
schema: getEnv("POSTGRES_SCHEMA", moduleName) ?? "public",
@@ -74,16 +77,20 @@ export function loadDatabaseConfig(
getEnv("POSTGRES_DRIVER_OPTIONS", moduleName) ||
JSON.stringify(getDefaultDriverOptions(clientUrl))
),
debug: process.env.NODE_ENV?.startsWith("dev") ?? false,
debug: false,
connection: undefined,
}
if (isModuleServiceInitializeOptions(options)) {
database.clientUrl = getDatabaseUrl(options)
database.clientUrl = getDatabaseUrl({
database: { ...options.database, clientUrl },
})
database.schema = options.database!.schema ?? database.schema
database.driverOptions =
options.database!.driverOptions ??
getDefaultDriverOptions(database.clientUrl)
database.debug = options.database!.debug ?? database.debug
database.connection = options.database!.connection
}
if (!database.clientUrl && !silent) {