Expose optional persistenceContext on TechInsights construction
This enables integrators to provide their own database implementations for fact handling if something more suitable than Postgres/SQlite is needed. Signed-off-by: Jussi Hallila <jussi@hallila.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-tech-insights-backend': patch
|
||||
---
|
||||
|
||||
Expose optional persistenceContext on TechInsights construction to enable integrators to provide their own database implementations for fact handling.
|
||||
@@ -5,6 +5,7 @@
|
||||
```ts
|
||||
import { CheckResult } from '@backstage/plugin-tech-insights-common';
|
||||
import { Config } from '@backstage/config';
|
||||
import { DateTime } from 'luxon';
|
||||
import { Duration } from 'luxon';
|
||||
import express from 'express';
|
||||
import { FactChecker } from '@backstage/plugin-tech-insights-node';
|
||||
@@ -13,12 +14,16 @@ import { FactLifecycle } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactRetriever } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactRetrieverRegistration } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactSchema } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactSchemaDefinition } from '@backstage/plugin-tech-insights-node';
|
||||
import { FlatTechInsightFact } from '@backstage/plugin-tech-insights-node';
|
||||
import { HumanDuration } from '@backstage/types';
|
||||
import { Knex } from 'knex';
|
||||
import { Logger } from 'winston';
|
||||
import { PluginDatabaseManager } from '@backstage/backend-common';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
import { PluginTaskScheduler } from '@backstage/backend-tasks';
|
||||
import { TechInsightCheck } from '@backstage/plugin-tech-insights-node';
|
||||
import { TechInsightFact } from '@backstage/plugin-tech-insights-node';
|
||||
import { TechInsightsStore } from '@backstage/plugin-tech-insights-node';
|
||||
import { TokenManager } from '@backstage/backend-common';
|
||||
|
||||
@@ -106,6 +111,41 @@ export type TechInsightsContext<
|
||||
factRetrieverEngine: FactRetrieverEngine;
|
||||
};
|
||||
|
||||
// @public
|
||||
export class TechInsightsDatabase implements TechInsightsStore {
|
||||
constructor(db: Knex, logger: Logger);
|
||||
// (undocumented)
|
||||
getFactsBetweenTimestampsByIds(
|
||||
ids: string[],
|
||||
entityTriplet: string,
|
||||
startDateTime: DateTime,
|
||||
endDateTime: DateTime,
|
||||
): Promise<{
|
||||
[factId: string]: FlatTechInsightFact[];
|
||||
}>;
|
||||
// (undocumented)
|
||||
getLatestFactsByIds(
|
||||
ids: string[],
|
||||
entityTriplet: string,
|
||||
): Promise<{
|
||||
[factId: string]: FlatTechInsightFact;
|
||||
}>;
|
||||
// (undocumented)
|
||||
getLatestSchemas(ids?: string[]): Promise<FactSchema[]>;
|
||||
// (undocumented)
|
||||
insertFacts({
|
||||
id,
|
||||
facts,
|
||||
lifecycle,
|
||||
}: {
|
||||
id: string;
|
||||
facts: TechInsightFact[];
|
||||
lifecycle?: FactLifecycle;
|
||||
}): Promise<void>;
|
||||
// (undocumented)
|
||||
insertFactSchema(schemaDefinition: FactSchemaDefinition): Promise<void>;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface TechInsightsOptions<
|
||||
CheckType extends TechInsightCheck,
|
||||
@@ -122,6 +162,7 @@ export interface TechInsightsOptions<
|
||||
factRetrievers?: FactRetrieverRegistration[];
|
||||
// (undocumented)
|
||||
logger: Logger;
|
||||
persistenceContext?: PersistenceContext;
|
||||
// (undocumented)
|
||||
scheduler: PluginTaskScheduler;
|
||||
// (undocumented)
|
||||
|
||||
@@ -23,6 +23,7 @@ export type {
|
||||
TechInsightsContext,
|
||||
} from './service/techInsightsContextBuilder';
|
||||
export type { FactRetrieverEngine } from './service/fact/FactRetrieverEngine';
|
||||
export type { TechInsightsDatabase } from './service/persistence/TechInsightsDatabase';
|
||||
export type { PersistenceContext } from './service/persistence/persistenceContext';
|
||||
export { createFactRetrieverRegistration } from './service/fact/createFactRetriever';
|
||||
export type { FactRetrieverRegistry } from './service/fact/FactRetrieverRegistry';
|
||||
|
||||
@@ -46,6 +46,11 @@ type RawDbFactSchemaRow = {
|
||||
entityFilter?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Default TechInsightsDatabase implementation.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export class TechInsightsDatabase implements TechInsightsStore {
|
||||
private readonly CHUNK_SIZE = 50;
|
||||
|
||||
|
||||
@@ -74,6 +74,12 @@ export interface TechInsightsOptions<
|
||||
*/
|
||||
factRetrieverRegistry?: FactRetrieverRegistry;
|
||||
|
||||
/**
|
||||
* Optional persistenceContext implementation that replaces the default one.
|
||||
* This can be used to replace underlying database with a more suitable implementation if needed
|
||||
*/
|
||||
persistenceContext?: PersistenceContext;
|
||||
|
||||
logger: Logger;
|
||||
config: Config;
|
||||
discovery: PluginEndpointDiscovery;
|
||||
@@ -139,9 +145,11 @@ export const buildTechInsightsContext = async <
|
||||
|
||||
const factRetrieverRegistry = buildFactRetrieverRegistry();
|
||||
|
||||
const persistenceContext = await initializePersistenceContext(database, {
|
||||
logger,
|
||||
});
|
||||
const persistenceContext =
|
||||
options.persistenceContext ??
|
||||
(await initializePersistenceContext(database, {
|
||||
logger,
|
||||
}));
|
||||
|
||||
const factRetrieverEngine = await DefaultFactRetrieverEngine.create({
|
||||
scheduler,
|
||||
|
||||
Reference in New Issue
Block a user