import type { APIGatewayEvent, Context, DynamoDBStreamEvent, SQSEvent } from 'aws-lambda'; import Request from '../API/Request.js'; import Response, { ResponseErrorType } from '../API/Response.js'; import type { DatabaseImplType, DatabaseTransactionType, DatabaseType, DbConfig } from '../Database/types.js'; import Logger, { LoggerConfig } from '../Logger/Logger.js'; import Publisher, { PublisherConfig } from '../Publisher/Publisher.js'; /** * Defines a type for executing a transaction and returning a promise with the response. * @param {TransactionType} transaction - The transaction to execute. * @returns A promise that resolves to the response of the transaction. */ export type TransactionExecution = (transaction: TransactionType) => Promise | Response | MiscRespType>; /** * Represents the configuration options for a transaction. * @typedef {Object} TransactionConfig * @property {boolean} [throwOnErrors] - Whether to throw an error if there are any errors during the transaction. * @property {boolean} [syncReturn] - Whether to return the result of the transaction synchronously. * @property {boolean} [skipCleanTmp] - Whether to skip cleaning the temporary folder, in EventProcessor. * @property {LoggerConfig} [logger] - The configuration options for the logger. * @property {PublisherConfig} [publisher] - The configuration options for the publisher. */ export type TransactionConfig = { throwOnErrors?: boolean; syncReturn?: boolean; skipCleanTmp?: boolean; logger?: LoggerConfig; publisher?: PublisherConfig; }; /** * Represents a simple string dictionary with string values */ export type StringMap = { [key: string]: string | null; }; /** * Represents a transaction object that handles the execution of a request and manages the response. * @template InputType - The type of the input data for the transaction. * @template ResponseInnerType - The type of the inner response data for the transaction. * @template MiscRespType - The type of miscellaneous response data for the transaction. */ export default class Transaction { /** * The instance of the DatabaseManager class used for managing the database. */ private databaseManager; /** * An array of database transactions. * @type {DatabaseTransaction[]} */ private transactions; /** * Represents an event object. * @private * @type {any} */ private event; /** * The context object for the current instance. */ private context; /** * The response object that can hold different types of responses. * @type {Response | MiscRespType | null} */ private response; /** * A private boolean variable indicating whether the return value of a synchronous operation * should be synchronized with the calling thread. */ private syncReturn; /** * A boolean flag indicating whether retroactive errors are enabled or not. * @private */ private retrowErrors; /** * A logger object used for logging messages, errors, and other information. * @readonly */ readonly logger: Logger; /** * The request object for making a request of type InputType. * @readonly */ readonly request: Request; /** * The publisher of the content. */ readonly publisher: Publisher; /** * A function that acts as a response proxy for a given response object. * @param {Response} response - The response object to proxy. * @returns A promise that resolves to void. */ responseProxy: ((response: Response) => Promise) | null; /** * Constructs a new instance of the Transaction class. * @param {APIGatewayEvent | SQSEvent | DynamoDBStreamEvent} event - The event object passed to the Lambda function. * @param {Context} context - The context object passed to the Lambda function. * @param {TransactionConfig} [config] - Optional configuration object for the transaction. * @returns None */ constructor(event: APIGatewayEvent | SQSEvent | DynamoDBStreamEvent, context: Context, config?: TransactionConfig); /** * Executes a transaction using the provided execution function and returns a promise * that resolves to the response or miscellaneous response. * @param {TransactionExecution, ResponseInnerType, MiscRespType>} executionFunc - The execution function to be executed. * @returns {Promise | MiscRespType>} - A promise that resolves to the response or miscellaneous response. */ execute(executionFunc: TransactionExecution, ResponseInnerType, MiscRespType>): Promise | MiscRespType | null>; /** * Executes a transaction using the provided execution function and handles the response. * @param {TransactionExecution, ResponseInnerType, MiscRespType>} executionFunc - The function to execute the transaction. * @returns {Promise} - A promise that resolves to a boolean indicating whether the execution failed or not. */ private iexecute; /** * Retrieves a database transaction based on the provided database configuration. * @param {DbConfig} config - The configuration object for the database. * @returns {Promise>} A promise that resolves to the database transaction. */ getDbTransaction(config: DbConfig): Promise>; /** * Retrieves a database instance based on the provided configuration. * @param {DbConfig} config - The configuration object specifying the type of database. * @returns {DatabaseImplType} A database instance based on the provided configuration. */ getDatabase(config: DbConfig): DatabaseImplType; /** * Get the remaining time in milliseconds for the current execution context. * @returns {number} The remaining time in milliseconds, or -1 if the time is not available. */ getRemainingTimeInMillis(): number; private executeDBTransactions; /** * Executes a logger flush operation with error handling and logging. * @param {Function} safeExecution - The function to execute safely. * @returns None * @throws {Error} - If `retrowErrors` is true and an error occurs during execution. */ private executeLoggerFlush; /** * Returns an error response with the specified error message and error code. * @param {string} error - The error message. * @param {string} code - The error code. * @returns {Response} - The error response. */ private getErrorResponse; }