UNPKG

smart-saga-pattern

Version:

A library implementing the Saga pattern for microservice architecture

124 lines (123 loc) 3.39 kB
import { SagaContext, StateStore } from "../types"; /** * Options for the FileSystemStateStore */ export interface FileSystemStateStoreOptions { /** * Directory to store saga state files */ storageDir: string; /** * Whether to create the storage directory if it doesn't exist * @default true */ createDirIfNotExists?: boolean; /** * File extension for state files * @default '.json' */ fileExtension?: string; /** * Whether to pretty print JSON * @default false */ prettyPrint?: boolean; /** * Whether to use synchronous file operations * @default false */ sync?: boolean; /** * Time-to-live for saga state files in milliseconds * Files older than this will be eligible for cleanup * @default 86400000 (24 hours) */ ttl?: number; /** * Maximum number of files to keep in the storage directory * When this limit is reached, older files will be deleted * @default 1000 */ maxFiles?: number; /** * Whether to automatically clean up old files when saving new ones * @default true */ autoCleanup?: boolean; /** * Cleanup probability (0-1) * Determines how often cleanup is performed when autoCleanup is true * Lower values mean less frequent cleanup * @default 0.1 (10% chance on each save operation) */ cleanupProbability?: number; } /** * File system implementation of StateStore * @template T Type of the data in the context */ export declare class FileSystemStateStore<T = Record<string, any>> implements StateStore<T> { private options; /** * Creates a new FileSystemStateStore * @param options Options for the store */ constructor(options: FileSystemStateStoreOptions); /** * Saves a Saga context * @param sagaId ID of the Saga * @param context Saga context to save */ save(sagaId: string, context: SagaContext<T>): Promise<void>; /** * Loads a Saga context * @param sagaId ID of the Saga */ load(sagaId: string): Promise<SagaContext<T> | null>; /** * Updates a Saga context * @param sagaId ID of the Saga * @param context Partial Saga context to update */ update(sagaId: string, context: Partial<SagaContext<T>>): Promise<void>; /** * Deletes a Saga context * @param sagaId ID of the Saga */ delete(sagaId: string): Promise<void>; /** * Gets the file path for a saga ID * @param sagaId ID of the Saga */ private getFilePath; /** * Ensures the storage directory exists */ private ensureDirectoryExists; /** * Serializes a context to a string * @param context Context to serialize */ private serializeContext; /** * Deserializes a string to a context * @param data String to deserialize */ private deserializeContext; /** * Cleans up old saga state files * Removes files that are: * 1. Older than the TTL * 2. Exceed the maximum number of files (keeping the newest ones) */ cleanup(): Promise<number>; /** * Gets all saga state files in the storage directory */ private getStateFiles; /** * Deletes a file * @param filePath Path to the file to delete */ private deleteFile; }