diff --git a/packages/engine-http/src/content/ContentApiMiddlewareFactory.ts b/packages/engine-http/src/content/ContentApiMiddlewareFactory.ts index bfd335e5..05ee96d6 100644 --- a/packages/engine-http/src/content/ContentApiMiddlewareFactory.ts +++ b/packages/engine-http/src/content/ContentApiMiddlewareFactory.ts @@ -53,7 +53,7 @@ export class ContentApiMiddlewareFactory { request, timer, systemDatabase, - stageSlug: stage.slug, + stageId: stage.id, }) if (notModifiedRes?.isModified === false) { response.status = 304 diff --git a/packages/engine-http/src/content/NotModifiedChecker.ts b/packages/engine-http/src/content/NotModifiedChecker.ts index e404ab50..5b1afaa3 100644 --- a/packages/engine-http/src/content/NotModifiedChecker.ts +++ b/packages/engine-http/src/content/NotModifiedChecker.ts @@ -11,11 +11,11 @@ export interface NotModifiedCheckResult { } export class NotModifiedChecker { - public async checkNotModified({ request, timer, systemDatabase, stageSlug }: { + public async checkNotModified({ request, timer, systemDatabase, stageId }: { request: Request timer: Timer systemDatabase: DatabaseContext - stageSlug: string + stageId: string }): Promise { if (request.headers[NotModifiedHeaderName] === undefined) { return null @@ -28,7 +28,7 @@ export class NotModifiedChecker { } const latestRef = await timer('NotModifiedCheck', () => { const queryHandler = systemDatabase.queryHandler - return queryHandler.fetch(new LatestTransactionIdByStageQuery(stageSlug)) + return queryHandler.fetch(new LatestTransactionIdByStageQuery(stageId)) }) return { diff --git a/packages/engine-system-api/src/model/queries/events/LatestTransactionIdByStageQuery.ts b/packages/engine-system-api/src/model/queries/events/LatestTransactionIdByStageQuery.ts index 1840b529..e9fa44f5 100644 --- a/packages/engine-system-api/src/model/queries/events/LatestTransactionIdByStageQuery.ts +++ b/packages/engine-system-api/src/model/queries/events/LatestTransactionIdByStageQuery.ts @@ -1,34 +1,20 @@ -import { DatabaseQuery, DatabaseQueryable } from '@contember/database' +import { DatabaseQuery, DatabaseQueryable, SelectBuilder } from '@contember/database' import { ImplementationException } from '../../../utils' export class LatestTransactionIdByStageQuery extends DatabaseQuery { - constructor(private readonly stageSlug: string) { + constructor(private readonly stageId: string) { super() } async fetch(queryable: DatabaseQueryable): Promise { - const stageId = ( - await queryable.db.query<{ id: string }>( - ` - SELECT id - FROM system.stage - WHERE slug = ? - `, - [this.stageSlug], - ) - ).rows[0].id - const rows = ( - await queryable.db.query<{ transaction_id: string }>( - ` - SELECT transaction_id - FROM system.stage_transaction - WHERE stage_id = ? - ORDER BY applied_at DESC - LIMIT 1 - `, - [stageId], - ) - ).rows + const rows = await SelectBuilder.create<{ transaction_id: string }>() + .from('stage_transaction') + .select('transaction_id') + .where({ stage_id: this.stageId }) + .orderBy('applied_at', 'desc') + .limit(1) + .getResult(queryable.db) + if (rows.length !== 1) { throw new ImplementationException() }