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 21:31:46 +00:00
committed by GitHub
parent b07dd33a57
commit 902ac12f73
36 changed files with 75 additions and 258 deletions
-1
View File
@@ -42,7 +42,6 @@ module.exports = {
[Modules.AUTH]: true,
[Modules.USER]: {
scope: "internal",
resources: "shared",
resolve: "@medusajs/user",
options: {
jwt_secret: "test",
@@ -52,7 +52,6 @@ module.exports = {
},
[Modules.USER]: {
scope: "internal",
resources: "shared",
resolve: "@medusajs/user",
options: {
jwt_secret: "test",
@@ -9,7 +9,6 @@ export const InventoryModule = {
dependencies: [],
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
@@ -8,7 +8,6 @@ export const InventoryStockLocationLink = {
isQueryable: true,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
@@ -8,7 +8,6 @@ export const ProductInventoryLinkModule = {
isQueryable: true,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
@@ -9,7 +9,6 @@ export const ProductModule = {
dependencies: ["eventBusModuleService"],
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
@@ -9,7 +9,6 @@ export const StockLocationModule = {
dependencies: ["eventBusModuleService"],
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
+1 -26
View File
@@ -5,7 +5,7 @@ import {
Modules,
upperCaseFirst,
} from "@medusajs/utils"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types"
import { MODULE_SCOPE } from "./types"
export const ModulesDefinition: {
[key: string]: ModuleDefinition
@@ -19,7 +19,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.STOCK_LOCATION]: {
@@ -31,7 +30,6 @@ export const ModulesDefinition: {
dependencies: [Modules.EVENT_BUS],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.INVENTORY]: {
@@ -43,7 +41,6 @@ export const ModulesDefinition: {
dependencies: [Modules.EVENT_BUS],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.CACHE]: {
@@ -54,7 +51,6 @@ export const ModulesDefinition: {
isQueryable: false,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.PRODUCT]: {
@@ -66,7 +62,6 @@ export const ModulesDefinition: {
dependencies: [Modules.EVENT_BUS, ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.PRICING]: {
@@ -78,7 +73,6 @@ export const ModulesDefinition: {
dependencies: [Modules.EVENT_BUS, ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.PROMOTION]: {
@@ -90,7 +84,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.AUTH]: {
@@ -102,7 +95,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.WORKFLOW_ENGINE]: {
@@ -115,7 +107,6 @@ export const ModulesDefinition: {
__passSharedContainer: true,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.SALES_CHANNEL]: {
@@ -127,7 +118,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.FULFILLMENT]: {
@@ -139,7 +129,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER, Modules.EVENT_BUS],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.CART]: {
@@ -151,7 +140,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.CUSTOMER]: {
@@ -163,7 +151,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.PAYMENT]: {
@@ -175,7 +162,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.USER]: {
@@ -187,7 +173,6 @@ export const ModulesDefinition: {
dependencies: [Modules.EVENT_BUS, ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.REGION]: {
@@ -199,7 +184,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.ORDER]: {
@@ -211,7 +195,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER, Modules.EVENT_BUS],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.TAX]: {
@@ -223,7 +206,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER, Modules.EVENT_BUS],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.API_KEY]: {
@@ -235,7 +217,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.STORE]: {
@@ -247,7 +228,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.CURRENCY]: {
@@ -259,7 +239,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.FILE]: {
@@ -271,7 +250,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.NOTIFICATION]: {
@@ -283,7 +261,6 @@ export const ModulesDefinition: {
dependencies: [Modules.EVENT_BUS, ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.INDEX]: {
@@ -300,7 +277,6 @@ export const ModulesDefinition: {
],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.LOCKING]: {
@@ -312,7 +288,6 @@ export const ModulesDefinition: {
dependencies: [ContainerRegistrationKeys.LOGGER],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -1,5 +1,5 @@
import { InternalModuleDeclaration } from "@medusajs/types"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "../../types"
import { MODULE_SCOPE } from "../../types"
import { asValue } from "awilix"
import { MedusaModule } from "../../medusa-module"
@@ -12,7 +12,6 @@ const mockRegisterMedusaModule = jest.fn().mockImplementation(() => {
},
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -47,7 +46,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
abc: 123,
@@ -63,7 +61,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
abc: 123,
@@ -76,7 +73,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
different_options: "abc",
@@ -98,7 +94,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
abc: 123,
@@ -121,7 +116,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
abc: 123,
@@ -134,7 +128,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
different_options: "abc",
@@ -151,7 +144,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
abc: 123,
@@ -164,7 +156,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
main: true,
options: {
@@ -182,7 +173,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
alias: "mod_A",
options: {
@@ -196,7 +186,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
main: true,
alias: "mod_B",
@@ -211,7 +200,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
alias: "mod_C",
options: {
@@ -240,7 +228,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
alias: "mod_A",
options: {
@@ -254,7 +241,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
main: true,
alias: "mod_B",
@@ -269,7 +255,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
main: true,
alias: "mod_C",
@@ -290,7 +275,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
alias: "module_alias",
options: {
@@ -304,7 +288,6 @@ describe("Medusa Modules", () => {
defaultPath: "@path",
declaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
alias: "module_alias",
options: {
@@ -1,6 +1,6 @@
import { ModuleResolution } from "@medusajs/types"
import { createMedusaContainer } from "@medusajs/utils"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "../../types"
import { MODULE_SCOPE } from "../../types"
import { moduleLoader } from "../module-loader"
const logger = {
@@ -29,12 +29,10 @@ describe("modules loader", () => {
label: "TestService",
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -57,12 +55,10 @@ describe("modules loader", () => {
label: "TestService",
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -97,12 +93,10 @@ describe("modules loader", () => {
label: "TestService",
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -122,12 +116,10 @@ describe("modules loader", () => {
label: "TestService",
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -150,12 +142,10 @@ describe("modules loader", () => {
isRequired: true,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -179,12 +169,10 @@ describe("modules loader", () => {
isRequired: true,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}
@@ -210,13 +198,10 @@ describe("modules loader", () => {
isRequired: true,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
// @ts-ignore
moduleDeclaration: {
resources: MODULE_RESOURCE_TYPE.SHARED,
},
moduleDeclaration: {},
},
}
@@ -228,35 +213,4 @@ describe("modules loader", () => {
)
}
})
it("should throw an error if the resources is not set when scope is defined as internal", async () => {
expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
resolutionPath: "@modules/no-service",
definition: {
key: "testService",
defaultPackage: "testService",
label: "TestService",
isRequired: true,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
// @ts-ignore
moduleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
},
} as any,
}
try {
await moduleLoader({ container, moduleResolutions, logger })
} catch (err) {
expect(err.message).toEqual(
"The module TestService is missing its resources config"
)
}
})
})
@@ -1,6 +1,6 @@
import { InternalModuleDeclaration, ModuleDefinition } from "@medusajs/types"
import { ModulesDefinition } from "../../definitions"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "../../types"
import { MODULE_SCOPE } from "../../types"
import { registerMedusaModule } from "../register-modules"
const testServiceResolved = require.resolve(
@@ -16,7 +16,6 @@ describe("module definitions loader", () => {
isRequired: false,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
}
@@ -45,7 +44,6 @@ describe("module definitions loader", () => {
options: {},
moduleDeclaration: {
scope: "internal",
resources: "shared",
},
})
)
@@ -67,7 +65,6 @@ describe("module definitions loader", () => {
label: "Custom: customModulesABC",
}),
moduleDeclaration: {
resources: "shared",
scope: "internal",
},
options: {
@@ -130,7 +127,6 @@ describe("module definitions loader", () => {
options: {},
moduleDeclaration: {
scope: "internal",
resources: "shared",
},
})
)
@@ -154,7 +150,6 @@ describe("module definitions loader", () => {
options: {},
moduleDeclaration: {
scope: "internal",
resources: "shared",
},
})
)
@@ -170,7 +165,6 @@ describe("module definitions loader", () => {
const res = registerMedusaModule(defaultDefinition.key, {
scope: MODULE_SCOPE.INTERNAL,
resolve: defaultDefinition.defaultPackage,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
} as InternalModuleDeclaration)
expect(res[defaultDefinition.key]).toEqual(
@@ -180,7 +174,7 @@ describe("module definitions loader", () => {
options: {},
moduleDeclaration: {
scope: "internal",
resources: "isolated",
resolve: defaultDefinition.defaultPackage,
},
})
@@ -203,7 +197,7 @@ describe("module definitions loader", () => {
options: { test: 123 },
moduleDeclaration: {
scope: "internal",
resources: "shared",
options: { test: 123 },
},
})
@@ -219,7 +213,6 @@ describe("module definitions loader", () => {
resolve: defaultDefinition.defaultPackage,
options: { test: 123 },
scope: "internal",
resources: "isolated",
} as any)
expect(res[defaultDefinition.key]).toEqual(
@@ -229,7 +222,7 @@ describe("module definitions loader", () => {
options: { test: 123 },
moduleDeclaration: {
scope: "internal",
resources: "isolated",
resolve: defaultDefinition.defaultPackage,
options: { test: 123 },
},
@@ -50,7 +50,7 @@ async function loadModule(
}
const keyName = modDefinition.key
const { scope, resources } = resolution.moduleDeclaration ?? ({} as any)
const { scope } = resolution.moduleDeclaration ?? ({} as any)
const canSkip =
!resolution.resolutionPath &&
@@ -63,11 +63,8 @@ async function loadModule(
throw new Error("External Modules are not supported yet.")
}
if (!scope || (scope === MODULE_SCOPE.INTERNAL && !resources)) {
if (!scope) {
let message = `The module ${resolution.definition.label} has to define its scope (internal | external)`
if (scope === MODULE_SCOPE.INTERNAL && !resources) {
message = `The module ${resolution.definition.label} is missing its resources config`
}
container.register(keyName, asValue(undefined))
@@ -12,7 +12,7 @@ import {
normalizeImportPathWithSource,
} from "@medusajs/utils"
import { ModulesDefinition } from "../definitions"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "../types"
import { MODULE_SCOPE } from "../types"
export const registerMedusaModule = (
moduleKey: string,
@@ -86,12 +86,10 @@ function getCustomModuleResolution(
defaultPackage: "",
dependencies,
defaultModuleDeclaration: {
resources: MODULE_RESOURCE_TYPE.SHARED,
scope: MODULE_SCOPE.INTERNAL,
},
},
moduleDeclaration: {
resources: conf?.resources ?? MODULE_RESOURCE_TYPE.SHARED,
scope: MODULE_SCOPE.INTERNAL,
},
dependencies,
@@ -7,14 +7,14 @@ import {
ModuleWithJoinerConfigFixtures,
ModuleWithoutJoinerConfigFixtures,
} from "../__fixtures__"
import { ModuleService as ModuleServiceWithProvider } from "../__fixtures__/module-with-providers"
import { ModuleProviderService as ModuleServiceWithProviderProvider1 } from "../__fixtures__/module-with-providers/provider-1"
import { ModuleProvider2Service as ModuleServiceWithProviderProvider2 } from "../__fixtures__/module-with-providers/provider-2"
import {
getProviderRegistrationKey,
loadInternalModule,
loadResources,
} from "../load-internal"
import { ModuleProviderService as ModuleServiceWithProviderProvider1 } from "../__fixtures__/module-with-providers/provider-1"
import { ModuleProvider2Service as ModuleServiceWithProviderProvider2 } from "../__fixtures__/module-with-providers/provider-2"
import { ModuleService as ModuleServiceWithProvider } from "../__fixtures__/module-with-providers"
describe("load internal", () => {
describe("loadResources", () => {
@@ -34,7 +34,6 @@ describe("load internal", () => {
defaultPackage: false,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
}
@@ -117,7 +116,6 @@ describe("load internal", () => {
defaultPackage: false,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
}
@@ -200,7 +198,6 @@ describe("load internal", () => {
defaultPackage: false,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
}
@@ -282,7 +279,6 @@ describe("load internal", () => {
defaultPackage: false,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
}
@@ -346,7 +342,6 @@ describe("load internal", () => {
),
moduleDeclaration: {
scope: "internal",
resources: "shared",
},
definition: {
key: "module-with-providers",
@@ -354,7 +349,6 @@ describe("load internal", () => {
defaultPackage: false,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
options: {
@@ -399,7 +393,6 @@ describe("load internal", () => {
),
moduleDeclaration: {
scope: "internal",
resources: "shared",
},
definition: {
key: "module-with-providers",
@@ -407,7 +400,6 @@ describe("load internal", () => {
defaultPackage: false,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
options: {
@@ -28,7 +28,6 @@ import { asFunction, asValue } from "awilix"
import { statSync } from "fs"
import { readdir } from "fs/promises"
import { dirname, join, resolve } from "path"
import { MODULE_RESOURCE_TYPE } from "../../types"
type ModuleResource = {
services: Function[]
@@ -192,9 +191,6 @@ export async function loadInternalModule(args: {
? resolution.definition.key
: resolution.definition.key + "__loaderOnly"
const { resources } =
resolution.moduleDeclaration as InternalModuleDeclaration
const loadedModule = await resolveModuleExports({ resolution })
if ("error" in loadedModule) {
@@ -232,14 +228,13 @@ export async function loadInternalModule(args: {
const localContainer = createMedusaContainer()
const dependencies = resolution?.dependencies ?? []
if (resources === MODULE_RESOURCE_TYPE.SHARED) {
dependencies.push(
ContainerRegistrationKeys.MANAGER,
ContainerRegistrationKeys.CONFIG_MODULE,
ContainerRegistrationKeys.LOGGER,
ContainerRegistrationKeys.PG_CONNECTION
)
}
dependencies.push(
ContainerRegistrationKeys.MANAGER,
ContainerRegistrationKeys.CONFIG_MODULE,
ContainerRegistrationKeys.LOGGER,
ContainerRegistrationKeys.PG_CONNECTION
)
for (const dependency of dependencies) {
localContainer.register(
+9 -11
View File
@@ -18,6 +18,7 @@ import {
dynamicImport,
GraphQLUtils,
isObject,
isSharedConnectionSymbol,
isString,
MedusaError,
MODULE_PACKAGE_NAMES,
@@ -34,7 +35,7 @@ import {
} from "./medusa-module"
import { RemoteLink } from "./remote-link"
import { createQuery, RemoteQuery } from "./remote-query"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types"
import { MODULE_SCOPE } from "./types"
const LinkModulePackage = MODULE_PACKAGE_NAMES[Modules.LINK]
@@ -123,15 +124,14 @@ export async function loadModules(args: {
}
declaration.scope ??= MODULE_SCOPE.INTERNAL
if (declaration.scope === MODULE_SCOPE.INTERNAL && !declaration.resources) {
declaration.resources = MODULE_RESOURCE_TYPE.SHARED
}
if (
declaration.scope === MODULE_SCOPE.INTERNAL &&
declaration.resources === MODULE_RESOURCE_TYPE.SHARED
) {
if (declaration.scope === MODULE_SCOPE.INTERNAL) {
declaration.options ??= {}
if (!declaration.options.database) {
declaration.options[isSharedConnectionSymbol] = true
}
declaration.options.database ??= {
...sharedResourcesConfig?.database,
}
@@ -493,9 +493,7 @@ async function MedusaApp_({
for (const { resolution: moduleResolution } of moduleResolutions) {
if (
!moduleResolution.options?.database &&
moduleResolution.moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
moduleResolution.moduleDeclaration?.resources ===
MODULE_RESOURCE_TYPE.SHARED
moduleResolution.moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL
) {
moduleResolution.options ??= {}
moduleResolution.options.database = {
+3 -14
View File
@@ -26,7 +26,7 @@ import {
registerMedusaModule,
} from "./loaders"
import { loadModuleMigrations } from "./loaders/utils"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types"
import { MODULE_SCOPE } from "./types"
const logger: any = {
log: (a) => console.log(a),
@@ -439,9 +439,6 @@ class MedusaModule {
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) {
modDeclaration = {
scope: declaration?.scope || MODULE_SCOPE.INTERNAL,
resources:
(declaration as InternalModuleDeclaration)?.resources ||
MODULE_RESOURCE_TYPE.ISOLATED,
resolve: defaultPath,
options: declaration?.options ?? declaration,
dependencies:
@@ -449,15 +446,10 @@ class MedusaModule {
alias: declaration?.alias,
main: declaration?.main,
worker_mode: workerMode,
} as any
} as InternalModuleDeclaration
}
// TODO: Only do that while legacy modules sharing the manager exists then remove the ternary in favor of createMedusaContainer({}, globalContainer)
const container =
modDeclaration.scope === MODULE_SCOPE.INTERNAL &&
modDeclaration.resources === MODULE_RESOURCE_TYPE.SHARED
? sharedContainer ?? createMedusaContainer()
: createMedusaContainer({}, sharedContainer)
const container = sharedContainer ?? createMedusaContainer()
if (injectedDependencies) {
for (const service in injectedDependencies) {
@@ -750,7 +742,6 @@ class MedusaModule {
}: MigrationOptions): Promise<void> {
const moduleResolutions = registerMedusaModule(moduleKey, {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: modulePath,
options,
})
@@ -787,7 +778,6 @@ class MedusaModule {
}: MigrationOptions): Promise<void> {
const moduleResolutions = registerMedusaModule(moduleKey, {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: modulePath,
options,
})
@@ -824,7 +814,6 @@ class MedusaModule {
}: MigrationOptions): Promise<void> {
const moduleResolutions = registerMedusaModule(moduleKey, {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: modulePath,
options,
})
@@ -2,8 +2,3 @@ export enum MODULE_SCOPE {
INTERNAL = "internal",
EXTERNAL = "external",
}
export enum MODULE_RESOURCE_TYPE {
SHARED = "shared",
ISOLATED = "isolated",
}
+3 -3
View File
@@ -1,8 +1,8 @@
export * from "./common"
export * from "./rule"
export * from "./batch"
export * from "./camel-case"
export * from "./common"
export * from "./config-module"
export * from "./medusa-cli"
export * from "./medusa-container"
export * from "./rule"
export * from "./with-calculated"
export * from "./camel-case"
@@ -46,7 +46,6 @@ export type CustomModuleDefinition = {
export type InternalModuleDeclaration = {
scope: "internal"
resources: "shared" | "isolated"
dependencies?: string[]
definition?: CustomModuleDefinition // That represent the definition of the module, such as the one we have for the medusa supported modules. This property is used for custom made modules.
resolve?: string | ModuleExports
+4 -5
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"
@@ -1,3 +0,0 @@
export function doNotForceTransaction(): boolean {
return false
}
@@ -1,2 +0,0 @@
export * from "./do-not-force-transaction"
export * from "./should-force-transaction"
@@ -1,3 +0,0 @@
export function shouldForceTransaction(target: any): boolean {
return target.moduleDeclaration?.resources === "isolated"
}
@@ -31,3 +31,5 @@ export function createPgConnection(options: Options) {
},
})
}
export const isSharedConnectionSymbol = Symbol.for("isSharedConnection")
@@ -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)
}
@@ -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,
@@ -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
@@ -1,6 +1,7 @@
import {
ContainerRegistrationKeys,
DmlEntity,
isSharedConnectionSymbol,
loadModels,
Modules,
ModulesSdkUtils,
@@ -118,6 +119,7 @@ export function moduleIntegrationTestRunner<TService = any>({
options: {
database: dbConfig,
...moduleOptions,
[isSharedConnectionSymbol]: true,
},
},
}
@@ -46,7 +46,6 @@ describe("RedisEventBusService", () => {
eventBus = new RedisEventBusService(moduleDeps, simpleModuleOptions, {
scope: "internal",
resources: "shared",
})
})
@@ -71,7 +70,6 @@ describe("RedisEventBusService", () => {
it("Throws on isolated module declaration", () => {
try {
eventBus = new RedisEventBusService(moduleDeps, simpleModuleOptions, {
resources: "isolated",
scope: "internal",
})
} catch (error) {
@@ -89,7 +87,6 @@ describe("RedisEventBusService", () => {
eventBus = new RedisEventBusService(moduleDeps, simpleModuleOptions, {
scope: "internal",
resources: "shared",
})
queue = (eventBus as any).queue_
@@ -154,7 +151,6 @@ describe("RedisEventBusService", () => {
},
},
{
resources: "shared",
scope: "internal",
}
)
@@ -199,7 +195,6 @@ describe("RedisEventBusService", () => {
},
},
{
resources: "shared",
scope: "internal",
}
)
@@ -344,7 +339,6 @@ describe("RedisEventBusService", () => {
jest.clearAllMocks()
eventBus = new RedisEventBusService(moduleDeps, simpleModuleOptions, {
resources: "shared",
scope: "internal",
})
})
@@ -260,7 +260,7 @@ moduleIntegrationTestRunner({
[Modules.FULFILLMENT]: {
definition: ModulesDefinition[Modules.FULFILLMENT],
options: {
databaseConfig,
database: databaseConfig,
providers: Object.keys(providersConfig).map((id) => ({
resolve: resolve(
process.cwd() +
@@ -310,7 +310,7 @@ moduleIntegrationTestRunner({
[Modules.FULFILLMENT]: {
definition: ModulesDefinition[Modules.FULFILLMENT],
options: {
databaseConfig,
database: databaseConfig,
providers: Object.keys(providersConfig2).map((id) => ({
resolve: resolve(
process.cwd() +
@@ -6,12 +6,12 @@ import {
Subscriber,
} from "@medusajs/framework/types"
import {
MikroOrmBaseRepository as BaseRepository,
ContainerRegistrationKeys,
InjectManager,
InjectTransactionManager,
isDefined,
MedusaContext,
MikroOrmBaseRepository as BaseRepository,
} from "@medusajs/framework/utils"
import { EntityManager, SqlEntityManager } from "@mikro-orm/postgresql"
import { IndexData, IndexRelation } from "@models"
@@ -227,7 +227,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
}
}
@InjectManager("baseRepository_")
@InjectManager()
async query<const TEntry extends string>(
config: IndexTypes.IndexQueryConfig<TEntry>,
@MedusaContext() sharedContext: Context = {}
@@ -320,7 +320,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
* @param sharedContext
* @protected
*/
@InjectTransactionManager("baseRepository_")
@InjectTransactionManager()
protected async onCreate<
TData extends { id: string; [key: string]: unknown }
>(
@@ -414,7 +414,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
* @param sharedContext
* @protected
*/
@InjectTransactionManager("baseRepository_")
@InjectTransactionManager()
protected async onUpdate<
TData extends { id: string; [key: string]: unknown }
>(
@@ -461,7 +461,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
* @param sharedContext
* @protected
*/
@InjectTransactionManager("baseRepository_")
@InjectTransactionManager()
protected async onDelete<
TData extends { id: string; [key: string]: unknown }
>(
@@ -515,7 +515,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
* @param schemaEntityObjectRepresentation
* @protected
*/
@InjectTransactionManager("baseRepository_")
@InjectTransactionManager()
protected async onAttach<
TData extends { id: string; [key: string]: unknown }
>(
@@ -636,7 +636,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
* @param sharedContext
* @protected
*/
@InjectTransactionManager("baseRepository_")
@InjectTransactionManager()
protected async onDetach<
TData extends { id: string; [key: string]: unknown }
>(
@@ -1,8 +1,4 @@
import {
MedusaModule,
MODULE_RESOURCE_TYPE,
MODULE_SCOPE,
} from "@medusajs/framework/modules-sdk"
import { MedusaModule, MODULE_SCOPE } from "@medusajs/framework/modules-sdk"
import {
ExternalModuleDeclaration,
ILinkModule,
@@ -158,11 +154,6 @@ export const initialize = async (
dependencies: [Modules.EVENT_BUS],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: injectedDependencies?.[
ContainerRegistrationKeys.PG_CONNECTION
]
? MODULE_RESOURCE_TYPE.SHARED
: MODULE_RESOURCE_TYPE.ISOLATED,
},
}
@@ -22,7 +22,6 @@ import {
ModulesSdkUtils,
} from "@medusajs/framework/utils"
import { LinkService } from "@services"
import { shouldForceTransaction } from "../utils"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
@@ -173,7 +172,7 @@ export default class LinkModuleService implements ILinkModule {
return [rows.map((row) => row.toJSON()), count]
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
@InjectTransactionManager()
async create(
primaryKeyOrBulkData:
| string
@@ -222,7 +221,7 @@ export default class LinkModuleService implements ILinkModule {
return links.map((row) => row.toJSON())
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
@InjectTransactionManager()
async dismiss(
primaryKeyOrBulkData: string | string[] | [string | string[], string][],
foreignKeyData?: string,
@@ -247,7 +246,7 @@ export default class LinkModuleService implements ILinkModule {
return links.map((row) => row.toJSON())
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
@InjectTransactionManager()
async delete(
data: any,
@MedusaContext() sharedContext: Context = {}
@@ -322,7 +321,7 @@ export default class LinkModuleService implements ILinkModule {
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
@InjectTransactionManager()
protected async softDelete_(
data: any[],
@MedusaContext() sharedContext: Context = {}
@@ -380,7 +379,7 @@ export default class LinkModuleService implements ILinkModule {
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
@InjectTransactionManager()
async restore_(
data: any,
@MedusaContext() sharedContext: Context = {}
@@ -5,7 +5,6 @@ import {
MedusaContext,
ModulesSdkUtils,
} from "@medusajs/framework/utils"
import { doNotForceTransaction } from "../utils"
type InjectedDependencies = {
linkRepository: any
@@ -41,7 +40,7 @@ export default class LinkService<TEntity> {
return await this.linkRepository_.findAndCount(queryOptions, sharedContext)
}
@InjectTransactionManager(doNotForceTransaction, "linkRepository_")
@InjectTransactionManager("linkRepository_")
async create(
data: unknown[],
@MedusaContext() sharedContext: Context = {}
@@ -51,7 +50,7 @@ export default class LinkService<TEntity> {
})
}
@InjectTransactionManager(doNotForceTransaction, "linkRepository_")
@InjectTransactionManager("linkRepository_")
async dismiss(
data: unknown[],
@MedusaContext() sharedContext: Context = {}
@@ -75,7 +74,7 @@ export default class LinkService<TEntity> {
return rows
}
@InjectTransactionManager(doNotForceTransaction, "linkRepository_")
@InjectTransactionManager("linkRepository_")
async delete(
data: unknown,
@MedusaContext() sharedContext: Context = {}
@@ -85,7 +84,7 @@ export default class LinkService<TEntity> {
})
}
@InjectTransactionManager(doNotForceTransaction, "linkRepository_")
@InjectTransactionManager("linkRepository_")
async softDelete(
data: any[],
@MedusaContext() sharedContext: Context = {}
@@ -109,7 +108,7 @@ export default class LinkService<TEntity> {
})
}
@InjectTransactionManager(doNotForceTransaction, "linkRepository_")
@InjectTransactionManager("linkRepository_")
async restore(
data: any,
@MedusaContext() sharedContext: Context = {}
@@ -1,12 +1,2 @@
import { MODULE_RESOURCE_TYPE } from "@medusajs/framework/modules-sdk"
export * from "./generate-entity"
export * from "./generate-schema"
export function shouldForceTransaction(target: any): boolean {
return target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
}
export function doNotForceTransaction(): boolean {
return false
}