-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfactory.ts
More file actions
329 lines (285 loc) · 9.83 KB
/
Copy pathfactory.ts
File metadata and controls
329 lines (285 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* Database Provider Factory
* Creates appropriate provider instance based on connection type
* Uses dynamic imports to reduce memory footprint - providers are loaded on demand
*/
import { type DatabaseProvider, type DatabaseConnection, type ProviderOptions } from "./types";
import { DatabaseConfigError } from "./errors";
import { createSSHTunnel, closeSSHTunnel } from "@/lib/ssh/tunnel";
import { logger } from "@/lib/logger";
// ============================================================================
// Provider Factory
// ============================================================================
/**
* Create a database provider based on connection configuration
* Uses dynamic imports to load providers on-demand, reducing initial memory usage
*
* @param connection - Database connection configuration
* @param options - Optional provider options (pooling, timeout, etc.)
* @returns Promise<DatabaseProvider> instance
* @throws DatabaseConfigError if connection type is not supported
*
* @example
* // SQL Database
* const provider = await createDatabaseProvider({
* id: '1',
* name: 'My PostgreSQL',
* type: 'postgres',
* host: 'localhost',
* port: 5432,
* database: 'mydb',
* user: 'admin',
* password: 'secret',
* createdAt: new Date(),
* });
*
* // MongoDB
* const mongoProvider = await createDatabaseProvider({
* id: '2',
* name: 'My MongoDB',
* type: 'mongodb',
* connectionString: 'mongodb://localhost:27017/mydb',
* createdAt: new Date(),
* });
*
* await provider.connect();
* const result = await provider.query('SELECT * FROM users');
* await provider.disconnect();
*/
export async function createDatabaseProvider(
connection: DatabaseConnection,
options: ProviderOptions = {},
): Promise<DatabaseProvider> {
// Sanitize user-controlled values to prevent log injection
const sanitize = (v: string) => v.replace(/[\r\n]/g, " ").replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]/g, "");
console.log(`[DB] Creating ${sanitize(connection.type)} provider for "${sanitize(connection.name || "")}"`);
switch (connection.type) {
// SQL Databases - dynamically imported to reduce memory
case "postgres": {
const { PostgresProvider } = await import("./providers/sql/postgres");
return new PostgresProvider(connection, options);
}
case "mysql": {
const { MySQLProvider } = await import("./providers/sql/mysql");
return new MySQLProvider(connection, options);
}
case "sqlite": {
const { SQLiteProvider } = await import("./providers/sql/sqlite");
return new SQLiteProvider(connection, options);
}
case "oracle": {
const { OracleProvider } = await import("./providers/sql/oracle");
return new OracleProvider(connection, options);
}
case "mssql": {
const { MSSQLProvider } = await import("./providers/sql/mssql");
return new MSSQLProvider(connection, options);
}
// Document Databases - dynamically imported
case "mongodb": {
const { MongoDBProvider } = await import("./providers/document/mongodb");
return new MongoDBProvider(connection, options);
}
// Key-Value Stores - dynamically imported
case "redis": {
const { RedisProvider } = await import("./providers/keyvalue/redis");
return new RedisProvider(connection, options);
}
// Embedded databases - dynamically imported
case "libredb": {
const { LibreDBProvider } = await import("./providers/embedded/libredb");
return new LibreDBProvider(connection, options);
}
default:
throw new DatabaseConfigError(
`Unknown database type: ${connection.type}. Supported types: postgres, mysql, sqlite, oracle, mssql, mongodb, redis, libredb`,
connection.type,
);
}
}
// ============================================================================
// Provider Cache (for connection reuse)
// ============================================================================
interface CachedProvider {
provider: DatabaseProvider;
lastUsed: number;
}
const providerCache = new Map<string, CachedProvider>();
/** Idle timeout: evict providers unused for 30 minutes */
const IDLE_TIMEOUT_MS = 30 * 60 * 1000;
/** Sweep interval: check for idle providers every 5 minutes */
const SWEEP_INTERVAL_MS = 5 * 60 * 1000;
let sweepTimer: ReturnType<typeof setInterval> | null = null;
/**
* Evict providers that have been idle longer than maxIdleMs.
* Called by the periodic sweep timer, but also exported for direct testing.
*
* @returns number of evicted providers
*/
export async function evictIdleProviders(maxIdleMs: number = IDLE_TIMEOUT_MS): Promise<number> {
const now = Date.now();
let evicted = 0;
for (const [id, entry] of providerCache) {
if (now - entry.lastUsed >= maxIdleMs) {
logger.info(`[DB] Evicting idle provider: ${id} (idle ${Math.round((now - entry.lastUsed) / 60000)}min)`);
try {
await entry.provider.disconnect();
} catch (error) {
logger.warn(`[DB] Error disconnecting idle provider ${id}`, { connectionId: id, error: String(error) });
}
providerCache.delete(id);
// Also close SSH tunnel
try {
await closeSSHTunnel(id);
} catch {
/* ignore */
}
evicted++;
}
}
// Stop sweeping if cache is empty
if (providerCache.size === 0 && sweepTimer) {
clearInterval(sweepTimer);
sweepTimer = null;
}
return evicted;
}
function startIdleSweep(): void {
if (sweepTimer) return;
sweepTimer = setInterval(() => {
void evictIdleProviders();
}, SWEEP_INTERVAL_MS);
// Allow process to exit even if timer is running
if (sweepTimer && typeof sweepTimer === "object" && "unref" in sweepTimer) {
sweepTimer.unref();
}
}
/**
* Get or create a database provider with caching
* Useful for API routes to reuse connections
*
* @param connection - Database connection configuration
* @param options - Optional provider options
* @returns Cached or new DatabaseProvider instance
*/
export async function getOrCreateProvider(
connection: DatabaseConnection,
options: ProviderOptions = {},
): Promise<DatabaseProvider> {
const cacheKey = connection.id;
// Check cache
const cached = providerCache.get(cacheKey);
if (cached?.provider.isConnected()) {
cached.lastUsed = Date.now();
return cached.provider;
}
// If SSH tunnel is configured, create tunnel first and rewrite connection
let effectiveConnection = connection;
let tunnel: Awaited<ReturnType<typeof createSSHTunnel>> | null = null;
if (connection.sshTunnel?.enabled && connection.host && connection.port) {
tunnel = await createSSHTunnel(connection.id, connection.sshTunnel, connection.host, connection.port);
// Rewrite connection to point to local tunnel endpoint
effectiveConnection = {
...connection,
host: tunnel.localHost,
port: tunnel.localPort,
};
}
// Create new provider (async - dynamically loads the provider module)
const provider = await createDatabaseProvider(effectiveConnection, options);
try {
await provider.connect();
} catch (error) {
// Clean up SSH tunnel if provider connect fails to prevent FD leak
if (tunnel) {
await tunnel.close().catch(() => {});
}
throw error;
}
// Cache it
providerCache.set(cacheKey, { provider, lastUsed: Date.now() });
// Start idle sweep if not already running
startIdleSweep();
return provider;
}
/**
* Remove a provider from cache and disconnect
*/
export async function removeProvider(connectionId: string): Promise<void> {
const cached = providerCache.get(connectionId);
if (cached) {
try {
await cached.provider.disconnect();
} catch (error) {
logger.warn(`Error disconnecting provider ${connectionId}`, { connectionId, error: String(error) });
}
providerCache.delete(connectionId);
}
// Close SSH tunnel if exists
try {
await closeSSHTunnel(connectionId);
} catch (error) {
logger.warn(`Error closing SSH tunnel for ${connectionId}`, { connectionId, error: String(error) });
}
}
/**
* Clear all cached providers
*/
export async function clearProviderCache(): Promise<void> {
// Stop idle sweep
if (sweepTimer) {
clearInterval(sweepTimer);
sweepTimer = null;
}
const disconnectPromises: Promise<void>[] = [];
for (const [id, entry] of providerCache) {
disconnectPromises.push(
entry.provider.disconnect().catch((error) => {
console.error(`[DB] Error disconnecting provider ${id}:`, error);
}),
);
}
await Promise.all(disconnectPromises);
providerCache.clear();
}
/**
* Get cache statistics
*/
export function getProviderCacheStats(): { size: number; connections: string[] } {
return {
size: providerCache.size,
connections: Array.from(providerCache.keys()),
};
}
// ============================================================================
// Graceful Shutdown
// ============================================================================
let shutdownRegistered = false;
/**
* Register process signal handlers for graceful shutdown.
* Safe to call multiple times — handlers are only registered once.
*/
export function registerShutdownHandlers(): void {
if (shutdownRegistered) return;
shutdownRegistered = true;
const shutdown = async (signal: string) => {
logger.info(`[DB] Received ${signal}, closing all database connections...`);
try {
await clearProviderCache();
logger.info("[DB] All database connections closed gracefully");
} catch (error) {
logger.error("[DB] Error during graceful shutdown", { error: String(error) });
}
process.exit(0);
};
process.on("SIGTERM", () => {
void shutdown("SIGTERM");
});
process.on("SIGINT", () => {
void shutdown("SIGINT");
});
}
// Auto-register on server-side (not during tests)
if (typeof process !== "undefined" && process.env.NODE_ENV !== "test") {
registerShutdownHandlers();
}