chore: remove internal module resources option (#9582)

What:
* removes resouces type "shared" or "isolated" from internal modules.
* modules can have an isolated database connection by providing a database config as part of their options on `medusa-config`

CLOSES: FRMW-2593
This commit is contained in:
Carlos R. L. Rodrigues
2024-10-17 18:31:46 -03:00
committed by GitHub
parent b07dd33a57
commit 902ac12f73
36 changed files with 75 additions and 258 deletions

View File

@@ -2,7 +2,6 @@ export * from "./alter-columns-helper"
export * from "./array-difference"
export * from "./array-intersection"
export * from "./build-query"
export * from "./remove-undefined-properties"
export * from "./build-regexp-if-valid"
export * from "./camel-to-snake-case"
export * from "./container"
@@ -15,6 +14,7 @@ export * from "./deep-equal-obj"
export * from "./deep-flat-map"
export * from "./deep-merge"
export * from "./define-config"
export * from "./dynamic-import"
export * from "./env-editor"
export * from "./errors"
export * from "./file-system"
@@ -45,6 +45,7 @@ export * from "./medusa-container"
export * from "./normalize-import-path-with-source"
export * from "./object-from-string-path"
export * from "./object-to-string-path"
export * from "./omit-deep"
export * from "./optional-numeric-serializer"
export * from "./parse-cors-origins"
export * from "./partition-array"
@@ -57,6 +58,8 @@ export * from "./remote-query-object-from-string"
export * from "./remote-query-object-to-string"
export * from "./remove-nullisih"
export * from "./remove-undefined"
export * from "./remove-undefined-properties"
export * from "./resolve-exports"
export * from "./rules"
export * from "./selector-constraints-to-string"
export * from "./set-metadata"
@@ -67,11 +70,7 @@ export * from "./to-camel-case"
export * from "./to-handle"
export * from "./to-kebab-case"
export * from "./to-pascal-case"
export * from "./transaction"
export * from "./trim-zeros"
export * from "./upper-case-first"
export * from "./validate-handle"
export * from "./wrap-handler"
export * from "./resolve-exports"
export * from "./dynamic-import"
export * from "./omit-deep"

View File

@@ -1,3 +0,0 @@
export function doNotForceTransaction(): boolean {
return false
}

View File

@@ -1,2 +0,0 @@
export * from "./do-not-force-transaction"
export * from "./should-force-transaction"

View File

@@ -1,3 +0,0 @@
export function shouldForceTransaction(target: any): boolean {
return target.moduleDeclaration?.resources === "isolated"
}

View File

@@ -31,3 +31,5 @@ export function createPgConnection(options: Options) {
},
})
}
export const isSharedConnectionSymbol = Symbol.for("isSharedConnection")

View File

@@ -1,11 +1,7 @@
import { Context } from "@medusajs/types"
import { isString } from "../../common"
import { MedusaContextType } from "./context-parameter"
export function InjectTransactionManager(
shouldForceTransactionOrManagerProperty:
| string
| ((target: any) => boolean) = () => false,
managerProperty?: string
): MethodDecorator {
return function (
@@ -22,22 +18,14 @@ export function InjectTransactionManager(
}
const originalMethod = descriptor.value
const shouldForceTransaction = !isString(
shouldForceTransactionOrManagerProperty
)
? shouldForceTransactionOrManagerProperty
: () => false
managerProperty = isString(shouldForceTransactionOrManagerProperty)
? shouldForceTransactionOrManagerProperty
: managerProperty ?? "baseRepository_"
managerProperty ??= "baseRepository_"
const argIndex = target.MedusaContextIndex_[propertyKey]
descriptor.value = async function (...args: any[]) {
const shouldForceTransactionRes = shouldForceTransaction(target)
const context: Context = args[argIndex] ?? {}
const originalContext = args[argIndex] ?? {}
if (!shouldForceTransactionRes && context?.transactionManager) {
if (context?.transactionManager) {
return await originalMethod.apply(this, args)
}

View File

@@ -7,6 +7,7 @@ import {
mikroOrmCreateConnection,
mikroOrmFreeTextSearchFilterOptionsFactory,
} from "../../dal"
import { isSharedConnectionSymbol } from "../create-pg-connection"
import { loadDatabaseConfig } from "../load-module-database-config"
/**
@@ -53,10 +54,11 @@ export async function mikroOrmConnectionLoader({
return
}
if (
moduleDeclaration?.scope === "internal" &&
moduleDeclaration.resources === "shared"
) {
const moduleOptions = options as any
const reuseSharedConnection =
moduleOptions[isSharedConnectionSymbol] || !moduleOptions?.database
if (moduleDeclaration?.scope === "internal" && reuseSharedConnection) {
const shouldSwallowError = true
const dbConfig = loadDatabaseConfig(
moduleName,

View File

@@ -11,14 +11,12 @@ import {
} from "@medusajs/types"
import type { EntityClass, EntitySchema } from "@mikro-orm/core"
import {
doNotForceTransaction,
isDefined,
isObject,
isPresent,
isString,
lowerCaseFirst,
MedusaError,
shouldForceTransaction,
} from "../common"
import { FreeTextSearchFilterKey } from "../dal"
import { DmlEntity, toMikroORMEntity } from "../dml"
@@ -209,7 +207,7 @@ export function MedusaInternalService<
sharedContext?: Context
): Promise<InferEntityType<TEntity>[]>
@InjectTransactionManager(shouldForceTransaction, propertyRepositoryName)
@InjectTransactionManager(propertyRepositoryName)
async create(
data: any | any[],
@MedusaContext() sharedContext: Context = {}
@@ -246,7 +244,7 @@ export function MedusaInternalService<
sharedContext?: Context
): Promise<InferEntityType<TEntity>[]>
@InjectTransactionManager(shouldForceTransaction, propertyRepositoryName)
@InjectTransactionManager(propertyRepositoryName)
async update(
input: any | any[] | SelectorAndData | SelectorAndData[],
@MedusaContext() sharedContext: Context = {}
@@ -364,7 +362,7 @@ export function MedusaInternalService<
sharedContext?: Context
): Promise<void>
@InjectTransactionManager(doNotForceTransaction, propertyRepositoryName)
@InjectTransactionManager(propertyRepositoryName)
async delete(
idOrSelector:
| string