Browse Source

refactor(core): Update SearchService to work with new plugins

Michael Bromley 6 years ago
parent
commit
8aefc93b39

+ 4 - 11
packages/core/src/plugin/default-search-plugin/default-search-plugin.ts

@@ -1,4 +1,3 @@
-import { OnApplicationBootstrap } from '@nestjs/common';
 import { SearchReindexResponse } from '@vendure/common/lib/generated-types';
 import { SearchReindexResponse } from '@vendure/common/lib/generated-types';
 
 
 import { idsAreEqual } from '../../common/utils';
 import { idsAreEqual } from '../../common/utils';
@@ -8,9 +7,8 @@ import { EventBus } from '../../event-bus/event-bus';
 import { CatalogModificationEvent } from '../../event-bus/events/catalog-modification-event';
 import { CatalogModificationEvent } from '../../event-bus/events/catalog-modification-event';
 import { CollectionModificationEvent } from '../../event-bus/events/collection-modification-event';
 import { CollectionModificationEvent } from '../../event-bus/events/collection-modification-event';
 import { TaxRateModificationEvent } from '../../event-bus/events/tax-rate-modification-event';
 import { TaxRateModificationEvent } from '../../event-bus/events/tax-rate-modification-event';
-import { SearchService } from '../../service/services/search.service';
 import { PluginCommonModule } from '../plugin-common.module';
 import { PluginCommonModule } from '../plugin-common.module';
-import { VendurePlugin } from '../vendure-plugin';
+import { OnVendureBootstrap, VendurePlugin } from '../vendure-plugin';
 
 
 import { AdminFulltextSearchResolver, ShopFulltextSearchResolver } from './fulltext-search.resolver';
 import { AdminFulltextSearchResolver, ShopFulltextSearchResolver } from './fulltext-search.resolver';
 import { FulltextSearchService } from './fulltext-search.service';
 import { FulltextSearchService } from './fulltext-search.service';
@@ -54,22 +52,17 @@ export interface DefaultSearchReindexResponse extends SearchReindexResponse {
  */
  */
 @VendurePlugin({
 @VendurePlugin({
     imports: [PluginCommonModule],
     imports: [PluginCommonModule],
-    providers: [
-        FulltextSearchService,
-        SearchIndexService,
-        { provide: SearchService, useClass: FulltextSearchService },
-    ],
-    exports: [{ provide: SearchService, useClass: FulltextSearchService }],
+    providers: [FulltextSearchService, SearchIndexService],
     adminApiExtensions: { resolvers: [AdminFulltextSearchResolver] },
     adminApiExtensions: { resolvers: [AdminFulltextSearchResolver] },
     shopApiExtensions: { resolvers: [ShopFulltextSearchResolver] },
     shopApiExtensions: { resolvers: [ShopFulltextSearchResolver] },
     entities: [SearchIndexItem],
     entities: [SearchIndexItem],
     workers: [IndexerController],
     workers: [IndexerController],
 })
 })
-export class DefaultSearchPlugin implements OnApplicationBootstrap {
+export class DefaultSearchPlugin implements OnVendureBootstrap {
     constructor(private eventBus: EventBus, private searchIndexService: SearchIndexService) {}
     constructor(private eventBus: EventBus, private searchIndexService: SearchIndexService) {}
 
 
     /** @internal */
     /** @internal */
-    onApplicationBootstrap() {
+    async onVendureBootstrap() {
         this.eventBus.subscribe(CatalogModificationEvent, event => {
         this.eventBus.subscribe(CatalogModificationEvent, event => {
             if (event.entity instanceof Product || event.entity instanceof ProductVariant) {
             if (event.entity instanceof Product || event.entity instanceof ProductVariant) {
                 return this.searchIndexService.updateProductOrVariant(event.ctx, event.entity).start();
                 return this.searchIndexService.updateProductOrVariant(event.ctx, event.entity).start();

+ 8 - 2
packages/core/src/plugin/default-search-plugin/fulltext-search.service.ts

@@ -24,7 +24,7 @@ import { SqliteSearchStrategy } from './search-strategy/sqlite-search-strategy';
  * SearchStrategy implementations for db-specific code.
  * SearchStrategy implementations for db-specific code.
  */
  */
 @Injectable()
 @Injectable()
-export class FulltextSearchService implements SearchService {
+export class FulltextSearchService {
     private searchStrategy: SearchStrategy;
     private searchStrategy: SearchStrategy;
     private readonly minTermLength = 2;
     private readonly minTermLength = 2;
 
 
@@ -35,14 +35,20 @@ export class FulltextSearchService implements SearchService {
         private facetValueService: FacetValueService,
         private facetValueService: FacetValueService,
         private productVariantService: ProductVariantService,
         private productVariantService: ProductVariantService,
         private searchIndexService: SearchIndexService,
         private searchIndexService: SearchIndexService,
+        private searchService: SearchService,
     ) {
     ) {
+        this.searchService.adopt(this);
         this.setSearchStrategy();
         this.setSearchStrategy();
     }
     }
 
 
     /**
     /**
      * Perform a fulltext search according to the provided input arguments.
      * Perform a fulltext search according to the provided input arguments.
      */
      */
-    async search(ctx: RequestContext, input: SearchInput, enabledOnly: boolean = false): Promise<Omit<SearchResponse, 'facetValues'>> {
+    async search(
+        ctx: RequestContext,
+        input: SearchInput,
+        enabledOnly: boolean = false,
+    ): Promise<Omit<SearchResponse, 'facetValues'>> {
         const items = await this.searchStrategy.getSearchResults(ctx, input, enabledOnly);
         const items = await this.searchStrategy.getSearchResults(ctx, input, enabledOnly);
         const totalItems = await this.searchStrategy.getTotalCount(ctx, input, enabledOnly);
         const totalItems = await this.searchStrategy.getTotalCount(ctx, input, enabledOnly);
         return {
         return {

+ 13 - 0
packages/core/src/service/services/search.service.ts

@@ -17,7 +17,20 @@ import { Logger } from '../../config/logger/vendure-logger';
  */
  */
 @Injectable()
 @Injectable()
 export class SearchService {
 export class SearchService {
+    private override: Pick<SearchService, 'reindex'> | undefined;
+
+    /**
+     * Adopt a concrete search service implementation to pass through the
+     * calls to.
+     */
+    adopt(override: Pick<SearchService, 'reindex'>) {
+        this.override = override;
+    }
+
     async reindex(ctx: RequestContext): Promise<JobInfo> {
     async reindex(ctx: RequestContext): Promise<JobInfo> {
+        if (this.override) {
+            return this.override.reindex(ctx);
+        }
         if (!process.env.CI) {
         if (!process.env.CI) {
             Logger.warn(`The SearchService should be overridden by an appropriate search plugin.`);
             Logger.warn(`The SearchService should be overridden by an appropriate search plugin.`);
         }
         }