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.

250 lines 9.09 kB
import { ForgeSqlOperation, SchemaAnalyzeForgeSql } from "./ForgeSQLQueryBuilder"; import { Query } from "drizzle-orm"; import { ClusterStatementRowCamelCase, ExplainAnalyzeRow, SlowQueryNormalized } from "./SystemTables"; import { AnyMySqlTable } from "drizzle-orm/mysql-core"; /** * Interface representing a raw slow query row from the database */ interface SlowQueryRaw { Time: string; Txn_start_ts: number; User: string; Host: string; Conn_ID: number; DB: string; Query: string; Digest: string; Query_time: number; Compile_time: number; Optimize_time: number; Process_time: number; Wait_time: number; Parse_time: number; Rewrite_time: number; Cop_time: number; Cop_proc_avg: number; Cop_proc_max: number; Cop_proc_p90: number; Cop_proc_addr: string; Cop_wait_avg: number; Cop_wait_max: number; Cop_wait_p90: number; Cop_wait_addr: string; Mem_max: number; Disk_max: number; Total_keys: number; Process_keys: number; Request_count: number; KV_total: number; PD_total: number; Result_rows: number; Rocksdb_block_cache_hit_count: number; Rocksdb_block_read_count: number; Rocksdb_block_read_byte: number; Plan: string; Binary_plan: string; Plan_digest: string; } /** * Interface representing a row from the cluster statements table */ export interface ClusterStatementRow { INSTANCE: string; SUMMARY_BEGIN_TIME: string; SUMMARY_END_TIME: string; STMT_TYPE: string; SCHEMA_NAME: string; DIGEST: string; DIGEST_TEXT: string; TABLE_NAMES: string; INDEX_NAMES: string | null; SAMPLE_USER: string; EXEC_COUNT: number; SUM_ERRORS: number; SUM_WARNINGS: number; SUM_LATENCY: number; MAX_LATENCY: number; MIN_LATENCY: number; AVG_LATENCY: number; AVG_PARSE_LATENCY: number; MAX_PARSE_LATENCY: number; AVG_COMPILE_LATENCY: number; MAX_COMPILE_LATENCY: number; SUM_COP_TASK_NUM: number; MAX_COP_PROCESS_TIME: number; MAX_COP_PROCESS_ADDRESS: string; MAX_COP_WAIT_TIME: number; MAX_COP_WAIT_ADDRESS: string; AVG_PROCESS_TIME: number; MAX_PROCESS_TIME: number; AVG_WAIT_TIME: number; MAX_WAIT_TIME: number; AVG_BACKOFF_TIME: number; MAX_BACKOFF_TIME: number; AVG_TOTAL_KEYS: number; MAX_TOTAL_KEYS: number; AVG_PROCESSED_KEYS: number; MAX_PROCESSED_KEYS: number; AVG_ROCKSDB_DELETE_SKIPPED_COUNT: number; MAX_ROCKSDB_DELETE_SKIPPED_COUNT: number; AVG_ROCKSDB_KEY_SKIPPED_COUNT: number; MAX_ROCKSDB_KEY_SKIPPED_COUNT: number; AVG_ROCKSDB_BLOCK_CACHE_HIT_COUNT: number; MAX_ROCKSDB_BLOCK_CACHE_HIT_COUNT: number; AVG_ROCKSDB_BLOCK_READ_COUNT: number; MAX_ROCKSDB_BLOCK_READ_COUNT: number; AVG_ROCKSDB_BLOCK_READ_BYTE: number; MAX_ROCKSDB_BLOCK_READ_BYTE: number; AVG_PREWRITE_TIME: number; MAX_PREWRITE_TIME: number; AVG_COMMIT_TIME: number; MAX_COMMIT_TIME: number; AVG_GET_COMMIT_TS_TIME: number; MAX_GET_COMMIT_TS_TIME: number; AVG_COMMIT_BACKOFF_TIME: number; MAX_COMMIT_BACKOFF_TIME: number; AVG_RESOLVE_LOCK_TIME: number; MAX_RESOLVE_LOCK_TIME: number; AVG_LOCAL_LATCH_WAIT_TIME: number; MAX_LOCAL_LATCH_WAIT_TIME: number; AVG_WRITE_KEYS: number; MAX_WRITE_KEYS: number; AVG_WRITE_SIZE: number; MAX_WRITE_SIZE: number; AVG_PREWRITE_REGIONS: number; MAX_PREWRITE_REGIONS: number; AVG_TXN_RETRY: number; MAX_TXN_RETRY: number; SUM_EXEC_RETRY: number; SUM_EXEC_RETRY_TIME: number; SUM_BACKOFF_TIMES: number; BACKOFF_TYPES: string | null; AVG_MEM: number; MAX_MEM: number; AVG_DISK: number; MAX_DISK: number; AVG_KV_TIME: number; AVG_PD_TIME: number; AVG_BACKOFF_TOTAL_TIME: number; AVG_WRITE_SQL_RESP_TIME: number; AVG_TIDB_CPU_TIME: number; AVG_TIKV_CPU_TIME: number; MAX_RESULT_ROWS: number; MIN_RESULT_ROWS: number; AVG_RESULT_ROWS: number; PREPARED: number; AVG_AFFECTED_ROWS: number; FIRST_SEEN: string; LAST_SEEN: string; PLAN_IN_CACHE: number; PLAN_CACHE_HITS: number; PLAN_IN_BINDING: number; QUERY_SAMPLE_TEXT: string; PREV_SAMPLE_TEXT: string; PLAN_DIGEST: string; PLAN: string; BINARY_PLAN: string; CHARSET: string; COLLATION: string; PLAN_HINT: string; MAX_REQUEST_UNIT_READ: number; AVG_REQUEST_UNIT_READ: number; MAX_REQUEST_UNIT_WRITE: number; AVG_REQUEST_UNIT_WRITE: number; MAX_QUEUED_RC_TIME: number; AVG_QUEUED_RC_TIME: number; RESOURCE_GROUP: string; PLAN_CACHE_UNQUALIFIED: number; PLAN_CACHE_UNQUALIFIED_LAST_REASON: string; } /** * Class implementing SQL analysis operations for ForgeSQL ORM. * Provides methods for analyzing query performance, execution plans, and slow queries. */ export declare class ForgeSQLAnalyseOperation implements SchemaAnalyzeForgeSql { private readonly forgeOperations; /** * Creates a new instance of ForgeSQLAnalizeOperation. * @param {ForgeSqlOperation} forgeOperations - The ForgeSQL operations instance */ constructor(forgeOperations: ForgeSqlOperation); /** * Executes EXPLAIN on a raw SQL query. * @param {string} query - The SQL query to analyze * @param {unknown[]} bindParams - The query parameters * @returns {Promise<ExplainAnalyzeRow[]>} The execution plan analysis results */ explainRaw(query: string, bindParams: unknown[]): Promise<ExplainAnalyzeRow[]>; /** * Executes EXPLAIN on a Drizzle query. * @param {{ toSQL: () => Query }} query - The Drizzle query to analyze * @returns {Promise<ExplainAnalyzeRow[]>} The execution plan analysis results */ explain(query: { toSQL: () => Query; }): Promise<ExplainAnalyzeRow[]>; /** * Executes EXPLAIN ANALYZE on a raw SQL query. * @param {string} query - The SQL query to analyze * @param {unknown[]} bindParams - The query parameters * @returns {Promise<ExplainAnalyzeRow[]>} The execution plan analysis results */ explainAnalyzeRaw(query: string, bindParams: unknown[]): Promise<ExplainAnalyzeRow[]>; /** * Executes EXPLAIN ANALYZE on a Drizzle query. * @param {{ toSQL: () => Query }} query - The Drizzle query to analyze * @returns {Promise<ExplainAnalyzeRow[]>} The execution plan analysis results */ explainAnalyze(query: { toSQL: () => Query; }): Promise<ExplainAnalyzeRow[]>; /** * Decodes a query execution plan from its string representation. * @param {string} input - The raw execution plan string * @returns {ExplainAnalyzeRow[]} The decoded execution plan rows */ decodedPlan(input: string): ExplainAnalyzeRow[]; /** * Normalizes a raw slow query row into a more structured format. * @param {SlowQueryRaw} row - The raw slow query data * @returns {SlowQueryNormalized} The normalized slow query data */ normalizeSlowQuery(row: SlowQueryRaw): SlowQueryNormalized; /** * Builds a SQL query for retrieving cluster statement history. * @param {string[]} tables - The tables to analyze * @param {Date} [from] - The start date for the analysis * @param {Date} [to] - The end date for the analysis * @returns {string} The SQL query for cluster statement history */ buildClusterStatementQuery(tables: string[], from?: Date, to?: Date): string; /** * Retrieves and analyzes slow queries from the database. * @returns {Promise<SlowQueryNormalized[]>} The normalized slow query data */ analyzeSlowQueries(): Promise<SlowQueryNormalized[]>; /** * Converts a cluster statement row to camelCase format. * @param {Record<string, any>} input - The input row data * @returns {ClusterStatementRowCamelCase} The converted row data */ mapToCamelCaseClusterStatement(input: Record<string, any>): ClusterStatementRowCamelCase; /** * Analyzes query history for specific tables using raw table names. * @param {string[]} tables - The table names to analyze * @param {Date} [fromDate] - The start date for the analysis * @param {Date} [toDate] - The end date for the analysis * @returns {Promise<ClusterStatementRowCamelCase[]>} The analyzed query history */ analyzeQueriesHistoryRaw(tables: string[], fromDate?: Date, toDate?: Date): Promise<ClusterStatementRowCamelCase[]>; /** * Analyzes query history for specific tables using Drizzle table objects. * @param {AnyMySqlTable[]} tables - The Drizzle table objects to analyze * @param {Date} [fromDate] - The start date for the analysis * @param {Date} [toDate] - The end date for the analysis * @returns {Promise<ClusterStatementRowCamelCase[]>} The analyzed query history */ analyzeQueriesHistory(tables: AnyMySqlTable[], fromDate?: Date, toDate?: Date): Promise<ClusterStatementRowCamelCase[]>; } export {}; //# sourceMappingURL=ForgeSQLAnalyseOperations.d.ts.map