UNPKG

@aradox/multi-orm

Version:

Type-safe ORM with multi-datasource support, row-level security, and Prisma-like API for PostgreSQL, SQL Server, and HTTP APIs

179 lines 6.64 kB
"use strict"; /** * Computed Field Executor * * Loads and executes computed field resolvers defined in the schema. * Supports both synchronous and asynchronous resolvers with timeout control. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ComputedExecutor = void 0; const path = __importStar(require("path")); const logger_1 = require("../utils/logger"); class ComputedExecutor { resolverCache = new Map(); options; constructor(options) { this.options = { basePath: options.basePath, timeout: options.timeout ?? 5000, throwOnTimeout: options.throwOnTimeout ?? false, cacheResolvers: options.cacheResolvers ?? true }; } /** * Execute computed fields on a set of rows */ async executeComputedFields(rows, model, fields, select) { if (!rows || rows.length === 0) { return rows; } // Find computed fields that need to be executed const computedFields = Object.entries(fields).filter(([name, field]) => { // Only execute if field has computed config if (!field.computed) return false; // If select is specified, only execute selected computed fields if (select && !select[name]) return false; return true; }); if (computedFields.length === 0) { return rows; } logger_1.logger.debug('runtime', `Executing ${computedFields.length} computed field(s) for model ${model}`); // Execute computed fields for each row const results = await Promise.all(rows.map(row => this.executeForRow(row, model, computedFields))); return results; } /** * Execute computed fields for a single row */ async executeForRow(row, model, computedFields) { const result = { ...row }; for (const [fieldName, field] of computedFields) { try { const value = await this.executeField(row, model, fieldName, field); result[fieldName] = value; } catch (error) { const err = error; logger_1.logger.error('runtime', `Error executing computed field ${model}.${fieldName}: ${err.message}`); if (this.options.throwOnTimeout && err.message.includes('timeout')) { throw error; } // Set to undefined on error result[fieldName] = undefined; } } return result; } /** * Execute a single computed field */ async executeField(row, model, fieldName, field) { const computed = field.computed; // Load resolver function const resolver = await this.loadResolver(computed.resolver); // Build context const context = { row, model }; // Execute resolver if (computed.async) { // Async resolver with timeout const timeout = computed.timeout ?? this.options.timeout; return this.executeWithTimeout(resolver, context, timeout); } else { // Synchronous resolver return resolver(context); } } /** * Load a resolver function from file path */ async loadResolver(resolverPath) { // Check cache if (this.options.cacheResolvers && this.resolverCache.has(resolverPath)) { return this.resolverCache.get(resolverPath); } // Resolve absolute path const absolutePath = path.isAbsolute(resolverPath) ? resolverPath : path.resolve(this.options.basePath, resolverPath); logger_1.logger.debug('runtime', `Loading computed resolver: ${absolutePath}`); try { // Dynamic import const module = await Promise.resolve(`${absolutePath}`).then(s => __importStar(require(s))); const resolver = module.default || module; if (typeof resolver !== 'function') { throw new Error(`Resolver at ${resolverPath} must export a function`); } // Cache resolver if (this.options.cacheResolvers) { this.resolverCache.set(resolverPath, resolver); } return resolver; } catch (error) { const err = error; logger_1.logger.error('runtime', `Failed to load resolver ${resolverPath}: ${err.message}`); throw new Error(`Failed to load computed field resolver: ${resolverPath}`); } } /** * Execute resolver with timeout */ async executeWithTimeout(resolver, context, timeout) { return Promise.race([ resolver(context), new Promise((_, reject) => { setTimeout(() => { reject(new Error(`Computed field ${context.model} timed out after ${timeout}ms`)); }, timeout); }) ]); } /** * Clear resolver cache */ clearCache() { this.resolverCache.clear(); } } exports.ComputedExecutor = ComputedExecutor; //# sourceMappingURL=computed-executor.js.map