Skip to content

Commit 4b08cb3

Browse files
committed
fix: change search collator to use database
1 parent f24e5bf commit 4b08cb3

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

docs/search.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default async function createPlugin(
1717
schedule,
1818
factory: QetaCollatorFactory.fromConfig(env.config, {
1919
logger: env.logger,
20+
database: env.database,
2021
}),
2122
});
2223

plugins/qeta-backend/src/database/DatabaseQetaStore.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,17 @@ export type RawTagEntity = {
6565
tag: string;
6666
};
6767

68-
export type RawComponentEntity = {
69-
id: number;
70-
entity_ref: string;
71-
};
72-
7368
export class DatabaseQetaStore implements QetaStore {
7469
static async create({
7570
database,
71+
skipMigrations,
7672
}: {
7773
database: PluginDatabaseManager;
74+
skipMigrations?: boolean;
7875
}): Promise<DatabaseQetaStore> {
7976
const client = await database.getClient();
8077

81-
if (!database.migrations?.skip) {
78+
if (!database.migrations?.skip && !skipMigrations) {
8279
// prettier-ignore
8380
await client.migrate.latest({ // nosonar
8481
directory: migrationsDir,

plugins/qeta-backend/src/search/collators/QetaCollatorFactory.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,36 @@ import {
55
DocumentCollatorFactory,
66
IndexableDocument,
77
} from '@backstage/plugin-search-common';
8-
import fetch from 'node-fetch';
8+
import { PluginDatabaseManager } from '@backstage/backend-common';
9+
import { DatabaseQetaStore } from '../../database';
910

1011
export type QetaCollatorFactoryOptions = {
1112
logger: Logger;
13+
database: PluginDatabaseManager;
1214
};
1315

1416
export type QetaDocument = IndexableDocument & {
1517
tags?: string[];
1618
components?: string[];
1719
author: string;
1820
views: number;
21+
score: number;
1922
answersCount: number;
23+
created: Date;
24+
updatedBy?: string;
25+
updated?: Date;
2026
};
2127

2228
export class QetaCollatorFactory implements DocumentCollatorFactory {
23-
private readonly appBaseUrl: string;
24-
private readonly backendBaseUrl: string;
2529
private readonly logger: Logger;
30+
private readonly database: PluginDatabaseManager;
31+
private readonly appBaseUrl: string;
2632
public readonly type: string = 'qeta';
2733

2834
private constructor(config: Config, options: QetaCollatorFactoryOptions) {
2935
this.logger = options.logger;
36+
this.database = options.database;
3037
this.appBaseUrl = config.getString('app.baseUrl');
31-
this.backendBaseUrl = config.getString('backend.baseUrl');
3238
}
3339

3440
static fromConfig(config: Config, options: QetaCollatorFactoryOptions) {
@@ -41,13 +47,17 @@ export class QetaCollatorFactory implements DocumentCollatorFactory {
4147

4248
async *execute(): AsyncGenerator<QetaDocument> {
4349
this.logger.info('Executing QetaCollator');
44-
const response = await fetch(
45-
`${this.backendBaseUrl}/api/qeta/questions?includeAnswers=true&includeComponents=true`,
46-
);
47-
const data = await response.json();
48-
this.logger.info(`Found ${data.questions.length} questions to index`);
50+
const db = await DatabaseQetaStore.create({
51+
database: this.database,
52+
skipMigrations: true,
53+
});
54+
55+
const questions = await db.getQuestions({
56+
includeAnswers: true,
57+
includeComponents: true,
58+
});
4959

50-
for (const question of data.questions) {
60+
for (const question of questions.questions) {
5161
yield {
5262
...question,
5363
text: question.content,

0 commit comments

Comments
 (0)