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

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

View File

@@ -47,14 +47,8 @@ module.exports = {
},
productModuleService: {
scope: "internal",
resources: "isolated",
resources: "shared",
resolve: "@medusajs/product",
options: {
database: {
clientUrl: DB_URL,
debug: false,
},
},
},
},
}

View File

@@ -23,7 +23,7 @@ describe("modules loader", () => {
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> = {
testService: {
resolutionPath: false,
@@ -52,7 +52,7 @@ describe("modules loader", () => {
expect(testService).toBe(undefined)
})
it("registers service ", async () => {
it("should register the service ", async () => {
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
resolutionPath: "@modules/default",
@@ -93,7 +93,7 @@ describe("modules loader", () => {
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> = {
testService: {
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> = {
testService: {
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)
const moduleResolutions: Record<string, ModuleResolution> = {
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)
const moduleResolutions: Record<string, ModuleResolution> = {
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)
const moduleResolutions: Record<string, ModuleResolution> = {
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)
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {

View File

@@ -69,24 +69,20 @@ export async function loadInternalModule(
}
}
const localContainer =
resources === MODULE_RESOURCE_TYPE.ISOLATED
? createMedusaContainer()
: (container.createScope() as MedusaContainer)
const localContainer = createMedusaContainer()
if (resources === MODULE_RESOURCE_TYPE.ISOLATED) {
const moduleDependencies = resolution?.dependencies ?? []
const dependencies = resolution?.dependencies ?? []
if (resources === MODULE_RESOURCE_TYPE.SHARED) {
dependencies.push("manager", "configModule")
}
for (const dependency of moduleDependencies) {
localContainer.register(
dependency,
asFunction(() => {
return container.hasRegistration(dependency)
? container.resolve(dependency)
: undefined
})
)
}
for (const dependency of dependencies) {
localContainer.register(
dependency,
asFunction(() => {
return container.resolve(dependency, { allowUnregistered: true })
})
)
}
const moduleLoaders = loadedModule?.loaders ?? []

View File

@@ -12,7 +12,7 @@ import { EntitySchema } from "@mikro-orm/core"
import * as ProductModels from "@models"
import { createConnection } from "../utils"
import { ModulesSdkTypes } from "@medusajs/types"
import { ConfigModule, ModulesSdkTypes } from "@medusajs/types"
export default async (
{
@@ -28,7 +28,14 @@ export default async (
moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
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 = (