@wgtechlabs/nuvex 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +427 -0
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/cjs/core/client.js +981 -0
  5. package/dist/cjs/core/client.js.map +1 -0
  6. package/dist/cjs/core/database.js +297 -0
  7. package/dist/cjs/core/database.js.map +1 -0
  8. package/dist/cjs/core/engine.js +1202 -0
  9. package/dist/cjs/core/engine.js.map +1 -0
  10. package/dist/cjs/core/index.js +35 -0
  11. package/dist/cjs/core/index.js.map +1 -0
  12. package/dist/cjs/index.js +109 -0
  13. package/dist/cjs/index.js.map +1 -0
  14. package/dist/cjs/interfaces/index.js +12 -0
  15. package/dist/cjs/interfaces/index.js.map +1 -0
  16. package/dist/cjs/layers/index.js +22 -0
  17. package/dist/cjs/layers/index.js.map +1 -0
  18. package/dist/cjs/layers/memory.js +388 -0
  19. package/dist/cjs/layers/memory.js.map +1 -0
  20. package/dist/cjs/layers/postgres.js +492 -0
  21. package/dist/cjs/layers/postgres.js.map +1 -0
  22. package/dist/cjs/layers/redis.js +388 -0
  23. package/dist/cjs/layers/redis.js.map +1 -0
  24. package/dist/cjs/types/index.js +52 -0
  25. package/dist/cjs/types/index.js.map +1 -0
  26. package/dist/esm/core/client.js +944 -0
  27. package/dist/esm/core/client.js.map +1 -0
  28. package/dist/esm/core/database.js +289 -0
  29. package/dist/esm/core/database.js.map +1 -0
  30. package/dist/esm/core/engine.js +1198 -0
  31. package/dist/esm/core/engine.js.map +1 -0
  32. package/dist/esm/core/index.js +16 -0
  33. package/dist/esm/core/index.js.map +1 -0
  34. package/dist/esm/index.js +87 -0
  35. package/dist/esm/index.js.map +1 -0
  36. package/dist/esm/interfaces/index.js +11 -0
  37. package/dist/esm/interfaces/index.js.map +1 -0
  38. package/dist/esm/layers/index.js +16 -0
  39. package/dist/esm/layers/index.js.map +1 -0
  40. package/dist/esm/layers/memory.js +384 -0
  41. package/dist/esm/layers/memory.js.map +1 -0
  42. package/dist/esm/layers/postgres.js +485 -0
  43. package/dist/esm/layers/postgres.js.map +1 -0
  44. package/dist/esm/layers/redis.js +384 -0
  45. package/dist/esm/layers/redis.js.map +1 -0
  46. package/dist/esm/types/index.js +49 -0
  47. package/dist/esm/types/index.js.map +1 -0
  48. package/dist/types/core/client.d.ts +561 -0
  49. package/dist/types/core/client.d.ts.map +1 -0
  50. package/dist/types/core/database.d.ts +130 -0
  51. package/dist/types/core/database.d.ts.map +1 -0
  52. package/dist/types/core/engine.d.ts +450 -0
  53. package/dist/types/core/engine.d.ts.map +1 -0
  54. package/dist/types/core/index.d.ts +13 -0
  55. package/dist/types/core/index.d.ts.map +1 -0
  56. package/dist/types/index.d.ts +85 -0
  57. package/dist/types/index.d.ts.map +1 -0
  58. package/dist/types/interfaces/index.d.ts +209 -0
  59. package/dist/types/interfaces/index.d.ts.map +1 -0
  60. package/dist/types/layers/index.d.ts +16 -0
  61. package/dist/types/layers/index.d.ts.map +1 -0
  62. package/dist/types/layers/memory.d.ts +261 -0
  63. package/dist/types/layers/memory.d.ts.map +1 -0
  64. package/dist/types/layers/postgres.d.ts +313 -0
  65. package/dist/types/layers/postgres.d.ts.map +1 -0
  66. package/dist/types/layers/redis.d.ts +248 -0
  67. package/dist/types/layers/redis.d.ts.map +1 -0
  68. package/dist/types/types/index.d.ts +410 -0
  69. package/dist/types/types/index.d.ts.map +1 -0
  70. package/package.json +90 -0
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Nuvex - Interface Definitions
3
+ * Next-gen Unified Vault Experience
4
+ *
5
+ * Core interfaces for the multi-layer storage SDK
6
+ *
7
+ * @author Waren Gonzaga, WG Technology Labs
8
+ * @since 2025
9
+ */
10
+ import type { StorageOptions, BatchOperation, BatchResult, QueryOptions, QueryResult, NuvexConfig } from '../types/index.js';
11
+ /**
12
+ * Logger interface for consistent logging across the storage system
13
+ *
14
+ * Defines the standard logging interface that can be implemented by any
15
+ * logging library (Winston, Bunyan, console, etc.). All log methods
16
+ * accept optional metadata for structured logging.
17
+ *
18
+ * @interface Logger
19
+ */
20
+ export interface Logger {
21
+ /** Log debug information for development and troubleshooting */
22
+ debug(message: string, meta?: unknown): void;
23
+ /** Log general information about system operations */
24
+ info(message: string, meta?: unknown): void;
25
+ /** Log warnings about non-critical issues that should be addressed */
26
+ warn(message: string, meta?: unknown): void;
27
+ /** Log errors and exceptions that require immediate attention */
28
+ error(message: string, meta?: unknown): void;
29
+ }
30
+ /**
31
+ * Structured logging context
32
+ *
33
+ * Standard metadata structure for logging operations and performance data.
34
+ * Provides consistent context across all log entries.
35
+ *
36
+ * @interface LogContext
37
+ */
38
+ export interface LogContext {
39
+ /** Name of the operation being performed */
40
+ operation?: string;
41
+ /** Key involved in the operation */
42
+ key?: string;
43
+ /** Storage layer where the operation occurred */
44
+ layer?: string;
45
+ /** Duration of the operation in milliseconds */
46
+ duration?: number;
47
+ /** Whether the operation completed successfully */
48
+ success?: boolean;
49
+ /** Error message if the operation failed */
50
+ error?: string;
51
+ /** Additional operation-specific metadata */
52
+ metadata?: Record<string, unknown>;
53
+ }
54
+ /**
55
+ * Core storage interface
56
+ *
57
+ * Defines the fundamental storage operations that must be implemented by
58
+ * any storage engine. This interface provides the low-level operations
59
+ * for the multi-layer storage architecture.
60
+ *
61
+ * @interface Storage
62
+ */
63
+ export interface Storage {
64
+ /** Establish connections to all configured storage layers */
65
+ connect(): Promise<void>;
66
+ /** Close all connections and cleanup resources */
67
+ disconnect(): Promise<void>;
68
+ /** Check if the storage engine is connected and ready */
69
+ isConnected(): boolean;
70
+ /** Store a value with optional configuration */
71
+ set<T = unknown>(key: string, value: T, options?: StorageOptions): Promise<boolean>;
72
+ /** Retrieve a value from storage */
73
+ get<T = unknown>(key: string, options: StorageOptions): Promise<T | null>;
74
+ /** Delete a value from all storage layers */
75
+ delete(key: string, options: StorageOptions): Promise<boolean>;
76
+ /** Check if a key exists in any storage layer */
77
+ exists(key: string, options: StorageOptions): Promise<boolean>;
78
+ /** Set or update expiration time for a key */
79
+ expire(key: string, ttl: number): Promise<boolean>;
80
+ /** Atomically increment a numeric value */
81
+ increment(key: string, delta?: number, ttl?: number): Promise<number>;
82
+ /** Execute multiple storage operations in a batch */
83
+ setBatch(operations: BatchOperation[]): Promise<BatchResult[]>;
84
+ /** Retrieve multiple values in a batch */
85
+ getBatch(keys: string[], options: StorageOptions): Promise<BatchResult[]>;
86
+ /** Delete multiple values in a batch */
87
+ deleteBatch(keys: string[]): Promise<BatchResult[]>;
88
+ /** Execute advanced queries with filtering and pagination */
89
+ query<T = unknown>(options: QueryOptions): Promise<QueryResult<T>>;
90
+ /** Get all keys matching an optional pattern */
91
+ keys(pattern?: string): Promise<string[]>;
92
+ /** Clear all keys or keys matching a pattern */
93
+ clear(pattern?: string): Promise<number>;
94
+ /** Get current performance metrics for all layers or specific layer(s) */
95
+ getMetrics(layers?: 'memory' | 'redis' | 'postgres' | 'all' | Array<'memory' | 'redis' | 'postgres'>): Record<string, number>;
96
+ /** Reset all performance metrics to zero */
97
+ resetMetrics(): void;
98
+ /** Promote a key to a higher performance layer */
99
+ promote(key: string, targetLayer: string): Promise<boolean>;
100
+ /** Demote a key to a lower performance layer */
101
+ demote(key: string, targetLayer: string): Promise<boolean>;
102
+ /** Get information about which layer currently holds a key */
103
+ getLayerInfo(key: string): Promise<{
104
+ layer: string;
105
+ ttl?: number;
106
+ } | null>;
107
+ }
108
+ /**
109
+ * High-level store interface
110
+ *
111
+ * Extends the basic Storage interface with additional features for
112
+ * configuration management, health monitoring, and maintenance operations.
113
+ * This is typically the interface used by application developers.
114
+ *
115
+ * @interface Store
116
+ * @extends Storage
117
+ */
118
+ export interface Store extends Storage {
119
+ /** Update configuration with new settings */
120
+ configure(config: Partial<NuvexConfig>): Promise<void>;
121
+ /** Get current configuration */
122
+ getConfig(): NuvexConfig;
123
+ /** Perform comprehensive health checks on all storage layers or specific layer(s) */
124
+ healthCheck(layers?: 'memory' | 'redis' | 'postgres' | Array<'memory' | 'redis' | 'postgres'>): Promise<Record<string, boolean>>;
125
+ /** Clean up expired entries and optimize storage */
126
+ cleanup(): Promise<{
127
+ cleaned: number;
128
+ errors: number;
129
+ }>;
130
+ /** Compact storage and optimize performance */
131
+ compact(): Promise<void>;
132
+ /** Create a backup of all stored data */
133
+ backup(destination?: string): Promise<string>;
134
+ /** Restore data from a backup */
135
+ restore(source: string): Promise<boolean>;
136
+ }
137
+ /**
138
+ * Storage Layer interface for modular storage implementations
139
+ *
140
+ * Defines the contract that all storage layer implementations must follow.
141
+ * Each layer (Memory, Redis, PostgreSQL) implements this interface to provide
142
+ * consistent operations across the storage hierarchy.
143
+ *
144
+ * This interface supports:
145
+ * - Basic CRUD operations (get, set, delete)
146
+ * - Existence checks
147
+ * - Optional clear operation for cache layers
148
+ * - Health check via ping() method
149
+ *
150
+ * @interface StorageLayerInterface
151
+ * @since 1.0.0
152
+ */
153
+ export interface StorageLayerInterface {
154
+ /**
155
+ * Retrieve a value from this storage layer
156
+ * @param key - The key to retrieve
157
+ * @returns Promise resolving to the value or null if not found
158
+ */
159
+ get(key: string): Promise<unknown>;
160
+ /**
161
+ * Store a value in this storage layer
162
+ * @param key - The key to store
163
+ * @param value - The value to store
164
+ * @param ttlSeconds - Optional TTL in seconds
165
+ * @returns Promise resolving when the operation completes
166
+ */
167
+ set(key: string, value: unknown, ttlSeconds?: number): Promise<void>;
168
+ /**
169
+ * Delete a value from this storage layer
170
+ * @param key - The key to delete
171
+ * @returns Promise resolving when the operation completes
172
+ */
173
+ delete(key: string): Promise<void>;
174
+ /**
175
+ * Check if a key exists in this storage layer
176
+ * @param key - The key to check
177
+ * @returns Promise resolving to true if the key exists
178
+ */
179
+ exists(key: string): Promise<boolean>;
180
+ /**
181
+ * Clear all data from this storage layer (optional for some layers)
182
+ * @returns Promise resolving when the operation completes
183
+ */
184
+ clear?(): Promise<void>;
185
+ /**
186
+ * Get all keys matching an optional pattern from this storage layer
187
+ * @param pattern - Optional glob pattern for key matching (e.g., 'user:*')
188
+ * @returns Promise resolving to an array of matching keys
189
+ */
190
+ keys?(pattern?: string): Promise<string[]>;
191
+ /**
192
+ * Health check for this storage layer
193
+ * @returns Promise resolving to true if the layer is healthy and operational
194
+ */
195
+ ping(): Promise<boolean>;
196
+ /**
197
+ * Atomically increment a numeric value
198
+ *
199
+ * This operation is thread-safe and prevents race conditions.
200
+ * If the key doesn't exist, it's initialized to 0 before incrementing.
201
+ *
202
+ * @param key - The key to increment
203
+ * @param delta - The amount to increment by (can be negative for decrement)
204
+ * @param ttlSeconds - Optional TTL in seconds for the key
205
+ * @returns Promise resolving to the new value after increment
206
+ */
207
+ increment?(key: string, delta: number, ttlSeconds?: number): Promise<number>;
208
+ }
209
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,cAAc,EAEd,cAAc,EACd,WAAW,EACX,YAAY,EACZ,WAAW,EACX,WAAW,EAEZ,MAAM,mBAAmB,CAAC;AAI3B;;;;;;;;GAQG;AACH,MAAM,WAAW,MAAM;IACrB,gEAAgE;IAChE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC7C,sDAAsD;IACtD,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC5C,sEAAsE;IACtE,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC5C,iEAAiE;IACjE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC9C;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,UA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EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAErE;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC;;;OAGG;IACH,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;;;OAIG;IACH,IAAI,CAAC,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAE3C;;;OAGG;IACH,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAEzB;;;;;;;;;;OAUG;IACH,SAAS,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC9E"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Nuvex - Storage Layers
3
+ * Next-gen Unified Vault Experience
4
+ *
5
+ * Exports modular storage layer implementations for the 3-tier architecture.
6
+ * Each layer provides consistent interface but different performance and
7
+ * persistence characteristics.
8
+ *
9
+ * @author Waren Gonzaga, WG Technology Labs
10
+ * @version 1.0.0
11
+ * @since 2025
12
+ */
13
+ export { MemoryStorage } from './memory.js';
14
+ export { RedisStorage } from './redis.js';
15
+ export { PostgresStorage } from './postgres.js';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/layers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,261 @@
1
+ /**
2
+ * Nuvex - Memory Storage Layer (L1)
3
+ * Next-gen Unified Vault Experience
4
+ *
5
+ * In-memory cache layer with LRU (Least Recently Used) eviction policy.
6
+ * Provides the fastest access tier in the multi-layer storage architecture.
7
+ *
8
+ * Features:
9
+ * - LRU eviction with configurable maxSize
10
+ * - TTL-based expiration
11
+ * - Sub-millisecond access times
12
+ * - Automatic cleanup of expired entries
13
+ *
14
+ * @author Waren Gonzaga, WG Technology Labs
15
+ * @since 2025
16
+ */
17
+ import type { StorageLayerInterface, Logger } from '../interfaces/index.js';
18
+ /**
19
+ * Memory Storage Layer - L1 Cache with LRU Eviction
20
+ *
21
+ * Implements an in-memory cache with Least Recently Used (LRU) eviction policy.
22
+ * This layer provides the fastest access times but has limited capacity defined
23
+ * by maxSize. When the cache is full, the least recently accessed item is evicted
24
+ * to make room for new entries.
25
+ *
26
+ * **LRU Implementation:**
27
+ * - Uses JavaScript Map which maintains insertion order
28
+ * - On get(), moves accessed entry to end (marks as recently used)
29
+ * - On set(), evicts first entry (oldest/least recently used) when full
30
+ * - Combines LRU with TTL-based expiration for optimal memory management
31
+ *
32
+ * **Performance Characteristics:**
33
+ * - Get: O(1) average, O(n) worst case due to delete+set for LRU
34
+ * - Set: O(1) with occasional O(1) eviction
35
+ * - Memory: O(maxSize)
36
+ *
37
+ * @implements {StorageLayerInterface}
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * // Create memory layer with 1000 entry limit
42
+ * const memory = new MemoryStorage(1000);
43
+ *
44
+ * // Store with 60 second TTL
45
+ * await memory.set('user:123', userData, 60);
46
+ *
47
+ * // Retrieve (marks as recently used)
48
+ * const data = await memory.get('user:123');
49
+ *
50
+ * // Check health
51
+ * const isHealthy = await memory.ping(); // Always true
52
+ * ```
53
+ *
54
+ * @class MemoryStorage
55
+ * @since 1.0.0
56
+ */
57
+ export declare class MemoryStorage implements StorageLayerInterface {
58
+ /** In-memory cache using Map for LRU ordering */
59
+ private cache;
60
+ /** Maximum number of entries before LRU eviction kicks in */
61
+ private readonly maxSize;
62
+ /** Optional logger for debugging and monitoring */
63
+ private logger;
64
+ /**
65
+ * Creates a new MemoryStorage instance
66
+ *
67
+ * @param maxSize - Maximum number of entries to store (default: 10,000)
68
+ * @param logger - Optional logger for debugging
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * // Default configuration
73
+ * const memory = new MemoryStorage();
74
+ *
75
+ * // Custom size limit
76
+ * const memory = new MemoryStorage(5000);
77
+ *
78
+ * // With logging
79
+ * const memory = new MemoryStorage(10000, console);
80
+ * ```
81
+ */
82
+ constructor(maxSize?: number, logger?: Logger | null);
83
+ /**
84
+ * Retrieve a value from memory cache
85
+ *
86
+ * Implements LRU by moving accessed entries to the end of the Map,
87
+ * marking them as recently used. Automatically removes expired entries.
88
+ *
89
+ * @param key - The key to retrieve
90
+ * @returns Promise resolving to the value or null if not found/expired
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * const value = await memory.get('user:123');
95
+ * if (value !== null) {
96
+ * console.log('Cache hit!');
97
+ * }
98
+ * ```
99
+ */
100
+ get(key: string): Promise<unknown>;
101
+ /**
102
+ * Store a value in memory cache
103
+ *
104
+ * Implements LRU eviction when cache is full. If the cache has reached
105
+ * maxSize and the key doesn't already exist, the oldest entry (first in Map)
106
+ * is evicted to make room for the new entry.
107
+ *
108
+ * @param key - The key to store
109
+ * @param value - The value to store
110
+ * @param ttlSeconds - Optional TTL in seconds
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * // Store without TTL
115
+ * await memory.set('config:app', configData);
116
+ *
117
+ * // Store with 5 minute TTL
118
+ * await memory.set('session:abc', sessionData, 300);
119
+ * ```
120
+ */
121
+ set(key: string, value: unknown, ttlSeconds?: number): Promise<void>;
122
+ /**
123
+ * Delete a value from memory cache
124
+ *
125
+ * @param key - The key to delete
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * await memory.delete('user:123');
130
+ * ```
131
+ */
132
+ delete(key: string): Promise<void>;
133
+ /**
134
+ * Check if a key exists in memory cache
135
+ *
136
+ * Verifies existence and checks if the entry has expired.
137
+ * Automatically removes expired entries.
138
+ *
139
+ * @param key - The key to check
140
+ * @returns Promise resolving to true if the key exists and is not expired
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * if (await memory.exists('user:123')) {
145
+ * console.log('Key exists in memory');
146
+ * }
147
+ * ```
148
+ */
149
+ exists(key: string): Promise<boolean>;
150
+ /**
151
+ * Clear all entries from memory cache
152
+ *
153
+ * Removes all stored entries, resetting the cache to empty state.
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * await memory.clear();
158
+ * console.log('All memory cache cleared');
159
+ * ```
160
+ */
161
+ clear(): Promise<void>;
162
+ /**
163
+ * Get all keys matching an optional pattern
164
+ *
165
+ * Returns all non-expired keys from the memory cache, optionally filtered
166
+ * by a glob pattern. Supports '*' (match any characters) and '?' (match single character).
167
+ *
168
+ * @param pattern - Optional glob pattern (default: '*' returns all keys)
169
+ * @returns Promise resolving to array of matching keys
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * // Get all keys
174
+ * const allKeys = await memory.keys();
175
+ *
176
+ * // Get keys matching pattern
177
+ * const userKeys = await memory.keys('user:*');
178
+ * ```
179
+ */
180
+ keys(pattern?: string): Promise<string[]>;
181
+ /**
182
+ * Health check for memory storage layer
183
+ *
184
+ * Memory storage is always available if the application is running,
185
+ * so this method always returns true unless there's a critical failure.
186
+ *
187
+ * @returns Promise resolving to true (memory is always available)
188
+ *
189
+ * @example
190
+ * ```typescript
191
+ * const isHealthy = await memory.ping();
192
+ * console.log('Memory layer healthy:', isHealthy);
193
+ * ```
194
+ */
195
+ ping(): Promise<boolean>;
196
+ /**
197
+ * Get current cache size
198
+ *
199
+ * Returns the number of entries currently stored in the cache.
200
+ * Useful for monitoring and debugging.
201
+ *
202
+ * @returns Current number of entries in cache
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * console.log(`Cache usage: ${memory.size()}/${memory.getMaxSize()}`);
207
+ * ```
208
+ */
209
+ size(): number;
210
+ /**
211
+ * Get maximum cache size
212
+ *
213
+ * Returns the configured maximum number of entries.
214
+ *
215
+ * @returns Maximum cache size
216
+ */
217
+ getMaxSize(): number;
218
+ /**
219
+ * Clean up expired entries
220
+ *
221
+ * Iterates through all entries and removes expired ones.
222
+ * This is useful for manual cleanup or scheduled maintenance.
223
+ *
224
+ * @returns Number of entries removed
225
+ *
226
+ * @example
227
+ * ```typescript
228
+ * const cleaned = await memory.cleanup();
229
+ * console.log(`Removed ${cleaned} expired entries`);
230
+ * ```
231
+ */
232
+ cleanup(): Promise<number>;
233
+ /**
234
+ * Atomically increment a numeric value
235
+ *
236
+ * This operation is thread-safe for single-instance deployments.
237
+ * If the key doesn't exist or is expired, it's initialized to 0 before incrementing.
238
+ *
239
+ * @param key - The key to increment
240
+ * @param delta - The amount to increment by (can be negative for decrement)
241
+ * @param ttlSeconds - Optional TTL in seconds
242
+ * @returns Promise resolving to the new value after increment
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * const newValue = await memory.increment('counter', 1, 60);
247
+ * console.log(`Counter is now: ${newValue}`);
248
+ * ```
249
+ */
250
+ increment(key: string, delta: number, ttlSeconds?: number): Promise<number>;
251
+ /**
252
+ * Log a message if logger is configured
253
+ *
254
+ * @private
255
+ * @param level - Log level
256
+ * @param message - Log message
257
+ * @param meta - Optional metadata
258
+ */
259
+ private log;
260
+ }
261
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../src/layers/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAa5E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,qBAAa,aAAc,YAAW,qBAAqB;IACzD,iDAAiD;IACj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