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
@@ -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(