From 14ce4026ed3d0753cccd7c06f4ed3090ea6f3605 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 13 Dec 2024 00:53:50 +0100 Subject: [PATCH] catalog-backend: rename enableRawJsonRespone -> disableRelationsCompatibility Signed-off-by: Patrik Oldsberg --- .changeset/curly-teachers-marry.md | 2 +- plugins/catalog-backend/config.d.ts | 10 ++++++++++ plugins/catalog-backend/src/service/CatalogBuilder.ts | 8 ++++---- .../src/service/DefaultEntitiesCatalog.ts | 10 ++++++---- .../catalog-backend/src/service/createRouter.test.ts | 2 +- plugins/catalog-backend/src/service/createRouter.ts | 6 +++--- plugins/catalog-backend/src/tests/integration.test.ts | 6 +++--- 7 files changed, 28 insertions(+), 16 deletions(-) diff --git a/.changeset/curly-teachers-marry.md b/.changeset/curly-teachers-marry.md index 21f41da291..ad22613612 100644 --- a/.changeset/curly-teachers-marry.md +++ b/.changeset/curly-teachers-marry.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog-backend': minor --- -Added a new `catalog.enableRawJsonResponse` configuration option that avoids JSON deserialization and serialization if possible when reading entities. This can significantly improve the overall performance of the catalog, but it removes the backwards compatibility processing that ensures that both `entity.relation[].target` and `entity.relation[].targetRef` are present in returned entities. +Added a new `catalog.disableRelationsCompatibility` configuration option that avoids JSON deserialization and serialization if possible when reading entities. This can significantly improve the overall performance of the catalog, but it removes the backwards compatibility processing that ensures that both `entity.relation[].target` and `entity.relation[].targetRef` are present in returned entities. diff --git a/plugins/catalog-backend/config.d.ts b/plugins/catalog-backend/config.d.ts index 8dd5e50480..be6104ac84 100644 --- a/plugins/catalog-backend/config.d.ts +++ b/plugins/catalog-backend/config.d.ts @@ -138,6 +138,16 @@ export interface Config { }>; }>; + /** + * Disables the compatibility layer for relations in returned entities that + * ensures that all relations objects have both `target` and `targetRef`. + * + * Enabling this option can very significantly improve the performance of + * the catalog, but may break consumers that rely on the existence of + * `target` in the relations objects. + */ + disableRelationsCompatibility?: boolean; + /** * The strategy to use for entities that are orphaned, i.e. no longer have * any other entities or providers referencing them. The default value is diff --git a/plugins/catalog-backend/src/service/CatalogBuilder.ts b/plugins/catalog-backend/src/service/CatalogBuilder.ts index fd052bf474..7ce308e796 100644 --- a/plugins/catalog-backend/src/service/CatalogBuilder.ts +++ b/plugins/catalog-backend/src/service/CatalogBuilder.ts @@ -486,8 +486,8 @@ export class CatalogBuilder { discovery, }); - const enableRawJson = config.getOptionalBoolean( - 'catalog.enableRawJsonResponses', + const disableRelationsCompatibility = config.getOptionalBoolean( + 'catalog.disableRelationsCompatibility', ); const policy = this.buildEntityPolicy(); @@ -526,7 +526,7 @@ export class CatalogBuilder { database: dbClient, logger, stitcher, - enableRawJson, + disableRelationsCompatibility, }); let permissionsService: PermissionsService; @@ -638,7 +638,7 @@ export class CatalogBuilder { auth, httpAuth, permissionsService, - enableRawJson, + disableRelationsCompatibility, }); await connectEntityProviders(providerDatabase, entityProviders); diff --git a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts index ee9b46ce55..f0e0667bef 100644 --- a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts +++ b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts @@ -105,18 +105,20 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { private readonly database: Knex; private readonly logger: LoggerService; private readonly stitcher: Stitcher; - private readonly enableRawJson: boolean; + private readonly disableRelationsCompatibility: boolean; constructor(options: { database: Knex; logger: LoggerService; stitcher: Stitcher; - enableRawJson?: boolean; + disableRelationsCompatibility?: boolean; }) { this.database = options.database; this.logger = options.logger; this.stitcher = options.stitcher; - this.enableRawJson = Boolean(options.enableRawJson); + this.disableRelationsCompatibility = Boolean( + options.disableRelationsCompatibility, + ); } async entities(request?: EntitiesRequest): Promise { @@ -197,7 +199,7 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { return { entities: processRawEntitiesResult( rows.map(r => r.final_entity!), - this.enableRawJson + this.disableRelationsCompatibility ? request?.fields : e => { expandLegacyCompoundRelationsInEntity(e); diff --git a/plugins/catalog-backend/src/service/createRouter.test.ts b/plugins/catalog-backend/src/service/createRouter.test.ts index 312900bcf1..53f831d320 100644 --- a/plugins/catalog-backend/src/service/createRouter.test.ts +++ b/plugins/catalog-backend/src/service/createRouter.test.ts @@ -883,7 +883,7 @@ describe('createRouter readonly and raw json enabled', () => { getLocationByEntity: jest.fn(), }; const router = await createRouter({ - enableRawJson: true, + disableRelationsCompatibility: true, entitiesCatalog, locationService, logger: mockServices.logger.mock(), diff --git a/plugins/catalog-backend/src/service/createRouter.ts b/plugins/catalog-backend/src/service/createRouter.ts index 6ea9ea1c13..84826267f2 100644 --- a/plugins/catalog-backend/src/service/createRouter.ts +++ b/plugins/catalog-backend/src/service/createRouter.ts @@ -85,7 +85,7 @@ export interface RouterOptions { auth: AuthService; httpAuth: HttpAuthService; permissionsService: PermissionsService; - enableRawJson?: boolean; + disableRelationsCompatibility?: boolean; } /** @@ -113,7 +113,7 @@ export async function createRouter( permissionsService, auth, httpAuth, - enableRawJson = false, + disableRelationsCompatibility = false, } = options; const readonlyEnabled = @@ -215,7 +215,7 @@ export async function createRouter( signal.throwIfAborted(); - if (!enableRawJson) { + if (!disableRelationsCompatibility) { processEntitiesResponseItems( result.items, expandLegacyCompoundRelationsInEntity, diff --git a/plugins/catalog-backend/src/tests/integration.test.ts b/plugins/catalog-backend/src/tests/integration.test.ts index 32116ace0d..e85ebe48ce 100644 --- a/plugins/catalog-backend/src/tests/integration.test.ts +++ b/plugins/catalog-backend/src/tests/integration.test.ts @@ -198,7 +198,7 @@ class TestHarness { readonly #proxyProgressTracker: ProxyProgressTracker; static async create(options?: { - enableRawJson?: boolean; + disableRelationsCompatibility?: boolean; logger?: LoggerService; db?: Knex; permissions?: PermissionEvaluator; @@ -277,7 +277,7 @@ class TestHarness { database: db, logger, stitcher, - enableRawJson: options?.enableRawJson, + disableRelationsCompatibility: options?.disableRelationsCompatibility, }); const proxyProgressTracker = new ProxyProgressTracker( new NoopProgressTracker(), @@ -788,7 +788,7 @@ describe('Catalog Backend Integration', () => { it('should return valid responses in raw JSON mode', async () => { const harness = await TestHarness.create({ - enableRawJson: true, + disableRelationsCompatibility: true, }); const entityA = {