From baeacd1cc52c548eef6896fd83e606c858cf2165 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Sat, 7 Jan 2023 15:58:06 +0100 Subject: [PATCH] feat(medusa): Disable subscriber when no search engine is installed (#2958) --- .changeset/slimy-candles-wait.md | 5 +++++ packages/medusa/src/loaders/plugins.ts | 4 ++++ packages/medusa/src/subscribers/product.js | 18 ++++++++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 .changeset/slimy-candles-wait.md diff --git a/.changeset/slimy-candles-wait.md b/.changeset/slimy-candles-wait.md new file mode 100644 index 0000000000..bf0e6a1871 --- /dev/null +++ b/.changeset/slimy-candles-wait.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +feat: Deactivate search service product subscribers when search engine not enabled diff --git a/packages/medusa/src/loaders/plugins.ts b/packages/medusa/src/loaders/plugins.ts index a031137989..ed17715704 100644 --- a/packages/medusa/src/loaders/plugins.ts +++ b/packages/medusa/src/loaders/plugins.ts @@ -52,6 +52,8 @@ type PluginDetails = { version: string } +export const isSearchEngineInstalledResolutionKey = "isSearchEngineInstalled" + /** * Registers all services in the services directory */ @@ -447,6 +449,8 @@ export async function registerServices( ), [`searchService`]: aliasTo(name), }) + + container.register(isSearchEngineInstalledResolutionKey, asValue(true)) } else if (loaded.prototype instanceof AbstractTaxService) { container.registerAdd( "taxProviders", diff --git a/packages/medusa/src/subscribers/product.js b/packages/medusa/src/subscribers/product.js index 971fafb372..a04dde7e4c 100644 --- a/packages/medusa/src/subscribers/product.js +++ b/packages/medusa/src/subscribers/product.js @@ -1,6 +1,7 @@ import ProductVariantService from "../services/product-variant" import ProductService from "../services/product" import { indexTypes } from "medusa-core-utils" +import { isSearchEngineInstalledResolutionKey } from "../loaders/plugins" const searchFields = [ "id", @@ -31,12 +32,21 @@ const searchRelations = [ ] class ProductSearchSubscriber { - constructor({ eventBusService, searchService, productService }) { - this.eventBus_ = eventBusService + constructor(container) { + this.eventBus_ = container.eventBusService + this.searchService_ = container.searchService + this.productService_ = container.productService - this.searchService_ = searchService + /** + * Do not subscribe to any event in case no search engine have been installed. + * If some events need to be subscribed out of the search engine reason, they can be subscribed above this comment + */ - this.productService_ = productService + try { + container[isSearchEngineInstalledResolutionKey] + } catch (e) { + return this + } this.eventBus_.subscribe( ProductService.Events.CREATED,