fix(module-sdk): Shared modules loading (#4611)

* WIP

* tests wording

* Create nasty-files-type.md

* cleanup
This commit is contained in:
Adrien de Peretti
2023-07-27 12:53:31 +02:00
committed by GitHub
parent 8d0ce0af06
commit 379c83933e
5 changed files with 35 additions and 33 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/modules-sdk": patch
---
fix(module-sdk): Shared modules loading should not share the entire core container but only the resources that are meant to be shared
+1 -7
View File
@@ -47,14 +47,8 @@ module.exports = {
}, },
productModuleService: { productModuleService: {
scope: "internal", scope: "internal",
resources: "isolated", resources: "shared",
resolve: "@medusajs/product", resolve: "@medusajs/product",
options: {
database: {
clientUrl: DB_URL,
debug: false,
},
},
}, },
}, },
} }
@@ -23,7 +23,7 @@ describe("modules loader", () => {
container = createMedusaContainer() container = createMedusaContainer()
}) })
it("registers service as undefined in container when no resolution path is given", async () => { it("should register the service as undefined in the container when no resolution path is given", async () => {
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
resolutionPath: false, resolutionPath: false,
@@ -52,7 +52,7 @@ describe("modules loader", () => {
expect(testService).toBe(undefined) expect(testService).toBe(undefined)
}) })
it("registers service ", async () => { it("should register the service ", async () => {
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
resolutionPath: "@modules/default", resolutionPath: "@modules/default",
@@ -93,7 +93,7 @@ describe("modules loader", () => {
expect(typeof testService).toEqual("object") expect(typeof testService).toEqual("object")
}) })
it("runs defined loaders and logs error", async () => { it("should run the defined loaders and logs the errors if something fails", async () => {
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
resolutionPath: "@modules/brokenloader", resolutionPath: "@modules/brokenloader",
@@ -121,7 +121,7 @@ describe("modules loader", () => {
) )
}) })
it("logs error if no service is defined", async () => { it("should log the errors if no service is defined", async () => {
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
resolutionPath: "@modules/no-service", resolutionPath: "@modules/no-service",
@@ -149,7 +149,7 @@ describe("modules loader", () => {
) )
}) })
it("throws error if no service is defined and module is required", async () => { it("should throw an error if no service is defined and the module is required", async () => {
expect.assertions(1) expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
@@ -181,7 +181,7 @@ describe("modules loader", () => {
} }
}) })
it("throws error if default package isn't found and module is required", async () => { it("should throw an error if the default package isn't found and the module is required", async () => {
expect.assertions(1) expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
@@ -213,7 +213,7 @@ describe("modules loader", () => {
} }
}) })
it("throws error if no scope is defined to the module", async () => { it("should throw an error if no scope is defined on the module declaration", async () => {
expect.assertions(1) expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
@@ -245,7 +245,7 @@ describe("modules loader", () => {
} }
}) })
it("throws error if resources is not set when scope is defined as internal", async () => { it("should throw an error if the resources is not set when scope is defined as internal", async () => {
expect.assertions(1) expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = { const moduleResolutions: Record<string, ModuleResolution> = {
testService: { testService: {
@@ -69,24 +69,20 @@ export async function loadInternalModule(
} }
} }
const localContainer = const localContainer = createMedusaContainer()
resources === MODULE_RESOURCE_TYPE.ISOLATED
? createMedusaContainer()
: (container.createScope() as MedusaContainer)
if (resources === MODULE_RESOURCE_TYPE.ISOLATED) { const dependencies = resolution?.dependencies ?? []
const moduleDependencies = resolution?.dependencies ?? [] if (resources === MODULE_RESOURCE_TYPE.SHARED) {
dependencies.push("manager", "configModule")
}
for (const dependency of moduleDependencies) { for (const dependency of dependencies) {
localContainer.register( localContainer.register(
dependency, dependency,
asFunction(() => { asFunction(() => {
return container.hasRegistration(dependency) return container.resolve(dependency, { allowUnregistered: true })
? container.resolve(dependency) })
: undefined )
})
)
}
} }
const moduleLoaders = loadedModule?.loaders ?? [] const moduleLoaders = loadedModule?.loaders ?? []
+9 -2
View File
@@ -12,7 +12,7 @@ import { EntitySchema } from "@mikro-orm/core"
import * as ProductModels from "@models" import * as ProductModels from "@models"
import { createConnection } from "../utils" import { createConnection } from "../utils"
import { ModulesSdkTypes } from "@medusajs/types" import { ConfigModule, ModulesSdkTypes } from "@medusajs/types"
export default async ( export default async (
{ {
@@ -28,7 +28,14 @@ export default async (
moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL && moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
moduleDeclaration.resources === MODULE_RESOURCE_TYPE.SHARED moduleDeclaration.resources === MODULE_RESOURCE_TYPE.SHARED
) { ) {
return const { projectConfig } = container.resolve("configModule") as ConfigModule
options = {
database: {
clientUrl: projectConfig.database_url!,
driverOptions: projectConfig.database_extra!,
schema: projectConfig.database_schema!,
},
}
} }
const customManager = ( const customManager = (