diff --git a/packages/backend/src/plugins/search.ts b/packages/backend/src/plugins/search.ts index bde67360c7..fa97ead727 100644 --- a/packages/backend/src/plugins/search.ts +++ b/packages/backend/src/plugins/search.ts @@ -14,23 +14,21 @@ * limitations under the License. */ import { createRouter } from '@backstage/plugin-search-backend'; -import { - collateDocuments, - // registerCollator, -} from '@backstage/plugin-search-backend-node'; +import { Registry } from '@backstage/plugin-search-backend-node'; import { PluginEnvironment } from '../types'; // import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend'; export default async function createPlugin({ logger }: PluginEnvironment) { + const indexRegistry = new Registry(); // TODO: Within this PR, update to use REST API instead of Catalog Builder. - /* registerCollator({ + /* indexRegistry.addCollator({ type: 'software-catalog', defaultRefreshIntervalSeconds: 600, collator: new DefaultCatalogCollator(entitiesCatalog), });*/ // TODO: Make this a more proper refresh loop. - collateDocuments(); + indexRegistry.execute(); return await createRouter({ logger, diff --git a/plugins/search-backend-node/src/index.test.ts b/plugins/search-backend-node/src/index.test.ts index 01a4d9a486..22a67af20b 100644 --- a/plugins/search-backend-node/src/index.test.ts +++ b/plugins/search-backend-node/src/index.test.ts @@ -19,7 +19,6 @@ import { DocumentDecorator, IndexableDocument, } from '@backstage/search-common'; -import { registerCollator, registerDecorator } from './'; import { Registry } from './registry'; class TestDocumentCollator implements DocumentCollator { @@ -35,31 +34,29 @@ class TestDocumentDecorator implements DocumentDecorator { } describe('search indexer external api', () => { + let testRegistry: Registry; let testCollator: DocumentCollator; let testDecorator: DocumentDecorator; beforeEach(() => { + testRegistry = new Registry(); testCollator = new TestDocumentCollator(); testDecorator = new TestDocumentDecorator(); }); - afterEach(() => { - Registry.getInstance()._reset(); - }); - describe('registerCollator', () => { it('registers a collator', async () => { const collatorSpy = jest.spyOn(testCollator, 'execute'); // Register a collator. - registerCollator({ + testRegistry.addCollator({ type: 'anything', defaultRefreshIntervalSeconds: 600, collator: testCollator, }); // Execute the registry and ensure the collator was invoked. - await Registry.getInstance().execute(); + await testRegistry.execute(); expect(collatorSpy).toHaveBeenCalled(); }); }); @@ -69,19 +66,19 @@ describe('search indexer external api', () => { const decoratorSpy = jest.spyOn(testDecorator, 'execute'); // Register a collator. - registerCollator({ + testRegistry.addCollator({ type: 'anything', defaultRefreshIntervalSeconds: 600, collator: testCollator, }); // Register a decorator. - registerDecorator({ + testRegistry.addDecorator({ decorator: testDecorator, }); // Execute the registry and ensure the decorator was invoked. - await Registry.getInstance().execute(); + await testRegistry.execute(); expect(decoratorSpy).toHaveBeenCalled(); }); @@ -98,20 +95,20 @@ describe('search indexer external api', () => { const decoratorSpy = jest.spyOn(testDecorator, 'execute'); // Register a collator. - registerCollator({ + testRegistry.addCollator({ type: expectedType, defaultRefreshIntervalSeconds: 600, collator: testCollator, }); // Register a decorator for the same type. - registerDecorator({ + testRegistry.addDecorator({ types: [expectedType], decorator: testDecorator, }); // Execute the registry and ensure the decorator was invoked. - await Registry.getInstance().execute(); + await testRegistry.execute(); expect(decoratorSpy).toHaveBeenCalled(); expect(decoratorSpy).toHaveBeenCalledWith([docFixture]); }); @@ -129,20 +126,20 @@ describe('search indexer external api', () => { const decoratorSpy = jest.spyOn(testDecorator, 'execute'); // Register a collator. - registerCollator({ + testRegistry.addCollator({ type: expectedType, defaultRefreshIntervalSeconds: 600, collator: testCollator, }); // Register a decorator for a different type. - registerDecorator({ + testRegistry.addDecorator({ types: ['not-the-expected-type'], decorator: testDecorator, }); // Execute the registry and ensure the decorator was not invoked. - await Registry.getInstance().execute(); + await testRegistry.execute(); expect(decoratorSpy).not.toHaveBeenCalled(); }); }); diff --git a/plugins/search-backend-node/src/index.ts b/plugins/search-backend-node/src/index.ts index f016068db1..af6811e202 100644 --- a/plugins/search-backend-node/src/index.ts +++ b/plugins/search-backend-node/src/index.ts @@ -14,19 +14,4 @@ * limitations under the License. */ -import { Registry } from './registry'; -import { - RegisterCollatorParameters, - RegisterDecoratorParameters, -} from './types'; - -const registry = Registry.getInstance(); -export const registerCollator = (params: RegisterCollatorParameters) => { - registry.addCollator(params); -}; -export const registerDecorator = (params: RegisterDecoratorParameters) => { - registry.addDecorator(params); -}; -export const collateDocuments = () => { - registry.execute(); -}; +export { Registry } from './registry'; diff --git a/plugins/search-backend-node/src/registry.ts b/plugins/search-backend-node/src/registry.ts index 3c17ae5569..d62e9d1090 100644 --- a/plugins/search-backend-node/src/registry.ts +++ b/plugins/search-backend-node/src/registry.ts @@ -29,20 +29,11 @@ export class Registry { private collators: Record; private decorators: Record; - private static instance: Registry; - - private constructor() { + constructor() { this.collators = {}; this.decorators = {}; } - static getInstance(): Registry { - if (!Registry.instance) { - Registry.instance = new Registry(); - } - return Registry.instance; - } - addCollator({ type, collator, @@ -84,13 +75,4 @@ export class Registry { }), ); } - - /** - * Utility method for tests. Do not use otherwise. - * @private - */ - _reset() { - this.collators = {}; - this.decorators = {}; - } }