UNPKG

forge-sql-orm

Version:

Drizzle ORM integration for Atlassian @forge/sql. Provides a custom driver, schema migration, two levels of caching (local and global via @forge/kvs), optimistic locking, and query analysis.

89 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.printDegradationQueriesConsumer = printDegradationQueriesConsumer; const metadataContextUtils_1 = require("../utils/metadataContextUtils"); /** * Consumer function for processing async query degradation events. * * This function is called by the Forge event system when a query degradation event * is received from the queue. It processes the event and prints query performance * analysis including execution plans for slow queries. * * The function logs a warning message with job ID, total DB time, query count, and * start time to help correlate with the original resolver/service logs. * * @param forgeSQLORM - The ForgeSQL operation instance for database access * @param event - The async event containing query degradation data * @returns Promise that resolves when query analysis is complete * * @example * ```yaml * # manifest.yml - Configure consumer for async query degradation analysis * modules: * consumer: * - key: print-degradation-queries * queue: degradationQueue * function: handlerAsyncDegradation * function: * - key: handlerAsyncDegradation * handler: index.handlerAsyncDegradation * ``` * * @example * ```typescript * // index.ts - Handler function that processes async events * import { AsyncEvent } from "@forge/events"; * import { printDegradationQueriesConsumer } from "forge-sql-orm"; * import { FORGE_SQL_ORM } from "./utils/forgeSqlOrmUtils"; * * export const handlerAsyncDegradation = (event: AsyncEvent) => { * return printDegradationQueriesConsumer(FORGE_SQL_ORM, event); * }; * ``` * * @example * ```typescript * // Using async queue in resolver - Enable async processing * resolver.define("fetch", async (req: Request) => { * return await FORGE_SQL_ORM.executeWithMetadata( * async () => { * // ... your queries ... * return await SQL_QUERY; * }, * async (totalDbExecutionTime, totalResponseSize, printQueries) => { * if (totalDbExecutionTime > 800) { * await printQueries(); // Will queue for async processing * } * }, * { asyncQueueName: "degradationQueue" } // Enable async processing * ); * }); * ``` * * @example * ```typescript * // Log correlation - How to find related logs * // * // 1. In resolver/service log, you'll see: * // [Performance Analysis] Query degradation event queued for async processing | Job ID: abc-123 | ... * // * // 2. In consumer log (handlerAsyncDegradation), search for the same Job ID: * // [Performance Analysis] Processing query degradation event | Job ID: abc-123 | ... * // * // 3. To find all related logs, search logs for: "Job ID: abc-123" * // This will show both the queuing event and the processing event * // * // Example log flow: * // WARN resolver: [Performance Analysis] Query degradation event queued... | Job ID: abc-123 * // WARN handlerAsyncDegradation: [Performance Analysis] Processing query degradation event | Job ID: abc-123 * // WARN handlerAsyncDegradation: SQL: SELECT ... | Time: 3514 ms * ``` */ async function printDegradationQueriesConsumer(forgeSQLORM, event) { const body = event.body; body.beginTime = new Date(body.beginTime); // eslint-disable-next-line no-console console.warn(`[Performance Analysis] Processing query degradation event | Job ID: ${event.jobId} | Total DB time: ${body.totalDbExecutionTime}ms | Queries: ${body.statistics.length} | Started: ${body.beginTime.toISOString()}`); await (0, metadataContextUtils_1.printDegradationQueries)(forgeSQLORM, body); } //# sourceMappingURL=PrintQueryConsumer.js.map