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.

85 lines 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clearCacheSchedulerTrigger = void 0; const cacheUtils_1 = require("../utils/cacheUtils"); /** * Scheduler trigger for clearing expired cache entries. * * This trigger should be configured as a Forge scheduler to automatically * clean up expired cache entries based on their TTL (Time To Live). * * @note This function is automatically disabled in production environments and will return a 500 error if called. * * @param options - Optional ForgeSQL ORM configuration. If not provided, * uses default cache settings with cacheEntityName: "cache" * @returns Promise that resolves to HTTP response object * * @example * ```typescript * // In your index.ts * import { clearCacheSchedulerTrigger } from "forge-sql-orm"; * * export const clearCache = () => { * return clearCacheSchedulerTrigger({ * cacheEntityName: "cache", * logRawSqlQuery: true * }); * }; * ``` * * @example * ```yaml * # In manifest.yml * scheduledTrigger: * - key: clear-cache-trigger * function: clearCache * interval: fiveMinute * * function: * - key: clearCache * handler: index.clearCache * ``` */ const clearCacheSchedulerTrigger = async (options) => { try { const newOptions = options ?? { logRawSqlQuery: false, disableOptimisticLocking: false, cacheTTL: 120, cacheEntityName: "cache", cacheEntityQueryName: "sql", cacheEntityExpirationName: "expiration", cacheEntityDataName: "data", }; if (!newOptions.cacheEntityName) { throw new Error("cacheEntityName is not configured"); } await (0, cacheUtils_1.clearExpiredCache)(newOptions); return { headers: { "Content-Type": ["application/json"] }, statusCode: 200, statusText: "OK", body: JSON.stringify({ success: true, message: "Cache cleanup completed successfully", timestamp: new Date().toISOString(), }), }; } catch (error) { // eslint-disable-next-line no-console console.error("Error during cache cleanup: ", JSON.stringify(error)); return { headers: { "Content-Type": ["application/json"] }, statusCode: 500, statusText: "Internal Server Error", body: JSON.stringify({ success: false, error: error instanceof Error ? error.message : "Unknown error during cache cleanup", timestamp: new Date().toISOString(), }), }; } }; exports.clearCacheSchedulerTrigger = clearCacheSchedulerTrigger; //# sourceMappingURL=clearCacheSchedulerTrigger.js.map