Refactored types based on feedback

Signed-off-by: Andre Wanlin <67169551+awanlin@users.noreply.github.com>
This commit is contained in:
Andre Wanlin
2022-06-21 16:03:12 -05:00
committed by Fredrik Adelöw
parent cb62dd8a8a
commit a45d796451
8 changed files with 81 additions and 67 deletions
+14 -7
View File
@@ -12,9 +12,7 @@ import { PluginDatabaseManager } from '@backstage/backend-common';
import { SearchEngine } from '@backstage/plugin-search-backend-node';
import { SearchQuery } from '@backstage/plugin-search-common';
// Warning: (ae-missing-release-tag) "ConcretePgSearchQuery" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
// @public
export type ConcretePgSearchQuery = {
pgQuery: PgSearchQuery;
pageSize: number;
@@ -97,15 +95,13 @@ export class PgSearchEngine implements SearchEngine {
// (undocumented)
query(query: SearchQuery): Promise<IndexableResultSet>;
// (undocumented)
setTranslator(
translator: (query: SearchQuery) => ConcretePgSearchQuery,
): void;
setTranslator(translator: PgSearchQueryTranslator): void;
// (undocumented)
static supported(database: PluginDatabaseManager): Promise<boolean>;
// (undocumented)
translator(
query: SearchQuery,
options: PgSearchHighlightOptions,
options: PgSearchQueryTranslatorOptions,
): ConcretePgSearchQuery;
}
@@ -168,6 +164,17 @@ export interface PgSearchQuery {
types?: string[];
}
// @public
export type PgSearchQueryTranslator = (
query: SearchQuery,
options: PgSearchQueryTranslatorOptions,
) => ConcretePgSearchQuery;
// @public
export type PgSearchQueryTranslatorOptions = {
highlightOptions: PgSearchHighlightOptions;
};
// Warning: (ae-missing-release-tag) "RawDocumentRow" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
@@ -15,12 +15,12 @@
*/
import { ConfigReader } from '@backstage/config';
import { DatabaseStore } from '../database';
import { PgSearchHighlightOptions } from '../types';
import {
ConcretePgSearchQuery,
decodePageCursor,
encodePageCursor,
PgSearchEngine,
PgSearchHighlightOptions,
} from './PgSearchEngine';
import { PgSearchEngineIndexer } from './PgSearchEngineIndexer';
@@ -86,7 +86,7 @@ describe('PgSearchEngine', () => {
term: 'testTerm',
filters: {},
},
highlightOptions,
{ highlightOptions },
);
});
@@ -96,7 +96,7 @@ describe('PgSearchEngine', () => {
term: 'Hello',
pageCursor: 'MQ==',
},
highlightOptions,
{ highlightOptions },
);
expect(actualTranslatedQuery).toMatchObject({
@@ -114,7 +114,7 @@ describe('PgSearchEngine', () => {
{
term: 'Hello World',
},
highlightOptions,
{ highlightOptions },
);
expect(actualTranslatedQuery).toMatchObject({
@@ -133,7 +133,7 @@ describe('PgSearchEngine', () => {
term: 'H&e|l!l*o W\0o(r)l:d',
pageCursor: '',
},
highlightOptions,
{ highlightOptions },
) as ConcretePgSearchQuery;
expect(actualTranslatedQuery).toMatchObject({
@@ -151,7 +151,7 @@ describe('PgSearchEngine', () => {
filters: { kind: 'testKind' },
types: ['my-filter'],
},
highlightOptions,
{ highlightOptions },
);
expect(actualTranslatedQuery).toMatchObject({
@@ -28,13 +28,58 @@ import {
} from '../database';
import { v4 as uuid } from 'uuid';
import { Config } from '@backstage/config';
import { PgSearchHighlightOptions, PgSearchOptions } from '../types';
/**
* Search query that the Postgres search engine understands.
* @public
*/
export type ConcretePgSearchQuery = {
pgQuery: PgSearchQuery;
pageSize: number;
};
/**
* Options available for the Postgres specific query translator.
* @public
*/
export type PgSearchQueryTranslatorOptions = {
highlightOptions: PgSearchHighlightOptions;
};
/**
* Postgres specific query translator.
* @public
*/
export type PgSearchQueryTranslator = (
query: SearchQuery,
options: PgSearchQueryTranslatorOptions,
) => ConcretePgSearchQuery;
/**
* Options to instantiate PgSearchEngine
* @public
*/
export type PgSearchOptions = {
config: Config;
database: PluginDatabaseManager;
};
/**
* Options for highlighting search terms
* @public
*/
export type PgSearchHighlightOptions = {
useHighlight?: boolean;
maxWords?: number;
minWords?: number;
shortWord?: number;
highlightAll?: boolean;
maxFragments?: number;
fragmentDelimiter?: string;
preTag: string;
postTag: string;
};
export class PgSearchEngine implements SearchEngine {
private readonly highlightOptions: PgSearchHighlightOptions;
constructor(private readonly databaseStore: DatabaseStore, config: Config) {
@@ -85,7 +130,7 @@ export class PgSearchEngine implements SearchEngine {
translator(
query: SearchQuery,
options: PgSearchHighlightOptions,
options: PgSearchQueryTranslatorOptions,
): ConcretePgSearchQuery {
const pageSize = 25;
const { page } = decodePageCursor(query.pageCursor);
@@ -105,15 +150,13 @@ export class PgSearchEngine implements SearchEngine {
types: query.types,
offset,
limit,
options,
options: options.highlightOptions,
},
pageSize,
};
}
setTranslator(
translator: (query: SearchQuery) => ConcretePgSearchQuery,
): void {
setTranslator(translator: PgSearchQueryTranslator) {
this.translator = translator;
}
@@ -126,7 +169,9 @@ export class PgSearchEngine implements SearchEngine {
}
async query(query: SearchQuery): Promise<IndexableResultSet> {
const { pgQuery, pageSize } = this.translator(query, this.highlightOptions);
const { pgQuery, pageSize } = this.translator(query, {
highlightOptions: this.highlightOptions,
});
const rows = await this.databaseStore.transaction(async tx =>
this.databaseStore.query(tx, pgQuery),
@@ -14,7 +14,13 @@
* limitations under the License.
*/
export { PgSearchEngine } from './PgSearchEngine';
export type { ConcretePgSearchQuery } from './PgSearchEngine';
export type {
ConcretePgSearchQuery,
PgSearchQueryTranslatorOptions,
PgSearchQueryTranslator,
PgSearchOptions,
PgSearchHighlightOptions,
} from './PgSearchEngine';
export type {
PgSearchEngineIndexer,
PgSearchEngineIndexerOptions,
@@ -15,7 +15,7 @@
*/
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
import { IndexableDocument } from '@backstage/plugin-search-common';
import { PgSearchHighlightOptions } from '../types';
import { PgSearchHighlightOptions } from '../PgSearchEngine';
import { DatabaseDocumentStore } from './DatabaseDocumentStore';
const highlightOptions: PgSearchHighlightOptions = {
@@ -15,7 +15,7 @@
*/
import { IndexableDocument } from '@backstage/plugin-search-common';
import { Knex } from 'knex';
import { PgSearchHighlightOptions } from '../types';
import { PgSearchHighlightOptions } from '../PgSearchEngine';
export interface PgSearchQuery {
fields?: Record<string, string | string[]>;
@@ -22,4 +22,3 @@
export * from './database';
export * from './PgSearchEngine';
export type { PgSearchOptions, PgSearchHighlightOptions } from './types';
@@ -1,43 +0,0 @@
/*
* Copyright 2022 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 { PluginDatabaseManager } from '@backstage/backend-common';
import { Config } from '@backstage/config';
/**
* Options to instantiate PgSearchEngine
* @public
*/
export type PgSearchOptions = {
config: Config;
database: PluginDatabaseManager;
};
/**
* Options for highlighting search terms
* @public
*/
export type PgSearchHighlightOptions = {
useHighlight?: boolean;
maxWords?: number;
minWords?: number;
shortWord?: number;
highlightAll?: boolean;
maxFragments?: number;
fragmentDelimiter?: string;
preTag: string;
postTag: string;
};