diff --git a/.changeset/good-mails-hunt.md b/.changeset/good-mails-hunt.md new file mode 100644 index 0000000000..f1a6365a37 --- /dev/null +++ b/.changeset/good-mails-hunt.md @@ -0,0 +1,15 @@ +--- +"@medusajs/medusa": patch +"@medusajs/cache-inmemory": patch +"@medusajs/cache-redis": patch +"@medusajs/event-bus-local": patch +"@medusajs/event-bus-redis": patch +"@medusajs/inventory": patch +"@medusajs/link-modules": patch +"@medusajs/modules-sdk": patch +"@medusajs/pricing": patch +"@medusajs/product": patch +"@medusajs/stock-location": patch +--- + +fix(medusa, modules-sdk, modules): Module loading was missing the expected dependencies and remote query reference fix diff --git a/packages/cache-inmemory/src/initialize/index.ts b/packages/cache-inmemory/src/initialize/index.ts index 1e0629c905..80decde7af 100644 --- a/packages/cache-inmemory/src/initialize/index.ts +++ b/packages/cache-inmemory/src/initialize/index.ts @@ -11,12 +11,13 @@ export const initialize = async ( options?: InMemoryCacheModuleOptions | ExternalModuleDeclaration ): Promise => { const serviceKey = Modules.CACHE - const loaded = await MedusaModule.bootstrap( - serviceKey, - "@medusajs/cache-inmemory", - options as InternalModuleDeclaration | ExternalModuleDeclaration, - undefined - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: "@medusajs/cache-inmemory", + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + }) return loaded[serviceKey] } diff --git a/packages/cache-redis/src/initialize/index.ts b/packages/cache-redis/src/initialize/index.ts index 1a69dd7fe9..9d40e66558 100644 --- a/packages/cache-redis/src/initialize/index.ts +++ b/packages/cache-redis/src/initialize/index.ts @@ -11,12 +11,13 @@ export const initialize = async ( options?: RedisCacheModuleOptions | ExternalModuleDeclaration ): Promise => { const serviceKey = Modules.CACHE - const loaded = await MedusaModule.bootstrap( - serviceKey, - "@medusajs/cache-redis", - options as InternalModuleDeclaration | ExternalModuleDeclaration, - undefined - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: "@medusajs/cache-redis", + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + }) return loaded[serviceKey] } diff --git a/packages/event-bus-local/src/initialize/index.ts b/packages/event-bus-local/src/initialize/index.ts index f2bec97960..23ea34b9d0 100644 --- a/packages/event-bus-local/src/initialize/index.ts +++ b/packages/event-bus-local/src/initialize/index.ts @@ -3,10 +3,10 @@ import { IEventBusService } from "@medusajs/types" export const initialize = async (): Promise => { const serviceKey = Modules.EVENT_BUS - const loaded = await MedusaModule.bootstrap( - serviceKey, - "@medusajs/event-bus-local" - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: "@medusajs/event-bus-local", + }) return loaded[serviceKey] } diff --git a/packages/event-bus-redis/src/initialize/index.ts b/packages/event-bus-redis/src/initialize/index.ts index 4ffddc4dc1..b091cb9414 100644 --- a/packages/event-bus-redis/src/initialize/index.ts +++ b/packages/event-bus-redis/src/initialize/index.ts @@ -11,12 +11,13 @@ export const initialize = async ( options?: EventBusRedisModuleOptions | ExternalModuleDeclaration ): Promise => { const serviceKey = Modules.EVENT_BUS - const loaded = await MedusaModule.bootstrap( - serviceKey, - "@medusajs/event-bus-redis", - options as InternalModuleDeclaration | ExternalModuleDeclaration, - undefined - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: "@medusajs/event-bus-redis", + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + }) return loaded[serviceKey] } diff --git a/packages/inventory/src/initialize/index.ts b/packages/inventory/src/initialize/index.ts index 37cb99dea5..fa8b1b72c0 100644 --- a/packages/inventory/src/initialize/index.ts +++ b/packages/inventory/src/initialize/index.ts @@ -2,6 +2,7 @@ import { ExternalModuleDeclaration, InternalModuleDeclaration, MedusaModule, + MODULE_PACKAGE_NAMES, Modules, } from "@medusajs/modules-sdk" import { IEventBusService, IInventoryService } from "@medusajs/types" @@ -15,13 +16,15 @@ export const initialize = async ( } ): Promise => { const serviceKey = Modules.INVENTORY - const loaded = await MedusaModule.bootstrap( - serviceKey, - "@medusajs/inventory", - options as InternalModuleDeclaration | ExternalModuleDeclaration, - moduleDefinition, - injectedDependencies - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: MODULE_PACKAGE_NAMES[Modules.INVENTORY], + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + injectedDependencies, + moduleExports: moduleDefinition, + }) return loaded[serviceKey] } diff --git a/packages/link-modules/src/initialize/index.ts b/packages/link-modules/src/initialize/index.ts index 28327aacff..675bd09973 100644 --- a/packages/link-modules/src/initialize/index.ts +++ b/packages/link-modules/src/initialize/index.ts @@ -120,12 +120,12 @@ export const initialize = async ( }, } - const loaded = await MedusaModule.bootstrapLink( - linkModuleDefinition, - options as InternalModuleDeclaration, - moduleDefinition, - injectedDependencies - ) + const loaded = await MedusaModule.bootstrapLink({ + definition: linkModuleDefinition, + declaration: options as InternalModuleDeclaration, + moduleExports: moduleDefinition, + injectedDependencies, + }) allLinks[serviceKey as string] = Object.values(loaded)[0] } diff --git a/packages/medusa/src/loaders/index.ts b/packages/medusa/src/loaders/index.ts index e11d71cf23..f199b894d1 100644 --- a/packages/medusa/src/loaders/index.ts +++ b/packages/medusa/src/loaders/index.ts @@ -234,6 +234,7 @@ export default async ({ modulesConfig, servicesConfig: joinerConfig, remoteFetchData: remoteQueryFetchData(container), + sharedContainer: container, injectedDependencies: { [ContainerRegistrationKeys.PG_CONNECTION]: container.resolve( ContainerRegistrationKeys.PG_CONNECTION diff --git a/packages/modules-sdk/src/loaders/__tests__/medusa-module.ts b/packages/modules-sdk/src/loaders/__tests__/medusa-module.ts index b00d76bda3..7d08834932 100644 --- a/packages/modules-sdk/src/loaders/__tests__/medusa-module.ts +++ b/packages/modules-sdk/src/loaders/__tests__/medusa-module.ts @@ -46,35 +46,47 @@ describe("Medusa Modules", () => { }) it("should create singleton instances", async () => { - await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - options: { - abc: 123, - }, - } as InternalModuleDeclaration) + await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + options: { + abc: 123, + }, + } as InternalModuleDeclaration, + }) expect(mockRegisterMedusaModule).toBeCalledTimes(1) expect(mockModuleLoader).toBeCalledTimes(1) - await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - options: { - abc: 123, - }, - } as InternalModuleDeclaration) + await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + options: { + abc: 123, + }, + } as InternalModuleDeclaration, + }) - await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - options: { - different_options: "abc", - }, - } as InternalModuleDeclaration) + await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + options: { + different_options: "abc", + }, + } as InternalModuleDeclaration, + }) expect(mockRegisterMedusaModule).toBeCalledTimes(2) expect(mockModuleLoader).toBeCalledTimes(2) @@ -85,14 +97,18 @@ describe("Medusa Modules", () => { for (let i = 5; i--; ) { load.push( - MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - options: { - abc: 123, - }, - } as InternalModuleDeclaration) + MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + options: { + abc: 123, + }, + } as InternalModuleDeclaration, + }) ) } @@ -104,81 +120,109 @@ describe("Medusa Modules", () => { }) it("getModuleInstance should return the first instance of the module if there is none flagged as 'main'", async () => { - const moduleA = await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - options: { - abc: 123, - }, - } as InternalModuleDeclaration) + const moduleA = await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + options: { + abc: 123, + }, + } as InternalModuleDeclaration, + }) - const moduleB = await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - options: { - different_options: "abc", - }, - } as InternalModuleDeclaration) + const moduleB = await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + options: { + different_options: "abc", + }, + } as InternalModuleDeclaration, + }) expect(MedusaModule.getModuleInstance("moduleKey")).toEqual(moduleA) }) it("should return the module flagged as 'main' when multiple instances are available", async () => { - const moduleA = await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - options: { - abc: 123, - }, - } as InternalModuleDeclaration) + const moduleA = await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + options: { + abc: 123, + }, + } as InternalModuleDeclaration, + }) - const moduleB = await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - main: true, - options: { - different_options: "abc", - }, - } as InternalModuleDeclaration) + const moduleB = await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + main: true, + options: { + different_options: "abc", + }, + } as InternalModuleDeclaration, + }) expect(MedusaModule.getModuleInstance("moduleKey")).toEqual(moduleB) }) it("should retrieve the module by their given alias", async () => { - const moduleA = await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - alias: "mod_A", - options: { - abc: 123, - }, - } as InternalModuleDeclaration) + const moduleA = await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + alias: "mod_A", + options: { + abc: 123, + }, + } as InternalModuleDeclaration, + }) - const moduleB = await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - main: true, - alias: "mod_B", - options: { - different_options: "abc", - }, - } as InternalModuleDeclaration) + const moduleB = await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + main: true, + alias: "mod_B", + options: { + different_options: "abc", + }, + } as InternalModuleDeclaration, + }) - const moduleC = await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - alias: "mod_C", - options: { - moduleC: true, - }, - } as InternalModuleDeclaration) + const moduleC = await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + alias: "mod_C", + options: { + moduleC: true, + }, + } as InternalModuleDeclaration, + }) // main expect(MedusaModule.getModuleInstance("moduleKey")).toEqual(moduleB) @@ -195,37 +239,49 @@ describe("Medusa Modules", () => { }) it("should prevent two main modules being set as 'main'", async () => { - await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - alias: "mod_A", - options: { - abc: 123, - }, - } as InternalModuleDeclaration) + await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + alias: "mod_A", + options: { + abc: 123, + }, + } as InternalModuleDeclaration, + }) - await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - main: true, - alias: "mod_B", - options: { - different_options: "abc", - }, - } as InternalModuleDeclaration) + await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + main: true, + alias: "mod_B", + options: { + different_options: "abc", + }, + } as InternalModuleDeclaration, + }) - const moduleC = MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - main: true, - alias: "mod_C", - options: { - moduleC: true, - }, - } as InternalModuleDeclaration) + const moduleC = MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + main: true, + alias: "mod_C", + options: { + moduleC: true, + }, + } as InternalModuleDeclaration, + }) expect(moduleC).rejects.toThrow( "Module moduleKey already have a 'main' registered." @@ -233,25 +289,33 @@ describe("Medusa Modules", () => { }) it("should prevent the same alias be used for different instances of the same module", async () => { - await MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - alias: "module_alias", - options: { - different_options: "abc", - }, - } as InternalModuleDeclaration) + await MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + alias: "module_alias", + options: { + different_options: "abc", + }, + } as InternalModuleDeclaration, + }) - const moduleC = MedusaModule.bootstrap("moduleKey", "@path", { - scope: MODULE_SCOPE.INTERNAL, - resources: MODULE_RESOURCE_TYPE.ISOLATED, - resolve: "@path", - alias: "module_alias", - options: { - moduleC: true, - }, - } as InternalModuleDeclaration) + const moduleC = MedusaModule.bootstrap({ + moduleKey: "moduleKey", + defaultPath: "@path", + declaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.ISOLATED, + resolve: "@path", + alias: "module_alias", + options: { + moduleC: true, + }, + } as InternalModuleDeclaration, + }) expect(moduleC).rejects.toThrow( "Module moduleKey already registed as 'module_alias'. Please choose a different alias." diff --git a/packages/modules-sdk/src/medusa-app.ts b/packages/modules-sdk/src/medusa-app.ts index 0c779aecdb..41ba0a8aae 100644 --- a/packages/modules-sdk/src/medusa-app.ts +++ b/packages/modules-sdk/src/medusa-app.ts @@ -6,6 +6,7 @@ import { InternalModuleDeclaration, LoadedModule, LoaderOptions, + MedusaContainer, MODULE_RESOURCE_TYPE, MODULE_SCOPE, ModuleDefinition, @@ -15,14 +16,20 @@ import { } from "@medusajs/types" import { ContainerRegistrationKeys, - ModulesSdkUtils, + createMedusaContainer, isObject, + ModulesSdkUtils, } from "@medusajs/utils" -import { MODULE_PACKAGE_NAMES, Modules } from "./definitions" +import { + MODULE_PACKAGE_NAMES, + ModuleRegistrationName, + Modules, +} from "./definitions" import { MedusaModule } from "./medusa-module" import { RemoteLink } from "./remote-link" import { RemoteQuery } from "./remote-query" import { cleanGraphQLSchema } from "./utils" +import { asValue } from "awilix" const LinkModulePackage = "@medusajs/link-modules" @@ -57,7 +64,7 @@ export type SharedResources = { } } -async function loadModules(modulesConfig, injectedDependencies) { +async function loadModules(modulesConfig, sharedContainer) { const allModules = {} await Promise.all( @@ -85,14 +92,18 @@ async function loadModules(modulesConfig, injectedDependencies) { declaration.resources = MODULE_RESOURCE_TYPE.SHARED } - const loaded = (await MedusaModule.bootstrap( - moduleName, - path, + const loaded = (await MedusaModule.bootstrap({ + moduleKey: moduleName, + defaultPath: path, declaration, - undefined, - injectedDependencies, - definition - )) as LoadedModule + sharedContainer, + moduleDefinition: definition, + })) as LoadedModule + + const service = loaded[moduleName] + sharedContainer.register({ + [service.__definition.registrationName]: asValue(service), + }) if (allModules[moduleName] && !Array.isArray(allModules[moduleName])) { allModules[moduleName] = [] @@ -153,6 +164,7 @@ function registerCustomJoinerConfigs(servicesConfig: ModuleJoinerConfig[]) { export async function MedusaApp( { + sharedContainer, sharedResourcesConfig, servicesConfig, modulesConfigPath, @@ -162,6 +174,7 @@ export async function MedusaApp( remoteFetchData, injectedDependencies, }: { + sharedContainer?: MedusaContainer sharedResourcesConfig?: SharedResources loadedModules?: LoadedModule[] servicesConfig?: ModuleJoinerConfig[] @@ -185,6 +198,8 @@ export async function MedusaApp( notFound?: Record> runMigrations: RunMigrationFn }> { + const sharedContainer_ = createMedusaContainer({}, sharedContainer) + const modules: MedusaModuleConfig = modulesConfig ?? ( @@ -222,7 +237,20 @@ export async function MedusaApp( linkModuleOptions = linkModule } - const allModules = await loadModules(modules, injectedDependencies) + for (const injectedDependency of Object.keys(injectedDependencies)) { + sharedContainer_.register({ + [injectedDependency]: asValue(injectedDependencies[injectedDependency]), + }) + } + + const allModules = await loadModules(modules, sharedContainer_) + + // Share Event bus with link modules + injectedDependencies[ModuleRegistrationName.EVENT_BUS] = + sharedContainer_.resolve(ModuleRegistrationName.EVENT_BUS, { + allowUnregistered: true, + }) + const { remoteLink, linkResolution, diff --git a/packages/modules-sdk/src/medusa-module.ts b/packages/modules-sdk/src/medusa-module.ts index 934795898d..845232e95a 100644 --- a/packages/modules-sdk/src/medusa-module.ts +++ b/packages/modules-sdk/src/medusa-module.ts @@ -3,6 +3,7 @@ import { InternalModuleDeclaration, LinkModuleDefinition, LoadedModule, + MedusaContainer, MODULE_RESOURCE_TYPE, MODULE_SCOPE, ModuleDefinition, @@ -48,6 +49,23 @@ type ModuleAlias = { main?: boolean } +export type ModuleBootstrapOptions = { + moduleKey: string + defaultPath: string + declaration?: InternalModuleDeclaration | ExternalModuleDeclaration + moduleExports?: ModuleExports + sharedContainer?: MedusaContainer + moduleDefinition?: ModuleDefinition + injectedDependencies?: Record +} + +export type LinkModuleBootstrapOptions = { + definition: LinkModuleDefinition + declaration?: InternalModuleDeclaration + moduleExports?: ModuleExports + injectedDependencies?: Record +} + export class MedusaModule { private static instances_: Map = new Map() private static modules_: Map = new Map() @@ -146,14 +164,15 @@ export class MedusaModule { MedusaModule.modules_.set(moduleKey, modules!) } - public static async bootstrap( - moduleKey: string, - defaultPath: string, - declaration?: InternalModuleDeclaration | ExternalModuleDeclaration, - moduleExports?: ModuleExports, - injectedDependencies?: Record, - moduleDefinition?: ModuleDefinition - ): Promise<{ + public static async bootstrap({ + moduleKey, + defaultPath, + declaration, + moduleExports, + sharedContainer, + moduleDefinition, + injectedDependencies, + }: ModuleBootstrapOptions): Promise<{ [key: string]: T }> { const hashKey = simpleHash( @@ -193,11 +212,14 @@ export class MedusaModule { } } - const container = createMedusaContainer() + const container = createMedusaContainer({}, sharedContainer) if (injectedDependencies) { for (const service in injectedDependencies) { container.register(service, asValue(injectedDependencies[service])) + if (!container.hasRegistration(service)) { + container.register(service, asValue(injectedDependencies[service])) + } } } @@ -255,19 +277,19 @@ export class MedusaModule { return services } - public static async bootstrapLink( - definition: LinkModuleDefinition, - declaration?: InternalModuleDeclaration, - moduleExports?: ModuleExports, - injectedDependencies?: Record - ): Promise<{ + public static async bootstrapLink({ + definition, + declaration, + moduleExports, + injectedDependencies, + }: LinkModuleBootstrapOptions): Promise<{ [key: string]: unknown }> { const moduleKey = definition.key const hashKey = simpleHash(stringifyCircular({ moduleKey, declaration })) if (MedusaModule.instances_.has(hashKey)) { - return MedusaModule.instances_.get(hashKey) + return { [moduleKey]: MedusaModule.instances_.get(hashKey) } } if (MedusaModule.loading_.has(hashKey)) { diff --git a/packages/modules-sdk/src/remote-query.ts b/packages/modules-sdk/src/remote-query.ts index 3a80ed26b6..6db10af712 100644 --- a/packages/modules-sdk/src/remote-query.ts +++ b/packages/modules-sdk/src/remote-query.ts @@ -34,6 +34,8 @@ export class RemoteQuery { ) } + const servicesConfig_ = [...servicesConfig] + for (const mod of modulesLoaded) { if (!mod.__definition.isQueryable) { continue @@ -48,12 +50,12 @@ export class RemoteQuery { } this.modulesMap.set(serviceName, mod) - servicesConfig!.push(mod.__joinerConfig) + servicesConfig_!.push(mod.__joinerConfig) } this.customRemoteFetchData = customRemoteFetchData this.remoteJoiner = new RemoteJoiner( - servicesConfig as JoinerServiceConfig[], + servicesConfig_ as JoinerServiceConfig[], this.remoteFetchData.bind(this) ) } diff --git a/packages/pricing/src/initialize/index.ts b/packages/pricing/src/initialize/index.ts index 1295c2910e..0f35ee616e 100644 --- a/packages/pricing/src/initialize/index.ts +++ b/packages/pricing/src/initialize/index.ts @@ -19,13 +19,15 @@ export const initialize = async ( ): Promise => { const serviceKey = Modules.PRICING - const loaded = await MedusaModule.bootstrap( - serviceKey, - MODULE_PACKAGE_NAMES[Modules.PRICING], - options as InternalModuleDeclaration | ExternalModuleDeclaration, - moduleDefinition, - injectedDependencies - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: MODULE_PACKAGE_NAMES[Modules.PRICING], + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + injectedDependencies, + moduleExports: moduleDefinition, + }) return loaded[serviceKey] } diff --git a/packages/product/src/initialize/index.ts b/packages/product/src/initialize/index.ts index c231243009..ef9d4e9503 100644 --- a/packages/product/src/initialize/index.ts +++ b/packages/product/src/initialize/index.ts @@ -1,8 +1,8 @@ import { ExternalModuleDeclaration, InternalModuleDeclaration, - MODULE_PACKAGE_NAMES, MedusaModule, + MODULE_PACKAGE_NAMES, Modules, } from "@medusajs/modules-sdk" import { IProductModuleService, ModulesSdkTypes } from "@medusajs/types" @@ -20,13 +20,15 @@ export const initialize = async ( ): Promise => { const serviceKey = Modules.PRODUCT - const loaded = await MedusaModule.bootstrap( - serviceKey, - MODULE_PACKAGE_NAMES[Modules.PRODUCT], - options as InternalModuleDeclaration | ExternalModuleDeclaration, - moduleDefinition, - injectedDependencies - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: MODULE_PACKAGE_NAMES[Modules.PRODUCT], + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + injectedDependencies, + moduleExports: moduleDefinition, + }) return loaded[serviceKey] } diff --git a/packages/stock-location/src/initialize/index.ts b/packages/stock-location/src/initialize/index.ts index 355b4a845f..56842628c8 100644 --- a/packages/stock-location/src/initialize/index.ts +++ b/packages/stock-location/src/initialize/index.ts @@ -2,6 +2,7 @@ import { ExternalModuleDeclaration, InternalModuleDeclaration, MedusaModule, + MODULE_PACKAGE_NAMES, Modules, } from "@medusajs/modules-sdk" import { IEventBusService, IStockLocationService } from "@medusajs/types" @@ -15,13 +16,15 @@ export const initialize = async ( } ): Promise => { const serviceKey = Modules.STOCK_LOCATION - const loaded = await MedusaModule.bootstrap( - serviceKey, - "@medusajs/stock-location", - options as InternalModuleDeclaration | ExternalModuleDeclaration, - moduleDefinition, - injectedDependencies - ) + const loaded = await MedusaModule.bootstrap({ + moduleKey: serviceKey, + defaultPath: MODULE_PACKAGE_NAMES[Modules.STOCK_LOCATION], + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + injectedDependencies, + moduleExports: moduleDefinition, + }) return loaded[serviceKey] }