axiodb
Version:
The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor
79 lines (78 loc) • 2.78 kB
TypeScript
import { ErrorInterface, SuccessInterface } from "../../config/Interfaces/Helper/response.helper.interface";
import { SessionOptions } from "../../config/Interfaces/Transaction/transaction.interface";
import Transaction from "./Transaction.service";
/**
* Session provides MongoDB-like session management for transactions.
* Supports automatic retry, timeout handling, and the withTransaction pattern.
*/
export default class Session {
private readonly sessionId;
private readonly collectionPath;
private readonly isEncrypted;
private readonly encryptionKey?;
private readonly options;
private readonly ResponseHelper;
private readonly startTime;
private isActive;
private currentTransaction;
constructor(collectionPath: string, isEncrypted?: boolean, encryptionKey?: string, options?: SessionOptions);
/**
* Gets the session ID
*/
getSessionId(): string;
/**
* Checks if the session is still active
*/
isSessionActive(): boolean;
/**
* Starts a new transaction within this session.
* Only one transaction can be active at a time per session.
*
* @returns A new Transaction instance
* @throws Error if session is inactive or another transaction is active
*/
startTransaction(): Transaction;
/**
* Executes a callback function within a transaction context.
* Automatically commits on success or rolls back on error.
* Supports automatic retry on transient errors.
*
* @param callback - Async function that receives the transaction and performs operations
* @returns Promise resolving to success/error result
*
* @example
* ```typescript
* const session = collection.startSession();
* await session.withTransaction(async (txn) => {
* await txn.insert({ name: 'Alice' });
* await txn.update({ name: 'Bob' }, { age: 30 });
* });
* ```
*/
withTransaction(callback: (txn: Transaction) => Promise<void>): Promise<SuccessInterface | ErrorInterface>;
/**
* Commits the current transaction if one is active.
*
* @returns Promise resolving to success/error result
*/
commitTransaction(): Promise<SuccessInterface | ErrorInterface>;
/**
* Rolls back the current transaction if one is active.
*
* @returns Promise resolving to success/error result
*/
abortTransaction(): Promise<SuccessInterface | ErrorInterface>;
/**
* Ends the session and cleans up resources.
* Any active transaction will be rolled back.
*/
endSession(): Promise<void>;
/**
* Determines if an error is retryable (transient)
*/
private isRetryableError;
/**
* Simple sleep utility
*/
private sleep;
}