From 4cc252bdd5b5e404fe54383f1a220e24c93afbae Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Mon, 6 Mar 2023 11:22:17 +0100 Subject: [PATCH] use alpha exports Signed-off-by: Emma Indal Co-authored-by: Camila Loiola --- packages/backend-next/src/index.ts | 2 +- packages/backend-next/src/plugins/search.ts | 2 +- .../package.json | 19 +++++- .../src/alpha.ts | 63 +++++++++++++++++++ .../src/engines/ElasticSearchSearchEngine.ts | 47 +------------- .../src/engines/index.ts | 1 - .../src/index.ts | 1 - plugins/search-backend-module-pg/package.json | 20 +++++- .../src/PgSearchEngine/PgSearchEngine.ts | 26 -------- .../src/PgSearchEngine/index.ts | 2 +- plugins/search-backend-module-pg/src/alpha.ts | 42 +++++++++++++ plugins/search-backend-node/package.json | 19 +++++- .../src/{services.ts => alpha.ts} | 51 +++++++++++++-- .../src/engines/LunrSearchEngine.ts | 2 +- plugins/search-backend-node/src/index.ts | 6 -- plugins/search-backend-node/src/types.ts | 23 ------- plugins/search-backend/package.json | 19 +++++- .../src/alpha.ts} | 16 +---- plugins/search-backend/src/index.ts | 1 - plugins/search-backend/src/plugin.ts | 12 ++-- yarn.lock | 1 + 21 files changed, 229 insertions(+), 146 deletions(-) create mode 100644 plugins/search-backend-module-elasticsearch/src/alpha.ts create mode 100644 plugins/search-backend-module-pg/src/alpha.ts rename plugins/search-backend-node/src/{services.ts => alpha.ts} (67%) rename plugins/{search-backend-node/src/extensions.ts => search-backend/src/alpha.ts} (56%) diff --git a/packages/backend-next/src/index.ts b/packages/backend-next/src/index.ts index cf275ae3ad..10680ca798 100644 --- a/packages/backend-next/src/index.ts +++ b/packages/backend-next/src/index.ts @@ -20,7 +20,7 @@ import { createBackend } from '@backstage/backend-defaults'; import { appPlugin } from '@backstage/plugin-app-backend/alpha'; import { todoPlugin } from '@backstage/plugin-todo-backend'; import { techdocsPlugin } from '@backstage/plugin-techdocs-backend/alpha'; -import { searchPlugin } from '@backstage/plugin-search-backend'; +import { searchPlugin } from '@backstage/plugin-search-backend/alpha'; import { searchIndexRegistry } from './plugins/search'; const backend = createBackend(); diff --git a/packages/backend-next/src/plugins/search.ts b/packages/backend-next/src/plugins/search.ts index 559e6a9cd1..1329135e56 100644 --- a/packages/backend-next/src/plugins/search.ts +++ b/packages/backend-next/src/plugins/search.ts @@ -17,7 +17,7 @@ import { coreServices, createBackendModule, } from '@backstage/backend-plugin-api'; -import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node'; +import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; import { DefaultCatalogCollatorFactory } from '@backstage/plugin-catalog-backend'; import { DefaultTechDocsCollatorFactory } from '@backstage/plugin-techdocs-backend'; import { ToolDocumentCollatorFactory } from '@backstage/plugin-explore-backend'; diff --git a/plugins/search-backend-module-elasticsearch/package.json b/plugins/search-backend-module-elasticsearch/package.json index aff861b784..3c16bd1a7d 100644 --- a/plugins/search-backend-module-elasticsearch/package.json +++ b/plugins/search-backend-module-elasticsearch/package.json @@ -6,9 +6,22 @@ "types": "src/index.ts", "license": "Apache-2.0", "publishConfig": { - "access": "public", - "main": "dist/index.cjs.js", - "types": "dist/index.d.ts" + "access": "public" + }, + "exports": { + ".": "./src/index.ts", + "./alpha": "./src/alpha.ts", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "alpha": [ + "src/alpha.ts" + ], + "package.json": [ + "package.json" + ] + } }, "backstage": { "role": "backend-plugin-module" diff --git a/plugins/search-backend-module-elasticsearch/src/alpha.ts b/plugins/search-backend-module-elasticsearch/src/alpha.ts new file mode 100644 index 0000000000..ad83919ee4 --- /dev/null +++ b/plugins/search-backend-module-elasticsearch/src/alpha.ts @@ -0,0 +1,63 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { searchEngineRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; +import { + coreServices, + createBackendModule, +} from '@backstage/backend-plugin-api'; +import { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + ElasticSearchCustomIndexTemplate, + ElasticSearchQueryTranslator, + ElasticSearchSearchEngine, +} from './engines'; + +export type ElasticSearchEngineModuleOptions = { + translator?: ElasticSearchQueryTranslator; + indexTemplate?: ElasticSearchCustomIndexTemplate; +}; + +export const elasticSearchEngineModule = createBackendModule( + (options?: ElasticSearchEngineModuleOptions) => ({ + moduleId: 'elasticSearchEngineModule', + pluginId: 'search', + register(env) { + env.registerInit({ + deps: { + searchEngineRegistry: searchEngineRegistryExtensionPoint, + logger: coreServices.logger, + config: coreServices.config, + }, + async init({ searchEngineRegistry, logger, config }) { + const searchEngine = await ElasticSearchSearchEngine.fromConfig({ + logger: loggerToWinstonLogger(logger), + config: config, + }); + searchEngineRegistry.setSearchEngine(searchEngine); + // set custom translator if available + if (options?.translator) { + searchEngine.setTranslator(options.translator); + } + + // set custom index template if available + if (options?.indexTemplate) { + searchEngine.setIndexTemplate(options.indexTemplate); + } + }, + }); + }, + }), +); diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts index f368799673..25df3101de 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts @@ -30,17 +30,9 @@ import { ElasticSearchClientWrapper } from './ElasticSearchClientWrapper'; import { ElasticSearchCustomIndexTemplate } from './types'; import { ElasticSearchSearchEngineIndexer } from './ElasticSearchSearchEngineIndexer'; import { Logger } from 'winston'; -import { - MissingIndexError, - searchEngineRegistryExtensionPoint, -} from '@backstage/plugin-search-backend-node'; +import { MissingIndexError } from '@backstage/plugin-search-backend-node'; import esb from 'elastic-builder'; import { v4 as uuid } from 'uuid'; -import { - coreServices, - createBackendModule, -} from '@backstage/backend-plugin-api'; -import { loggerToWinstonLogger } from '@backstage/backend-common'; export type { ElasticSearchClientOptions }; @@ -529,40 +521,3 @@ export async function createElasticSearchClientOptions( : {}), }; } - -export type ElasticSearchEngineModuleOptions = { - translator?: ElasticSearchQueryTranslator; - indexTemplate?: ElasticSearchCustomIndexTemplate; -}; - -export const elasticSearchEngineModule = createBackendModule( - (options?: ElasticSearchEngineModuleOptions) => ({ - moduleId: 'elasticSearchEngineModule', - pluginId: 'search', - register(env) { - env.registerInit({ - deps: { - searchEngineRegistry: searchEngineRegistryExtensionPoint, - logger: coreServices.logger, - config: coreServices.config, - }, - async init({ searchEngineRegistry, logger, config }) { - const searchEngine = await ElasticSearchSearchEngine.fromConfig({ - logger: loggerToWinstonLogger(logger), - config: config, - }); - searchEngineRegistry.setSearchEngine(searchEngine); - // set custom translator if available - if (options?.translator) { - searchEngine.setTranslator(options.translator); - } - - // set custom index template if available - if (options?.indexTemplate) { - searchEngine.setIndexTemplate(options.indexTemplate); - } - }, - }); - }, - }), -); diff --git a/plugins/search-backend-module-elasticsearch/src/engines/index.ts b/plugins/search-backend-module-elasticsearch/src/engines/index.ts index 01f77034a0..cb24bdc56a 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/index.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/index.ts @@ -17,7 +17,6 @@ export { decodePageCursor as decodeElasticSearchPageCursor, ElasticSearchSearchEngine, - elasticSearchEngineModule, } from './ElasticSearchSearchEngine'; export { isOpenSearchCompatible } from './ElasticSearchClientOptions'; export type { diff --git a/plugins/search-backend-module-elasticsearch/src/index.ts b/plugins/search-backend-module-elasticsearch/src/index.ts index acff595246..e0b05014cc 100644 --- a/plugins/search-backend-module-elasticsearch/src/index.ts +++ b/plugins/search-backend-module-elasticsearch/src/index.ts @@ -23,7 +23,6 @@ export { decodeElasticSearchPageCursor, ElasticSearchSearchEngine, - elasticSearchEngineModule, isOpenSearchCompatible, } from './engines'; export type { diff --git a/plugins/search-backend-module-pg/package.json b/plugins/search-backend-module-pg/package.json index b284557369..be1762394d 100644 --- a/plugins/search-backend-module-pg/package.json +++ b/plugins/search-backend-module-pg/package.json @@ -6,9 +6,22 @@ "types": "src/index.ts", "license": "Apache-2.0", "publishConfig": { - "access": "public", - "main": "dist/index.cjs.js", - "types": "dist/index.d.ts" + "access": "public" + }, + "exports": { + ".": "./src/index.ts", + "./alpha": "./src/alpha.ts", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "alpha": [ + "src/alpha.ts" + ], + "package.json": [ + "package.json" + ] + } }, "backstage": { "role": "backend-plugin-module" @@ -24,6 +37,7 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", "@backstage/plugin-search-backend-node": "workspace:^", "@backstage/plugin-search-common": "workspace:^", diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts index 845c12fa3d..d2cf80e0ca 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts @@ -30,11 +30,6 @@ import { import { v4 as uuid } from 'uuid'; import { Logger } from 'winston'; import { Config } from '@backstage/config'; -import { - coreServices, - createBackendModule, -} from '@backstage/backend-plugin-api'; -import { searchEngineRegistryExtensionPoint } from '@backstage/plugin-search-backend-node'; /** * Search query that the Postgres search engine understands. @@ -249,24 +244,3 @@ export function decodePageCursor(pageCursor?: string): { page: number } { export function encodePageCursor({ page }: { page: number }): string { return Buffer.from(`${page}`, 'utf-8').toString('base64'); } - -export const pgSearchEngineModule = createBackendModule({ - moduleId: 'pgSearchEngineModule', - pluginId: 'search', - register(env) { - env.registerInit({ - deps: { - searchEngineRegistry: searchEngineRegistryExtensionPoint, - database: coreServices.database, - config: coreServices.config, - }, - async init({ searchEngineRegistry, database, config }) { - searchEngineRegistry.setSearchEngine( - await PgSearchEngine.fromConfig(config, { - database: database, - }), - ); - }, - }); - }, -}); diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts index 7a90d9e3f7..e62e709f26 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -export { PgSearchEngine, pgSearchEngineModule } from './PgSearchEngine'; +export { PgSearchEngine } from './PgSearchEngine'; export type { ConcretePgSearchQuery, PgSearchQueryTranslatorOptions, diff --git a/plugins/search-backend-module-pg/src/alpha.ts b/plugins/search-backend-module-pg/src/alpha.ts new file mode 100644 index 0000000000..bd12b4f118 --- /dev/null +++ b/plugins/search-backend-module-pg/src/alpha.ts @@ -0,0 +1,42 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + coreServices, + createBackendModule, +} from '@backstage/backend-plugin-api'; +import { searchEngineRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; +import { PgSearchEngine } from './PgSearchEngine'; + +export const pgSearchEngineModule = createBackendModule({ + moduleId: 'pgSearchEngineModule', + pluginId: 'search', + register(env) { + env.registerInit({ + deps: { + searchEngineRegistry: searchEngineRegistryExtensionPoint, + database: coreServices.database, + config: coreServices.config, + }, + async init({ searchEngineRegistry, database, config }) { + searchEngineRegistry.setSearchEngine( + await PgSearchEngine.fromConfig(config, { + database: database, + }), + ); + }, + }); + }, +}); diff --git a/plugins/search-backend-node/package.json b/plugins/search-backend-node/package.json index d77551ef29..869f008de1 100644 --- a/plugins/search-backend-node/package.json +++ b/plugins/search-backend-node/package.json @@ -6,9 +6,22 @@ "types": "src/index.ts", "license": "Apache-2.0", "publishConfig": { - "access": "public", - "main": "dist/index.cjs.js", - "types": "dist/index.d.ts" + "access": "public" + }, + "exports": { + ".": "./src/index.ts", + "./alpha": "./src/alpha.ts", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "alpha": [ + "src/alpha.ts" + ], + "package.json": [ + "package.json" + ] + } }, "backstage": { "role": "node-library" diff --git a/plugins/search-backend-node/src/services.ts b/plugins/search-backend-node/src/alpha.ts similarity index 67% rename from plugins/search-backend-node/src/services.ts rename to plugins/search-backend-node/src/alpha.ts index 7a39184cf8..b2ca6570e1 100644 --- a/plugins/search-backend-node/src/services.ts +++ b/plugins/search-backend-node/src/alpha.ts @@ -15,19 +15,42 @@ */ import { Logger } from 'winston'; + import { createServiceRef, createServiceFactory, coreServices, } from '@backstage/backend-plugin-api'; -import { DocumentTypeInfo } from '@backstage/plugin-search-common'; +import { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + DocumentTypeInfo, + SearchEngine, +} from '@backstage/plugin-search-common'; +import { createExtensionPoint } from '@backstage/backend-plugin-api'; + +import { + RegisterCollatorParameters, + RegisterDecoratorParameters, +} from './types'; + import { IndexBuilder } from './IndexBuilder'; import { Scheduler } from './Scheduler'; -import { - IndexBuilderServiceBuildOptions, - SearchIndexBuilderService, -} from './types'; -import { loggerToWinstonLogger } from '@backstage/backend-common'; + +export interface SearchIndexBuilderService { + build(options: IndexBuilderServiceBuildOptions): Promise<{ + scheduler: Scheduler; + }>; + getDocumentTypes(): Record; +} + +export interface SearchIndexRegistryExtensionPoint { + addCollator(options: RegisterCollatorParameters): void; + addDecorator(options: RegisterDecoratorParameters): void; +} + +export interface SearchEngineRegistryExtensionPoint { + setSearchEngine(searchEngine: SearchEngine): void; +} type DefaultSearchIndexBuilderServiceOptions = { logger: Logger; @@ -85,3 +108,19 @@ export const searchIndexBuilderService = }, }), }); + +export const searchEngineRegistryExtensionPoint = + createExtensionPoint({ + id: 'search.engine.registry', + }); + +export const searchIndexRegistryExtensionPoint = + createExtensionPoint({ + id: 'search.index.registry', + }); + +export type IndexBuilderServiceBuildOptions = { + searchEngine: SearchEngine; + collators: RegisterCollatorParameters[]; + decorators: RegisterDecoratorParameters[]; +}; diff --git a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts index 75875dc944..57fe123ede 100644 --- a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts +++ b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts @@ -30,7 +30,7 @@ import { coreServices, createBackendModule, } from '@backstage/backend-plugin-api'; -import { searchEngineRegistryExtensionPoint } from '../extensions'; +import { searchEngineRegistryExtensionPoint } from '../alpha'; import { loggerToWinstonLogger } from '@backstage/backend-common'; /** diff --git a/plugins/search-backend-node/src/index.ts b/plugins/search-backend-node/src/index.ts index 7395036672..140c0a22f8 100644 --- a/plugins/search-backend-node/src/index.ts +++ b/plugins/search-backend-node/src/index.ts @@ -33,15 +33,9 @@ export type { IndexBuilderOptions, RegisterCollatorParameters, RegisterDecoratorParameters, - SearchIndexRegistryExtensionPoint, - SearchEngineRegistryExtensionPoint, } from './types'; export * from './errors'; export * from './indexing'; export * from './test-utils'; export type { ScheduleTaskParameters } from './Scheduler'; - -// TODO: export as alfa subpath -export * from './services'; -export * from './extensions'; diff --git a/plugins/search-backend-node/src/types.ts b/plugins/search-backend-node/src/types.ts index e280e1a7ef..dfcf4d12ce 100644 --- a/plugins/search-backend-node/src/types.ts +++ b/plugins/search-backend-node/src/types.ts @@ -18,11 +18,9 @@ import { TaskRunner } from '@backstage/backend-tasks'; import { DocumentCollatorFactory, DocumentDecoratorFactory, - DocumentTypeInfo, SearchEngine, } from '@backstage/plugin-search-common'; import { Logger } from 'winston'; -import { Scheduler } from './Scheduler'; /** * Options required to instantiate the index builder. @@ -59,24 +57,3 @@ export interface RegisterDecoratorParameters { */ factory: DocumentDecoratorFactory; } - -export type IndexBuilderServiceBuildOptions = { - searchEngine: SearchEngine; - collators: RegisterCollatorParameters[]; - decorators: RegisterDecoratorParameters[]; -}; -export interface SearchIndexBuilderService { - build(options: IndexBuilderServiceBuildOptions): Promise<{ - scheduler: Scheduler; - }>; - getDocumentTypes(): Record; -} - -export interface SearchIndexRegistryExtensionPoint { - addCollator(options: RegisterCollatorParameters): void; - addDecorator(options: RegisterDecoratorParameters): void; -} - -export interface SearchEngineRegistryExtensionPoint { - setSearchEngine(searchEngine: SearchEngine): void; -} diff --git a/plugins/search-backend/package.json b/plugins/search-backend/package.json index bbd5d78f79..6e971ba646 100644 --- a/plugins/search-backend/package.json +++ b/plugins/search-backend/package.json @@ -6,9 +6,22 @@ "types": "src/index.ts", "license": "Apache-2.0", "publishConfig": { - "access": "public", - "main": "dist/index.cjs.js", - "types": "dist/index.d.ts" + "access": "public" + }, + "exports": { + ".": "./src/index.ts", + "./alpha": "./src/alpha.ts", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "alpha": [ + "src/alpha.ts" + ], + "package.json": [ + "package.json" + ] + } }, "backstage": { "role": "backend-plugin" diff --git a/plugins/search-backend-node/src/extensions.ts b/plugins/search-backend/src/alpha.ts similarity index 56% rename from plugins/search-backend-node/src/extensions.ts rename to plugins/search-backend/src/alpha.ts index 23392b70a5..e35b33188b 100644 --- a/plugins/search-backend-node/src/extensions.ts +++ b/plugins/search-backend/src/alpha.ts @@ -14,18 +14,4 @@ * limitations under the License. */ -import { createExtensionPoint } from '@backstage/backend-plugin-api'; -import { - SearchEngineRegistryExtensionPoint, - SearchIndexRegistryExtensionPoint, -} from './types'; - -export const searchEngineRegistryExtensionPoint = - createExtensionPoint({ - id: 'search.engine.registry', - }); - -export const searchIndexRegistryExtensionPoint = - createExtensionPoint({ - id: 'search.index.registry', - }); +export { searchPlugin } from './plugin'; diff --git a/plugins/search-backend/src/index.ts b/plugins/search-backend/src/index.ts index 335841d383..851efd1875 100644 --- a/plugins/search-backend/src/index.ts +++ b/plugins/search-backend/src/index.ts @@ -21,4 +21,3 @@ */ export * from './service/router'; -export * from './plugin'; diff --git a/plugins/search-backend/src/plugin.ts b/plugins/search-backend/src/plugin.ts index 35a9835ef6..7c01ddc607 100644 --- a/plugins/search-backend/src/plugin.ts +++ b/plugins/search-backend/src/plugin.ts @@ -19,16 +19,18 @@ import { createBackendPlugin, } from '@backstage/backend-plugin-api'; import { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + RegisterCollatorParameters, + RegisterDecoratorParameters, + LunrSearchEngine, +} from '@backstage/plugin-search-backend-node'; import { searchIndexBuilderService, searchIndexRegistryExtensionPoint, SearchIndexRegistryExtensionPoint, - searchEngineRegistryExtensionPoint, - RegisterCollatorParameters, - RegisterDecoratorParameters, SearchEngineRegistryExtensionPoint, - LunrSearchEngine, -} from '@backstage/plugin-search-backend-node'; + searchEngineRegistryExtensionPoint, +} from '@backstage/plugin-search-backend-node/alpha'; import { createRouter } from './service/router'; import { SearchEngine } from '@backstage/plugin-search-common'; diff --git a/yarn.lock b/yarn.lock index 3c787dceb3..c2c60036fb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7796,6 +7796,7 @@ __metadata: resolution: "@backstage/plugin-search-backend-module-pg@workspace:plugins/search-backend-module-pg" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^"