import { EventEmitter } from 'events'; import { PersistenceManager } from './persistence.js'; import type { BreezeDBOptions, BatchOperation, BatchResult, BreezeDBEventMap } from './types.js'; /** * BreezeDB - A lightweight, feature-rich persistent key-value store * * Provides a simple key-value database with advanced features: * - TTL (Time To Live) support with automatic expiration * - Optional compression with gzip * - Optional encryption with AES-256-CBC * - Batch operations for efficiency * - Event system for monitoring operations * - Atomic writes with backup and recovery * * @example * ```typescript * const db = new BreezeDB('./data.json'); * db.set('user:1', { name: 'Alice', age: 30 }); * const user = db.get('user:1'); * ``` */ declare class BreezeDB extends EventEmitter { /** Path to the main database file */ readonly filePath: string; /** Path to the backup file */ readonly backupPath: string; /** Database configuration options with defaults applied */ readonly options: Required; /** TTL manager for handling key expiration */ private ttlManager; /** Persistence manager for file I/O operations */ persistenceManager: PersistenceManager; /** In-memory key-value store */ private data; /** Flag indicating if data has been modified since last save */ private isDirty; /** Timer for automatic saving */ private autoSaveTimer; /** Bound cleanup function for process exit handling */ private boundCleanup; /** * Creates a new BreezeDB instance * @param filePath - Path to the database file (default: './breezedb.json') * @param options - Configuration options for the database * @throws Error if encryption is enabled but no encryption key is provided */ constructor(filePath?: string, options?: BreezeDBOptions); /** * Set a key-value pair with optional TTL * @template T - Type of the value being stored * @param key - The key to store the value under * @param value - The value to store * @param ttl - Time to live in seconds (null for permanent) * @returns This BreezeDB instance for method chaining * @throws Error if key is invalid or TTL is not a positive number * @fires BreezeDB#set */ set(key: string, value: T, ttl?: number | null): this; /** * Get a value by key * @template T - Expected type of the returned value * @param key - The key to retrieve * @returns The value associated with the key, or undefined if not found or expired * @throws Error if key is invalid * @fires BreezeDB#get */ get(key: string): T | undefined; /** * Check if key exists and is not expired * @param key - The key to check * @returns True if the key exists and is not expired, false otherwise * @throws Error if key is invalid */ has(key: string): boolean; /** * Delete a key * @param key - The key to delete * @returns True if the key existed and was deleted, false if it didn't exist * @throws Error if key is invalid * @fires BreezeDB#delete */ delete(key: string): boolean; /** * Clear all data * @returns This BreezeDB instance for method chaining * @fires BreezeDB#clear */ clear(): this; /** * Execute multiple operations atomically * @param operations - Array of operations to execute * @returns Array of results for each operation * @throws Error if operations array is invalid * @fires BreezeDB#batch */ batch(operations: BatchOperation[]): BatchResult[]; /** * Set TTL for an existing key * @param key - The key to set TTL for * @param ttl - TTL duration in seconds * @returns True if TTL was set, false if key doesn't exist * @throws Error if TTL is not a positive number * @fires BreezeDB#ttl-set */ setTTL(key: string, ttl: number): boolean; /** * Get remaining TTL for a key * @param key - The key to check TTL for * @returns Remaining TTL in seconds, or null if no TTL set or key doesn't exist */ getTTL(key: string): number | null; /** * Clear TTL for a key * @param key - The key to clear TTL for * @returns True if TTL was cleared, false if no TTL was set * @fires BreezeDB#ttl-cleared */ clearTTL(key: string): boolean; /** * Get all non-expired keys * @returns Array of all keys that are not expired */ keys(): string[]; /** * Get all non-expired values * @returns Array of all values for non-expired keys */ values(): any[]; /** * Get all non-expired entries * @returns Array of [key, value] pairs for non-expired keys */ entries(): Array<[string, any]>; /** * Get number of non-expired keys * @returns Count of non-expired keys in the database */ size(): number; /** * Check if database is empty * @returns True if database has no non-expired keys, false otherwise */ isEmpty(): boolean; /** * Save database to disk asynchronously * @returns Promise that resolves when save is complete * @fires BreezeDB#save */ save(): Promise; /** * Save database to disk synchronously * @throws Error if save fails * @fires BreezeDB#save */ saveSync(): void; /** * Load database from disk asynchronously * @returns Promise that resolves when load is complete * @fires BreezeDB#load */ load(): Promise; /** * Load database from disk synchronously * @throws Error if load fails * @fires BreezeDB#load */ loadSync(): void; /** * Add event listener with proper typing * @template K - Event name type * @param event - The event name to listen for * @param listener - The callback function for the event * @returns This BreezeDB instance for method chaining */ on(event: K, listener: (data: BreezeDBEventMap[K]) => void): this; /** * Emit event with proper typing * @template K - Event name type * @param event - The event name to emit * @param data - The event data to emit * @returns True if the event had listeners, false otherwise */ emit(event: K, data: BreezeDBEventMap[K]): boolean; /** * Clean up expired keys */ private cleanupExpired; /** * Mark database as dirty and schedule auto-save */ private markDirty; /** * Start auto-save functionality */ private startAutoSave; /** * Cleanup resources and save pending changes * Called automatically on process exit or when close() is called */ private cleanup; /** * Close database and clean up resources * Saves any pending changes and stops all timers and listeners */ close(): void; } export default BreezeDB; //# sourceMappingURL=breezedb.d.ts.map