@omnitron-dev/titan-database 0.1.3 → 0.3.0

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 (72) hide show
  1. package/dist/connection-ref.d.ts +3 -0
  2. package/dist/connection-ref.d.ts.map +1 -0
  3. package/dist/connection-ref.js +70 -0
  4. package/dist/connection-ref.js.map +1 -0
  5. package/dist/database.constants.d.ts +13 -11
  6. package/dist/database.constants.d.ts.map +1 -1
  7. package/dist/database.constants.js.map +1 -1
  8. package/dist/database.decorators.d.ts +6 -0
  9. package/dist/database.decorators.d.ts.map +1 -1
  10. package/dist/database.decorators.js +63 -3
  11. package/dist/database.decorators.js.map +1 -1
  12. package/dist/database.health.d.ts.map +1 -1
  13. package/dist/database.health.js +6 -6
  14. package/dist/database.health.js.map +1 -1
  15. package/dist/database.manager.d.ts +28 -11
  16. package/dist/database.manager.d.ts.map +1 -1
  17. package/dist/database.manager.js +529 -192
  18. package/dist/database.manager.js.map +1 -1
  19. package/dist/database.module.d.ts.map +1 -1
  20. package/dist/database.module.js +65 -13
  21. package/dist/database.module.js.map +1 -1
  22. package/dist/database.types.d.ts +28 -20
  23. package/dist/database.types.d.ts.map +1 -1
  24. package/dist/database.types.js +13 -14
  25. package/dist/database.types.js.map +1 -1
  26. package/dist/exports/kysera.d.ts +2 -2
  27. package/dist/exports/kysera.d.ts.map +1 -1
  28. package/dist/exports/kysera.js +1 -1
  29. package/dist/exports/kysera.js.map +1 -1
  30. package/dist/exports/migration.d.ts +2 -0
  31. package/dist/exports/migration.d.ts.map +1 -1
  32. package/dist/exports/migration.js +1 -0
  33. package/dist/exports/migration.js.map +1 -1
  34. package/dist/exports/repository.d.ts +2 -1
  35. package/dist/exports/repository.d.ts.map +1 -1
  36. package/dist/exports/transaction.d.ts +1 -1
  37. package/dist/exports/transaction.d.ts.map +1 -1
  38. package/dist/exports/transaction.js +1 -1
  39. package/dist/exports/transaction.js.map +1 -1
  40. package/dist/index.d.ts +6 -5
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +4 -4
  43. package/dist/index.js.map +1 -1
  44. package/dist/migration/cli.d.ts +14 -0
  45. package/dist/migration/cli.d.ts.map +1 -0
  46. package/dist/migration/cli.js +154 -0
  47. package/dist/migration/cli.js.map +1 -0
  48. package/dist/migration/hardened-runner.d.ts +80 -0
  49. package/dist/migration/hardened-runner.d.ts.map +1 -0
  50. package/dist/migration/hardened-runner.js +284 -0
  51. package/dist/migration/hardened-runner.js.map +1 -0
  52. package/dist/migration/index.d.ts +5 -0
  53. package/dist/migration/index.d.ts.map +1 -0
  54. package/dist/migration/index.js +3 -0
  55. package/dist/migration/index.js.map +1 -0
  56. package/dist/plugins/sqlite-date-serializer.plugin.d.ts.map +1 -1
  57. package/dist/repository/transaction-aware.repository.d.ts +19 -3
  58. package/dist/repository/transaction-aware.repository.d.ts.map +1 -1
  59. package/dist/repository/transaction-aware.repository.js +146 -28
  60. package/dist/repository/transaction-aware.repository.js.map +1 -1
  61. package/dist/transaction/transaction.context.d.ts +5 -9
  62. package/dist/transaction/transaction.context.d.ts.map +1 -1
  63. package/dist/transaction/transaction.context.js +34 -16
  64. package/dist/transaction/transaction.context.js.map +1 -1
  65. package/dist/utils/describe-error.d.ts +2 -0
  66. package/dist/utils/describe-error.d.ts.map +1 -0
  67. package/dist/utils/describe-error.js +37 -0
  68. package/dist/utils/describe-error.js.map +1 -0
  69. package/dist/uuid-v7.d.ts +2 -2
  70. package/dist/uuid-v7.d.ts.map +1 -1
  71. package/dist/uuid-v7.js.map +1 -1
  72. package/package.json +22 -22
@@ -7,16 +7,58 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
+ var DatabaseManager_1;
10
11
  import { Kysely, PostgresDialect, MysqlDialect, SqliteDialect, CamelCasePlugin, sql } from 'kysely';
11
- import { Pool } from 'pg';
12
- import * as mysql from 'mysql2';
13
- import BetterSqlite3 from 'better-sqlite3';
12
+ import { describeError } from './utils/describe-error.js';
13
+ async function loadDriver(name, dialect, load) {
14
+ try {
15
+ return await load();
16
+ }
17
+ catch (err) {
18
+ throw new Error(`The '${dialect}' dialect needs the '${name}' package, which is not installed. ` +
19
+ `It is an optional peer dependency of @omnitron-dev/titan-database: install the driver ` +
20
+ `for the database this application uses. (${err.message})`, { cause: err });
21
+ }
22
+ }
23
+ function isPgPool(pool) {
24
+ const p = pool;
25
+ return (!!p && typeof p.totalCount === 'number' && typeof p.idleCount === 'number' && typeof p.waitingCount === 'number');
26
+ }
27
+ let pgModule = null;
28
+ function loadPg() {
29
+ pgModule ??= loadDriver('pg', 'postgres', () => import('pg'));
30
+ return pgModule;
31
+ }
32
+ let mysqlModule = null;
33
+ function loadMysql() {
34
+ mysqlModule ??= loadDriver('mysql2', 'mysql', () => import('mysql2'));
35
+ return mysqlModule;
36
+ }
37
+ let sqliteModule = null;
38
+ function loadSqlite() {
39
+ sqliteModule ??= loadDriver('better-sqlite3', 'sqlite', () => import('better-sqlite3'));
40
+ return sqliteModule;
41
+ }
42
+ let resilientClient = null;
43
+ export async function resilientPgClient() {
44
+ if (resilientClient)
45
+ return resilientClient;
46
+ const { Client: PgClient } = await loadPg();
47
+ resilientClient = class ResilientPgClient extends PgClient {
48
+ constructor(config) {
49
+ super(config);
50
+ this.on('error', () => { });
51
+ }
52
+ };
53
+ return resilientClient;
54
+ }
14
55
  import { sqliteDateSerializerPlugin } from './plugins/sqlite-date-serializer.plugin.js';
15
- import { createExecutor, isKyseraExecutor, getPlugins, getRawDb, } from '@kysera/executor';
16
- import { CircuitBreaker } from '@kysera/infra';
56
+ import { createExecutor, createExecutorSync, destroyExecutor, isKyseraExecutor, getPlugins, getRawDb, } from '@kysera/executor';
57
+ import { CircuitBreaker, withRetry, isTransientError } from '@kysera/infra';
58
+ import { createLiveConnectionRef } from './connection-ref.js';
17
59
  import { Injectable } from '@omnitron-dev/titan/decorators';
18
60
  import { Errors, TitanError, ErrorCode } from '@omnitron-dev/titan/errors';
19
- import { DATABASE_DEFAULT_CONNECTION, DEFAULT_POOL_CONFIG, DEFAULT_TIMEOUTS, } from './database.constants.js';
61
+ import { DATABASE_DEFAULT_CONNECTION, DEFAULT_POOL_CONFIG, } from './database.constants.js';
20
62
  const ERROR_MESSAGES = {
21
63
  CONNECTION_FAILED: (name, error) => `Failed to connect to database "${name}": ${error}`,
22
64
  INVALID_DIALECT: (dialect) => `Invalid database dialect: ${dialect}`,
@@ -33,49 +75,83 @@ const DATABASE_EVENTS = {
33
75
  ERROR: 'database.error',
34
76
  };
35
77
  import { EventEmitter } from '@omnitron-dev/eventemitter';
78
+ const PERMANENT_DRIVER_CODES = new Set([
79
+ '28P01',
80
+ '28000',
81
+ '3D000',
82
+ 'SQLITE_CANTOPEN',
83
+ 'ER_ACCESS_DENIED_ERROR',
84
+ 'ER_BAD_DB_ERROR',
85
+ 'ERR_INVALID_URL',
86
+ ]);
87
+ const PERMANENT_MESSAGES = [/Cannot open database because the directory does not exist/i];
88
+ const HIGH_LATENCY_MS = 500;
89
+ export const DEFAULT_SHUTDOWN_TIMEOUT_MS = 10_000;
90
+ const DISPOSE_WARN_MS = 30_000;
91
+ function healthCheckDeadlineMs(dialect) {
92
+ return dialect === 'sqlite' ? 10_000 : 5_000;
93
+ }
94
+ export function isPermanentConnectionError(error) {
95
+ if (error instanceof TitanError && error.code === ErrorCode.BAD_REQUEST)
96
+ return true;
97
+ const code = error?.code;
98
+ if (typeof code === 'string' && PERMANENT_DRIVER_CODES.has(code))
99
+ return true;
100
+ const message = error instanceof Error ? error.message : '';
101
+ return PERMANENT_MESSAGES.some((re) => re.test(message));
102
+ }
36
103
  let DatabaseManager = class DatabaseManager {
104
+ static { DatabaseManager_1 = this; }
105
+ static { this._bigintParserMode = null; }
106
+ get shutdownTimeoutMs() {
107
+ return this.options.shutdownTimeout ?? DEFAULT_SHUTDOWN_TIMEOUT_MS;
108
+ }
109
+ getRlsDefaults() {
110
+ return this.options.rls;
111
+ }
37
112
  constructor(options, logger) {
38
113
  this.connections = new Map();
39
114
  this.circuitBreakers = new Map();
40
115
  this.connectionSchemas = new Map();
41
116
  this.eventEmitter = new EventEmitter();
42
- this.shutdownInProgress = false;
43
117
  this.initialized = false;
118
+ this.initPromise = null;
44
119
  this.defaultRetryConfig = {
45
120
  maxRetries: 5,
46
121
  baseDelayMs: 1000,
47
122
  maxDelayMs: 16000,
48
123
  timeoutMs: 30000,
49
124
  };
50
- this.healthCheckIntervalMs = 30000;
51
125
  this.healthCheckFailures = new Map();
126
+ this.recovering = new Set();
52
127
  this.maxHealthCheckFailures = 3;
53
128
  this.options = options || {};
54
129
  this.logger = logger.child({ module: 'DatabaseManager' });
130
+ this.healthCheckIntervalMs = (this.options.kysera?.core?.healthCheck?.interval) ?? 30000;
55
131
  }
56
132
  async init() {
57
133
  if (this.initialized) {
58
134
  this.logger.debug('Database manager already initialized, skipping');
59
135
  return;
60
136
  }
137
+ if (this.initPromise) {
138
+ this.logger.debug('Database manager initialization already in progress, joining');
139
+ await this.initPromise;
140
+ return;
141
+ }
142
+ this.initPromise = this.doInit().finally(() => {
143
+ this.initPromise = null;
144
+ });
145
+ await this.initPromise;
146
+ }
147
+ async doInit() {
61
148
  this.logger.info('Initializing database manager');
62
149
  const connections = this.getConnectionConfigs();
63
150
  for (const [name, config] of Object.entries(connections)) {
64
151
  await this.createConnectionWithRetry(name, config);
65
152
  }
153
+ await this.applyGlobalPlugins();
66
154
  this.initialized = true;
67
- if (this.options.defaultSchema) {
68
- for (const [name, info] of this.connections) {
69
- if (info.config.dialect === 'postgres' && info.connected) {
70
- try {
71
- await this.setSchema(this.options.defaultSchema, name);
72
- }
73
- catch (error) {
74
- this.logger.warn({ connection: name, schema: this.options.defaultSchema, error }, 'Failed to set default schema');
75
- }
76
- }
77
- }
78
- }
79
155
  if (this.options.healthCheck !== false) {
80
156
  this.startProactiveHealthChecks();
81
157
  }
@@ -86,8 +162,8 @@ let DatabaseManager = class DatabaseManager {
86
162
  return;
87
163
  }
88
164
  this.logger.info({ intervalMs: this.healthCheckIntervalMs }, 'Starting proactive health checks');
89
- this.healthCheckTimer = setInterval(async () => {
90
- await this.runHealthChecks();
165
+ this.healthCheckTimer = setInterval(() => {
166
+ this.runHealthChecks().catch((err) => this.logger.error({ error: err }, 'Unhandled error in periodic health check'));
91
167
  }, this.healthCheckIntervalMs);
92
168
  if (this.healthCheckTimer.unref) {
93
169
  this.healthCheckTimer.unref();
@@ -101,28 +177,55 @@ let DatabaseManager = class DatabaseManager {
101
177
  }
102
178
  }
103
179
  async runHealthChecks() {
104
- const healthCheckPromises = [];
180
+ const promises = [];
105
181
  for (const [name, info] of this.connections) {
106
- if (!info.connected) {
107
- continue;
182
+ if (info.connected) {
183
+ promises.push(this.runSingleHealthCheck(name, info));
108
184
  }
109
- healthCheckPromises.push(this.runSingleHealthCheck(name, info));
185
+ else if (!info.connecting && !info.permanent && !this.recovering.has(name)) {
186
+ promises.push(this.recoverConnection(name));
187
+ }
188
+ }
189
+ if (promises.length > 0) {
190
+ await Promise.all(promises);
191
+ }
192
+ }
193
+ async recoverConnection(name) {
194
+ this.recovering.add(name);
195
+ try {
196
+ await this.reconnect(name);
197
+ this.healthCheckFailures.set(name, 0);
198
+ this.logger.info({ connection: name }, 'Connection recovered by the health check loop');
199
+ }
200
+ catch (error) {
201
+ this.logger.warn({ connection: name, error: describeError(error) }, 'Connection is still down; will try again on the next health check');
202
+ }
203
+ finally {
204
+ this.recovering.delete(name);
110
205
  }
111
- await Promise.all(healthCheckPromises);
112
206
  }
113
207
  async runSingleHealthCheck(name, info) {
114
208
  try {
115
- const startTime = Date.now();
116
- const isHealthy = await this.validateConnectionHealth(info.instance, info.config.dialect);
117
- const latency = Date.now() - startTime;
118
- if (isHealthy) {
209
+ const wallStart = Date.now();
210
+ const monoStart = process.hrtime.bigint();
211
+ const result = await this.validateConnectionHealth(info.instance, info.config.dialect);
212
+ const wallMs = Date.now() - wallStart;
213
+ const monoMs = Math.round(Number(process.hrtime.bigint() - monoStart) / 1e6);
214
+ if (result.healthy) {
119
215
  this.healthCheckFailures.set(name, 0);
120
- if (latency > 500) {
121
- this.logger.warn({ connection: name, latency }, 'Connection health check passed but with high latency');
216
+ const deadlineMs = healthCheckDeadlineMs(info.config.dialect);
217
+ if (monoMs > deadlineMs) {
218
+ this.logger.warn({ connection: name, monoMs, wallMs, deadlineMs, stalledAtLeastMs: monoMs - deadlineMs }, 'Health check returned later than its own abort deadline — the event loop was blocked, not the database');
219
+ }
220
+ else if (monoMs > HIGH_LATENCY_MS) {
221
+ this.logger.warn({ connection: name, monoMs, wallMs }, 'Connection health check passed but with high latency');
222
+ }
223
+ else if (wallMs > HIGH_LATENCY_MS) {
224
+ this.logger.debug({ connection: name, monoMs, wallMs }, 'Wall clock jumped during a health check — the machine likely slept');
122
225
  }
123
226
  }
124
227
  else {
125
- await this.handleHealthCheckFailure(name, info, new Error('Health check returned false'));
228
+ await this.handleHealthCheckFailure(name, info, result.error);
126
229
  }
127
230
  }
128
231
  catch (error) {
@@ -132,7 +235,7 @@ let DatabaseManager = class DatabaseManager {
132
235
  async handleHealthCheckFailure(name, info, error) {
133
236
  const failures = (this.healthCheckFailures.get(name) || 0) + 1;
134
237
  this.healthCheckFailures.set(name, failures);
135
- this.logger.warn({ connection: name, failures, maxFailures: this.maxHealthCheckFailures, error: error.message }, 'Connection health check failed');
238
+ this.logger.warn({ connection: name, failures, maxFailures: this.maxHealthCheckFailures, error: describeError(error) }, 'Connection health check failed');
136
239
  info.poolMetrics.errorCount++;
137
240
  info.poolMetrics.lastError = error;
138
241
  this.emitEvent({
@@ -164,12 +267,14 @@ let DatabaseManager = class DatabaseManager {
164
267
  }
165
268
  try {
166
269
  const startTime = Date.now();
167
- const isHealthy = await this.validateConnectionHealth(info.instance, info.config.dialect);
270
+ const result = await this.validateConnectionHealth(info.instance, info.config.dialect);
168
271
  const latency = Date.now() - startTime;
169
- return { healthy: isHealthy, latency };
272
+ return result.healthy
273
+ ? { healthy: true, latency }
274
+ : { healthy: false, latency, error: describeError(result.error) };
170
275
  }
171
276
  catch (error) {
172
- return { healthy: false, error: error.message };
277
+ return { healthy: false, error: describeError(error) };
173
278
  }
174
279
  }
175
280
  getHealthStatus() {
@@ -178,7 +283,7 @@ let DatabaseManager = class DatabaseManager {
178
283
  status.set(name, {
179
284
  connected: info.connected,
180
285
  consecutiveFailures: this.healthCheckFailures.get(name) || 0,
181
- lastError: info.lastError?.message,
286
+ lastError: info.lastError ? describeError(info.lastError) : undefined,
182
287
  poolMetrics: this.collectPoolMetrics(info),
183
288
  });
184
289
  }
@@ -208,33 +313,46 @@ let DatabaseManager = class DatabaseManager {
208
313
  }
209
314
  async createConnectionWithRetry(name, config) {
210
315
  const retryConfig = this.defaultRetryConfig;
211
- let lastError;
212
- for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
213
- try {
214
- this.logger.debug({ name, attempt, maxRetries: retryConfig.maxRetries }, 'Attempting database connection');
215
- const info = await this.createConnection(name, config);
216
- if (attempt > 0) {
217
- this.logger.info({ name, attempt, totalRetries: attempt }, 'Database connection established after retries');
218
- }
219
- return info;
220
- }
221
- catch (error) {
222
- lastError = error;
223
- const isLastAttempt = attempt === retryConfig.maxRetries;
224
- if (isLastAttempt) {
225
- this.logger.error({ name, attempt, error: lastError }, 'Database connection failed after all retries');
226
- break;
227
- }
228
- const delayMs = Math.min(retryConfig.baseDelayMs * Math.pow(2, attempt), retryConfig.maxDelayMs);
229
- this.logger.warn({ name, attempt, nextRetryIn: delayMs, error: lastError.message }, 'Database connection failed, retrying');
230
- await this.sleep(delayMs);
316
+ let attempts = 0;
317
+ try {
318
+ const info = await withRetry(() => {
319
+ attempts += 1;
320
+ this.logger.debug({ name, attempt: attempts - 1, maxRetries: retryConfig.maxRetries }, 'Attempting database connection');
321
+ return this.createConnection(name, config);
322
+ }, {
323
+ maxAttempts: retryConfig.maxRetries + 1,
324
+ delayMs: retryConfig.baseDelayMs,
325
+ maxDelayMs: retryConfig.maxDelayMs,
326
+ backoff: true,
327
+ jitterFactor: 0,
328
+ shouldRetry: (error) => {
329
+ if (error instanceof TitanError && error.details?.['permanent'] === true)
330
+ return false;
331
+ return (isTransientError(error) ||
332
+ (error instanceof TitanError && error.code === ErrorCode.SERVICE_UNAVAILABLE));
333
+ },
334
+ onRetry: (attempt, error) => {
335
+ this.logger.warn({
336
+ name,
337
+ attempt: attempt - 1,
338
+ error: error instanceof Error ? error.message : String(error),
339
+ }, 'Database connection failed, retrying');
340
+ },
341
+ });
342
+ if (attempts > 1) {
343
+ this.logger.info({ name, totalRetries: attempts - 1 }, 'Database connection established after retries');
231
344
  }
345
+ return info;
346
+ }
347
+ catch (error) {
348
+ const message = error instanceof Error ? error.message : String(error);
349
+ this.logger.error({ name, attempts, error }, 'Database connection failed after all retries');
350
+ throw new TitanError({
351
+ code: ErrorCode.SERVICE_UNAVAILABLE,
352
+ message: `Database connection ${name} failed after ${retryConfig.maxRetries} retries: ${message}`,
353
+ details: { connection: name, error: message, maxRetries: retryConfig.maxRetries },
354
+ });
232
355
  }
233
- throw new TitanError({
234
- code: ErrorCode.SERVICE_UNAVAILABLE,
235
- message: `Database connection ${name} failed after ${retryConfig.maxRetries} retries: ${lastError?.message}`,
236
- details: { connection: name, error: lastError?.message, maxRetries: retryConfig.maxRetries },
237
- });
238
356
  }
239
357
  async createConnection(name, config) {
240
358
  this.logger.debug({ name, dialect: config.dialect }, 'Creating database connection');
@@ -246,7 +364,6 @@ let DatabaseManager = class DatabaseManager {
246
364
  pool: undefined,
247
365
  connected: false,
248
366
  connecting: true,
249
- retryCount: 0,
250
367
  metrics: {
251
368
  queryCount: 0,
252
369
  errorCount: 0,
@@ -272,10 +389,16 @@ let DatabaseManager = class DatabaseManager {
272
389
  const { instance, pool } = await this.createKyselyInstance(config);
273
390
  info.instance = instance;
274
391
  info.pool = pool;
275
- await this.testConnection(instance, config.dialect);
276
- const isHealthy = await this.validateConnectionHealth(instance, config.dialect);
277
- if (!isHealthy) {
278
- throw Errors.unavailable('Database', 'Connection health check failed');
392
+ const health = await this.validateConnectionHealth(instance, config.dialect);
393
+ if (!health.healthy) {
394
+ throw new TitanError({
395
+ code: ErrorCode.SERVICE_UNAVAILABLE,
396
+ message: `Connection health check failed: ${describeError(health.error)}`,
397
+ details: {
398
+ permanent: isPermanentConnectionError(health.error),
399
+ driverCode: health.error.code,
400
+ },
401
+ });
279
402
  }
280
403
  info.connected = true;
281
404
  info.connecting = false;
@@ -303,7 +426,7 @@ let DatabaseManager = class DatabaseManager {
303
426
  info.connecting = false;
304
427
  info.lastError = error;
305
428
  this.connections.set(name, info);
306
- const errorMessage = ERROR_MESSAGES.CONNECTION_FAILED(name, error.message);
429
+ const errorMessage = ERROR_MESSAGES.CONNECTION_FAILED(name, describeError(error));
307
430
  this.logger.error({ name, error }, errorMessage);
308
431
  this.emitEvent({
309
432
  type: DATABASE_EVENTS.ERROR,
@@ -311,10 +434,13 @@ let DatabaseManager = class DatabaseManager {
311
434
  timestamp: new Date(),
312
435
  error: error,
313
436
  });
437
+ const permanent = (error instanceof TitanError && error.details?.['permanent'] === true) ||
438
+ isPermanentConnectionError(error);
439
+ info.permanent = permanent;
314
440
  throw new TitanError({
315
441
  code: ErrorCode.SERVICE_UNAVAILABLE,
316
442
  message: `Database connection ${name} is unavailable: ${errorMessage}`,
317
- details: { connection: name, error: errorMessage },
443
+ details: { connection: name, error: errorMessage, permanent },
318
444
  });
319
445
  }
320
446
  }
@@ -322,25 +448,64 @@ let DatabaseManager = class DatabaseManager {
322
448
  const connectionConfig = this.parseConnectionConfig(config);
323
449
  switch (config.dialect) {
324
450
  case 'postgres': {
451
+ const { Pool, types: pgTypes } = await loadPg();
452
+ const ResilientPgClient = await resilientPgClient();
325
453
  const pgConfig = { ...connectionConfig };
326
454
  if (pgConfig['ssl'] === false) {
327
455
  delete pgConfig['ssl'];
328
456
  }
457
+ const coerceBigint = config.coerceBigint !== false;
458
+ if (DatabaseManager_1._bigintParserMode === null) {
459
+ if (coerceBigint) {
460
+ pgTypes.setTypeParser(20, (val) => {
461
+ if (val === null)
462
+ return null;
463
+ const n = Number(val);
464
+ if (Number.isSafeInteger(n))
465
+ return n;
466
+ return BigInt(val);
467
+ });
468
+ }
469
+ DatabaseManager_1._bigintParserMode = coerceBigint;
470
+ }
471
+ else if (DatabaseManager_1._bigintParserMode !== coerceBigint) {
472
+ this.logger.warn({ connection: config.name, requested: coerceBigint, active: DatabaseManager_1._bigintParserMode }, 'pg BIGINT parser mode mismatch — pg.types.setTypeParser is process-global; first pool wins');
473
+ }
474
+ const pgPoolConfig = { ...DEFAULT_POOL_CONFIG, ...config.pool };
329
475
  const pool = new Pool({
330
476
  ...pgConfig,
331
- ...DEFAULT_POOL_CONFIG,
332
- ...config.pool,
477
+ ...pgPoolConfig,
478
+ connectionTimeoutMillis: pgPoolConfig.connectionTimeoutMillis ?? pgPoolConfig.acquireTimeoutMillis,
479
+ ...(this.options.defaultSchema
480
+ ? {
481
+ options: `-c search_path=${this.assertValidSchemaName(this.options.defaultSchema)},public`,
482
+ }
483
+ : {}),
484
+ Client: ResilientPgClient,
333
485
  });
486
+ if (this.options.defaultSchema) {
487
+ this.connectionSchemas.set(config.name || DATABASE_DEFAULT_CONNECTION, this.options.defaultSchema);
488
+ }
489
+ const connectionName = config.name || DATABASE_DEFAULT_CONNECTION;
334
490
  pool.on('error', (err, client) => {
335
- this.logger.error({ error: err, client: client ? 'present' : 'none' }, 'PostgreSQL pool error');
336
- const connInfo = this.connections.get(config.name || DATABASE_DEFAULT_CONNECTION);
491
+ const pgErr = err;
492
+ this.logger.error({
493
+ connection: connectionName,
494
+ code: pgErr.code,
495
+ severity: pgErr.severity,
496
+ routine: pgErr.routine,
497
+ clientPresent: !!client,
498
+ processID: client?.processID,
499
+ err,
500
+ }, 'PostgreSQL pool error');
501
+ const connInfo = this.connections.get(connectionName);
337
502
  if (connInfo) {
338
503
  connInfo.poolMetrics.errorCount++;
339
504
  connInfo.poolMetrics.lastError = err;
340
505
  }
341
506
  this.emitEvent({
342
507
  type: DATABASE_EVENTS.ERROR,
343
- connection: config.name || DATABASE_DEFAULT_CONNECTION,
508
+ connection: connectionName,
344
509
  timestamp: new Date(),
345
510
  error: err,
346
511
  });
@@ -360,7 +525,7 @@ let DatabaseManager = class DatabaseManager {
360
525
  connInfo.poolMetrics.acquireCount++;
361
526
  connInfo.poolMetrics.activeConnections++;
362
527
  connInfo.poolMetrics.idleConnections = Math.max(0, connInfo.poolMetrics.idleConnections - 1);
363
- connInfo.poolMetrics.lastAcquireAt = new Date();
528
+ connInfo.poolMetrics.lastAcquireAt = Date.now();
364
529
  }
365
530
  });
366
531
  pool.on('release', (_client) => {
@@ -390,20 +555,27 @@ let DatabaseManager = class DatabaseManager {
390
555
  return { instance, pool };
391
556
  }
392
557
  case 'mysql': {
558
+ const mysql = await loadMysql();
393
559
  const mysqlConfig = { ...connectionConfig };
394
560
  if (mysqlConfig['ssl'] === false) {
395
561
  delete mysqlConfig['ssl'];
396
562
  }
563
+ const connectionLimit = config.pool?.max || DEFAULT_POOL_CONFIG.max;
564
+ const userPool = (config.pool ?? {});
397
565
  const mysqlPoolConfig = {
398
- connectionLimit: config.pool?.max || DEFAULT_POOL_CONFIG.max,
566
+ connectionLimit,
399
567
  waitForConnections: true,
400
- queueLimit: 0,
568
+ queueLimit: userPool.queueLimit ?? connectionLimit * 10,
401
569
  connectTimeout: DEFAULT_POOL_CONFIG.acquireTimeoutMillis,
402
570
  };
403
571
  const pool = mysql.createPool({
404
572
  ...mysqlConfig,
405
573
  ...mysqlPoolConfig,
406
574
  });
575
+ pool.on('connection', (conn) => {
576
+ conn.on('error', () => {
577
+ });
578
+ });
407
579
  pool.on('error', (err) => {
408
580
  this.logger.error({ error: err }, 'MySQL pool error');
409
581
  const connInfo = this.connections.get(config.name || DATABASE_DEFAULT_CONNECTION);
@@ -433,7 +605,7 @@ let DatabaseManager = class DatabaseManager {
433
605
  connInfo.poolMetrics.acquireCount++;
434
606
  connInfo.poolMetrics.activeConnections++;
435
607
  connInfo.poolMetrics.idleConnections = Math.max(0, connInfo.poolMetrics.idleConnections - 1);
436
- connInfo.poolMetrics.lastAcquireAt = new Date();
608
+ connInfo.poolMetrics.lastAcquireAt = Date.now();
437
609
  }
438
610
  });
439
611
  pool.on('release', (_connection) => {
@@ -455,6 +627,7 @@ let DatabaseManager = class DatabaseManager {
455
627
  return { instance, pool };
456
628
  }
457
629
  case 'sqlite': {
630
+ const { default: BetterSqlite3 } = await loadSqlite();
458
631
  const database = new BetterSqlite3(connectionConfig.database || ':memory:', {
459
632
  verbose: config.debug ? (msg) => this.logger.debug({ msg }, 'SQLite verbose') : undefined,
460
633
  timeout: 5000,
@@ -516,42 +689,41 @@ let DatabaseManager = class DatabaseManager {
516
689
  `Common mistake: Using {...context.connection} instead of {connection: context.connection} ` +
517
690
  `in TitanDatabaseModule.forRoot()`);
518
691
  }
519
- return {
692
+ const base = {
520
693
  database: connConfig.database || (config.dialect === 'sqlite' ? ':memory:' : 'postgres'),
521
694
  host: connConfig.host,
522
695
  port: connConfig.port,
523
696
  user: connConfig.user,
524
697
  password: connConfig.password,
525
698
  ssl: connConfig.ssl,
526
- searchPath: connConfig.searchPath,
527
- charset: connConfig.charset,
528
- timezone: connConfig.timezone,
529
699
  };
530
- }
531
- async testConnection(db, dialect) {
532
- const timeout = this.options.queryTimeout || DEFAULT_TIMEOUTS.query;
533
- const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(Errors.timeout('database connection test', timeout)), timeout));
534
- const testQuery = sql `SELECT 1`.execute(db);
535
- await Promise.race([testQuery, timeoutPromise]);
700
+ if (config.dialect === 'mysql') {
701
+ return { ...base, charset: connConfig.charset, timezone: connConfig.timezone };
702
+ }
703
+ if (config.dialect === 'postgres') {
704
+ return { ...base, searchPath: connConfig.searchPath };
705
+ }
706
+ return base;
536
707
  }
537
708
  async validateConnectionHealth(db, dialect) {
709
+ const timeout = healthCheckDeadlineMs(dialect);
710
+ const ac = new AbortController();
711
+ const timer = setTimeout(() => ac.abort(), timeout);
538
712
  try {
539
- const timeout = dialect === 'sqlite' ? 10000 : 5000;
540
- const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Health check timeout')), timeout));
541
- const healthCheckPromise = (async () => {
542
- const testQuery = dialect === 'sqlite' ? sql `SELECT 1 AS health_check`.execute(db) : sql `SELECT 1 AS health_check`.execute(db);
543
- await testQuery;
544
- return true;
545
- })();
546
- return await Promise.race([healthCheckPromise, timeoutPromise]);
713
+ await Promise.race([
714
+ sql `SELECT 1 AS health_check`.execute(db),
715
+ new Promise((_, reject) => {
716
+ ac.signal.addEventListener('abort', () => reject(new Error(`Health check timed out after ${timeout}ms`)));
717
+ }),
718
+ ]);
719
+ return { healthy: true };
547
720
  }
548
721
  catch (error) {
549
- this.logger.error({ error }, 'Connection health check failed');
550
- return false;
722
+ return { healthy: false, error: error instanceof Error ? error : new Error(String(error)) };
723
+ }
724
+ finally {
725
+ clearTimeout(timer);
551
726
  }
552
- }
553
- sleep(ms) {
554
- return new Promise((resolve) => setTimeout(resolve, ms));
555
727
  }
556
728
  async getConnection(name = DATABASE_DEFAULT_CONNECTION) {
557
729
  const info = this.connections.get(name);
@@ -559,12 +731,16 @@ let DatabaseManager = class DatabaseManager {
559
731
  throw Errors.notFound('Database connection', name);
560
732
  }
561
733
  if (!info.connected && !info.connecting) {
562
- await this.reconnect(name);
734
+ await this.reconnect(name).catch(() => undefined);
563
735
  }
564
- if (!info.connected) {
565
- throw Errors.unavailable(name, info.lastError?.message || 'Unknown error');
736
+ const live = this.connections.get(name) ?? info;
737
+ if (!live.connected) {
738
+ throw this.unavailable(name);
566
739
  }
567
- return info.instance;
740
+ if (live.executor) {
741
+ return live.executor;
742
+ }
743
+ return live.instance;
568
744
  }
569
745
  async getExecutor(name = DATABASE_DEFAULT_CONNECTION, plugins) {
570
746
  const info = this.connections.get(name);
@@ -572,19 +748,58 @@ let DatabaseManager = class DatabaseManager {
572
748
  throw Errors.notFound('Database connection', name);
573
749
  }
574
750
  if (!info.connected && !info.connecting) {
575
- await this.reconnect(name);
751
+ await this.reconnect(name).catch(() => undefined);
576
752
  }
577
- if (!info.connected) {
578
- throw Errors.unavailable(name, info.lastError?.message || 'Unknown error');
753
+ const live = this.connections.get(name) ?? info;
754
+ if (!live.connected) {
755
+ throw this.unavailable(name);
579
756
  }
580
757
  if (plugins && plugins.length > 0) {
581
758
  this.logger.debug({ connection: name, plugins: plugins.map((p) => p.name) }, 'Creating executor with custom plugins');
582
- return createExecutor(info.instance, plugins);
759
+ return createExecutor(live.instance, plugins);
760
+ }
761
+ if (live.executor) {
762
+ return live.executor;
763
+ }
764
+ return createExecutor(live.instance, []);
765
+ }
766
+ getConnectionRef(name = DATABASE_DEFAULT_CONNECTION) {
767
+ return createLiveConnectionRef(() => this.requireLiveConnection(name));
768
+ }
769
+ getExecutorRef(name = DATABASE_DEFAULT_CONNECTION, plugins = []) {
770
+ let builtFrom;
771
+ let executor;
772
+ return createLiveConnectionRef(() => {
773
+ const instance = this.requireLiveInstance(name);
774
+ if (builtFrom !== instance || !executor) {
775
+ executor = createExecutorSync(instance, plugins);
776
+ builtFrom = instance;
777
+ }
778
+ return executor;
779
+ });
780
+ }
781
+ unavailable(name) {
782
+ return Errors.unavailable(name);
783
+ }
784
+ requireLiveConnection(name) {
785
+ const info = this.connections.get(name);
786
+ if (!info) {
787
+ throw Errors.notFound('Database connection', name);
788
+ }
789
+ if (!info.connected) {
790
+ throw this.unavailable(name);
791
+ }
792
+ return (info.executor ?? info.instance);
793
+ }
794
+ requireLiveInstance(name) {
795
+ const info = this.connections.get(name);
796
+ if (!info) {
797
+ throw Errors.notFound('Database connection', name);
583
798
  }
584
- if (info.executor) {
585
- return info.executor;
799
+ if (!info.connected) {
800
+ throw this.unavailable(name);
586
801
  }
587
- return createExecutor(info.instance, []);
802
+ return info.instance;
588
803
  }
589
804
  async setConnectionPlugins(name = DATABASE_DEFAULT_CONNECTION, plugins) {
590
805
  const info = this.connections.get(name);
@@ -592,7 +807,7 @@ let DatabaseManager = class DatabaseManager {
592
807
  throw Errors.notFound('Database connection', name);
593
808
  }
594
809
  if (!info.connected) {
595
- throw Errors.unavailable(name, 'Connection not established');
810
+ throw this.unavailable(name);
596
811
  }
597
812
  this.logger.info({ connection: name, plugins: plugins.map((p) => p.name) }, 'Setting connection plugins');
598
813
  const executor = await createExecutor(info.instance, plugins);
@@ -620,8 +835,38 @@ let DatabaseManager = class DatabaseManager {
620
835
  throw Errors.notFound('Database connection', name);
621
836
  }
622
837
  this.logger.info({ name }, 'Attempting to reconnect to database');
623
- info.retryCount++;
624
- await this.createConnectionWithRetry(name, info.config);
838
+ const config = info.config;
839
+ info.connected = false;
840
+ info.connecting = true;
841
+ this.healthCheckFailures.delete(name);
842
+ this.circuitBreakers.delete(name);
843
+ this.emitEvent({
844
+ type: DATABASE_EVENTS.DISCONNECTED,
845
+ connection: name,
846
+ timestamp: new Date(),
847
+ });
848
+ void this.disposeReleased(name, info);
849
+ await this.createConnectionWithRetry(name, config);
850
+ }
851
+ async disposeReleased(name, info) {
852
+ const started = Date.now();
853
+ const timer = setTimeout(() => {
854
+ this.logger.warn({ name, afterMs: DISPOSE_WARN_MS }, 'The replaced database connection has still not finished closing; its driver is not settling. The new connection is already serving.');
855
+ }, DISPOSE_WARN_MS);
856
+ timer.unref?.();
857
+ try {
858
+ if (info.executor && isKyseraExecutor(info.executor)) {
859
+ await destroyExecutor(info.executor);
860
+ }
861
+ await info.instance.destroy();
862
+ this.logger.info({ name, ms: Date.now() - started }, 'Replaced database connection closed');
863
+ }
864
+ catch (error) {
865
+ this.logger.warn({ name, error: describeError(error), ms: Date.now() - started }, 'Error closing the replaced database connection');
866
+ }
867
+ finally {
868
+ clearTimeout(timer);
869
+ }
625
870
  }
626
871
  async close(name = DATABASE_DEFAULT_CONNECTION) {
627
872
  const info = this.connections.get(name);
@@ -629,53 +874,63 @@ let DatabaseManager = class DatabaseManager {
629
874
  return;
630
875
  }
631
876
  this.connections.delete(name);
877
+ this.healthCheckFailures.delete(name);
878
+ this.circuitBreakers.delete(name);
632
879
  this.logger.info({ name }, 'Closing database connection');
633
880
  try {
634
881
  if (info.executor && isKyseraExecutor(info.executor)) {
635
- try {
636
- const { destroyExecutor } = await import('@kysera/executor');
637
- await destroyExecutor(info.executor);
638
- }
639
- catch {
640
- this.logger.debug({ name }, 'Skipping executor cleanup - dynamic import unavailable');
641
- }
882
+ await destroyExecutor(info.executor);
642
883
  }
643
884
  await info.instance.destroy();
644
- if (info.pool) {
645
- if (info.config.dialect === 'postgres' && info.pool instanceof Pool) {
646
- await info.pool.end();
647
- }
648
- else if (info.config.dialect === 'mysql' && 'end' in info.pool) {
649
- await new Promise((resolve, reject) => {
650
- info.pool.end((err) => (err ? reject(err) : resolve()));
651
- });
652
- }
653
- else if (info.config.dialect === 'sqlite' && 'close' in info.pool) {
654
- info.pool.close();
655
- }
656
- }
885
+ this.logger.info({ name }, 'Database connection closed');
886
+ }
887
+ catch (error) {
888
+ this.logger.error({ name, error }, 'Error closing database connection');
889
+ throw error;
890
+ }
891
+ finally {
657
892
  info.connected = false;
658
893
  this.emitEvent({
659
894
  type: DATABASE_EVENTS.DISCONNECTED,
660
895
  connection: name,
661
896
  timestamp: new Date(),
662
897
  });
663
- this.logger.info({ name }, 'Database connection closed');
664
- }
665
- catch (error) {
666
- this.logger.error({ name, error }, 'Error closing database connection');
667
- throw error;
668
898
  }
669
899
  }
670
900
  async closeAll() {
671
901
  this.stopProactiveHealthChecks();
902
+ this.initialized = false;
672
903
  if (this.connections.size === 0) {
673
904
  return;
674
905
  }
675
906
  this.logger.info('Closing all database connections');
676
907
  const connectionNames = Array.from(this.connections.keys());
677
- const closePromises = connectionNames.map((name) => this.close(name).catch((error) => this.logger.error({ name, error }, 'Error closing connection')));
678
- await Promise.all(closePromises);
908
+ const pending = new Set(connectionNames);
909
+ const closePromises = connectionNames.map((name) => this.close(name)
910
+ .catch((error) => this.logger.error({ name, error }, 'Error closing connection'))
911
+ .finally(() => pending.delete(name)));
912
+ const shutdownTimeout = this.shutdownTimeoutMs;
913
+ if (shutdownTimeout > 0) {
914
+ let timer;
915
+ const timedOut = await Promise.race([
916
+ Promise.all(closePromises).then(() => false),
917
+ new Promise((resolve) => {
918
+ timer = setTimeout(() => resolve(true), shutdownTimeout);
919
+ timer.unref?.();
920
+ }),
921
+ ]);
922
+ if (timer)
923
+ clearTimeout(timer);
924
+ if (timedOut) {
925
+ this.logger.error({ pending: Array.from(pending), shutdownTimeout }, 'Database connections did not close within shutdownTimeout; abandoning the wait');
926
+ return;
927
+ }
928
+ }
929
+ else {
930
+ void Promise.all(closePromises);
931
+ this.logger.info({ pending: Array.from(pending) }, 'Not waiting for database connections to finish closing (shutdownTimeout is 0)');
932
+ return;
933
+ }
679
934
  this.logger.info('All database connections closed');
680
935
  }
681
936
  isConnected(name = DATABASE_DEFAULT_CONNECTION) {
@@ -711,22 +966,35 @@ let DatabaseManager = class DatabaseManager {
711
966
  return metrics;
712
967
  }
713
968
  collectPoolMetrics(info) {
714
- const baseMetrics = { ...info.poolMetrics };
715
- if (info.pool) {
716
- if (info.config.dialect === 'postgres' && info.pool instanceof Pool) {
717
- const pgPool = info.pool;
718
- baseMetrics.totalConnections = pgPool.totalCount;
719
- baseMetrics.idleConnections = pgPool.idleCount;
720
- baseMetrics.waitingClients = pgPool.waitingCount;
721
- baseMetrics.activeConnections = pgPool.totalCount - pgPool.idleCount;
722
- }
723
- else if (info.config.dialect === 'mysql' && 'pool' in info.pool) {
724
- }
725
- }
726
- if (baseMetrics.acquireCount > 0 && baseMetrics.totalAcquireTimeMs > 0) {
727
- baseMetrics.averageAcquireTimeMs = baseMetrics.totalAcquireTimeMs / baseMetrics.acquireCount;
969
+ const m = info.poolMetrics;
970
+ let totalConnections = m.totalConnections;
971
+ let idleConnections = m.idleConnections;
972
+ let activeConnections = m.activeConnections;
973
+ let waitingClients = m.waitingClients;
974
+ if (info.pool && info.config.dialect === 'postgres' && isPgPool(info.pool)) {
975
+ const pgPool = info.pool;
976
+ totalConnections = pgPool.totalCount;
977
+ idleConnections = pgPool.idleCount;
978
+ waitingClients = pgPool.waitingCount;
979
+ activeConnections = pgPool.totalCount - pgPool.idleCount;
728
980
  }
729
- return baseMetrics;
981
+ const averageAcquireTimeMs = m.acquireCount > 0 && m.totalAcquireTimeMs > 0
982
+ ? m.totalAcquireTimeMs / m.acquireCount
983
+ : m.averageAcquireTimeMs;
984
+ return {
985
+ totalConnections,
986
+ idleConnections,
987
+ activeConnections,
988
+ waitingClients,
989
+ acquireCount: m.acquireCount,
990
+ releaseCount: m.releaseCount,
991
+ errorCount: m.errorCount,
992
+ lastError: m.lastError,
993
+ lastAcquireAt: m.lastAcquireAt,
994
+ averageAcquireTimeMs,
995
+ totalAcquireTimeMs: m.totalAcquireTimeMs,
996
+ poolSize: m.poolSize,
997
+ };
730
998
  }
731
999
  resetPoolMetrics(name) {
732
1000
  const resetMetrics = (info) => {
@@ -750,19 +1018,6 @@ let DatabaseManager = class DatabaseManager {
750
1018
  }
751
1019
  }
752
1020
  }
753
- registerShutdownHandlers() {
754
- const shutdownHandler = async () => {
755
- if (this.shutdownInProgress)
756
- return;
757
- this.shutdownInProgress = true;
758
- this.logger.info('Database manager shutdown initiated');
759
- const timeout = this.options.shutdownTimeout || DEFAULT_TIMEOUTS.shutdown;
760
- const timeoutPromise = new Promise((resolve) => setTimeout(resolve, timeout));
761
- await Promise.race([this.closeAll(), timeoutPromise]);
762
- };
763
- process.on('SIGINT', shutdownHandler);
764
- process.on('SIGTERM', shutdownHandler);
765
- }
766
1021
  emitEvent(event) {
767
1022
  this.eventEmitter.emit(event.type, event);
768
1023
  }
@@ -785,47 +1040,129 @@ let DatabaseManager = class DatabaseManager {
785
1040
  return this.circuitBreakers.get(name);
786
1041
  }
787
1042
  async withCircuitBreaker(name = DATABASE_DEFAULT_CONNECTION, fn) {
788
- const breaker = this.circuitBreakers.get(name);
789
1043
  const db = await this.getConnection(name);
1044
+ const breaker = this.circuitBreakers.get(name);
790
1045
  if (breaker) {
791
1046
  return breaker.execute(() => fn(db));
792
1047
  }
793
1048
  return fn(db);
794
1049
  }
795
1050
  async setSchema(schema, name = DATABASE_DEFAULT_CONNECTION) {
796
- const info = this.connections.get(name);
797
- if (!info) {
798
- throw Errors.notFound('Database connection', name);
799
- }
800
- if (info.config.dialect !== 'postgres') {
801
- throw Errors.badRequest(`setSchema is only supported for PostgreSQL connections, got: ${info.config.dialect}`);
802
- }
1051
+ throw Errors.badRequest(`setSchema('${schema}', '${name}') is disabled: a session-scoped SET search_path over a ` +
1052
+ `connection pool lands on ONE arbitrary pooled client and breaks schema isolation ` +
1053
+ `nondeterministically. Use the defaultSchema module option (applied as a per-connection ` +
1054
+ `startup parameter), Kysely's .withSchema() for per-query scoping, or a dedicated ` +
1055
+ `connection per tenant.`);
1056
+ }
1057
+ assertValidSchemaName(schema) {
803
1058
  if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(schema)) {
804
1059
  throw Errors.badRequest(`Invalid schema name: ${schema}`);
805
1060
  }
806
- await sql `SET search_path TO ${sql.ref(schema)}, public`.execute(info.instance);
807
- this.connectionSchemas.set(name, schema);
808
- this.logger.debug({ connection: name, schema }, 'Schema set for connection');
1061
+ return schema;
809
1062
  }
810
1063
  getSchema(name = DATABASE_DEFAULT_CONNECTION) {
811
1064
  return this.connectionSchemas.get(name);
812
1065
  }
813
- async withSchema(schema, fn, name = DATABASE_DEFAULT_CONNECTION) {
814
- const previousSchema = this.connectionSchemas.get(name);
815
- await this.setSchema(schema, name);
816
- try {
817
- const db = await this.getConnection(name);
818
- return await fn(db);
1066
+ async withSchema(schema, _fn, name = DATABASE_DEFAULT_CONNECTION) {
1067
+ throw Errors.badRequest(`withSchema('${schema}', '${name}') is disabled: switching search_path over a shared ` +
1068
+ `connection pool races between concurrent requests. Use Kysely's .withSchema() for ` +
1069
+ `per-query scoping or a dedicated connection per tenant.`);
1070
+ }
1071
+ async resolvePlugins(pluginSpecs) {
1072
+ const resolved = [];
1073
+ for (const spec of pluginSpecs) {
1074
+ if (typeof spec === 'string') {
1075
+ const plugin = await this.resolveBuiltInPlugin(spec);
1076
+ if (plugin)
1077
+ resolved.push(plugin);
1078
+ }
1079
+ else if ('interceptQuery' in spec || 'name' in spec) {
1080
+ resolved.push(spec);
1081
+ }
1082
+ else if ('plugin' in spec) {
1083
+ if (typeof spec.plugin === 'string') {
1084
+ const plugin = await this.resolveBuiltInPlugin(spec.plugin, spec.options);
1085
+ if (plugin)
1086
+ resolved.push(plugin);
1087
+ }
1088
+ else {
1089
+ resolved.push(spec.plugin);
1090
+ }
1091
+ }
819
1092
  }
820
- finally {
821
- await this.setSchema(previousSchema ?? 'public', name);
1093
+ return resolved;
1094
+ }
1095
+ async resolveBuiltInPlugin(name, options) {
1096
+ switch (name) {
1097
+ case 'soft-delete':
1098
+ case 'softDelete': {
1099
+ const { softDeletePlugin } = await import('@kysera/soft-delete');
1100
+ return softDeletePlugin(options);
1101
+ }
1102
+ case 'timestamps': {
1103
+ const { timestampsPlugin } = await import('@kysera/timestamps');
1104
+ return timestampsPlugin(options);
1105
+ }
1106
+ case 'audit': {
1107
+ const { auditPlugin } = await import('@kysera/audit');
1108
+ return auditPlugin(options);
1109
+ }
1110
+ case 'rls': {
1111
+ if (!options || typeof options !== 'object' || !('schema' in options)) {
1112
+ this.logger.warn({ plugin: name }, "RLS plugin requires options.schema (use { plugin: 'rls', options: { schema } }); skipping");
1113
+ return null;
1114
+ }
1115
+ const { rlsPlugin } = await import('@kysera/rls');
1116
+ return rlsPlugin(options);
1117
+ }
1118
+ default:
1119
+ this.logger.warn({ plugin: name }, 'Unknown built-in plugin name, skipping');
1120
+ return null;
1121
+ }
1122
+ }
1123
+ async applyGlobalPlugins() {
1124
+ const plugins = [];
1125
+ const builtIn = this.options.plugins?.builtIn;
1126
+ if (builtIn) {
1127
+ const legacyPlugins = [];
1128
+ if (builtIn.softDelete) {
1129
+ const opts = typeof builtIn.softDelete === 'object' ? builtIn.softDelete : undefined;
1130
+ const plugin = await this.resolveBuiltInPlugin('soft-delete', opts);
1131
+ if (plugin)
1132
+ legacyPlugins.push(plugin);
1133
+ }
1134
+ if (builtIn.timestamps) {
1135
+ const opts = typeof builtIn.timestamps === 'object' ? builtIn.timestamps : undefined;
1136
+ const plugin = await this.resolveBuiltInPlugin('timestamps', opts);
1137
+ if (plugin)
1138
+ legacyPlugins.push(plugin);
1139
+ }
1140
+ if (builtIn.audit) {
1141
+ const opts = typeof builtIn.audit === 'object' ? builtIn.audit : undefined;
1142
+ const plugin = await this.resolveBuiltInPlugin('audit', opts);
1143
+ if (plugin)
1144
+ legacyPlugins.push(plugin);
1145
+ }
1146
+ plugins.push(...legacyPlugins);
1147
+ }
1148
+ const pluginSpecs = this.options.kysera?.plugins;
1149
+ if (pluginSpecs && pluginSpecs.length > 0) {
1150
+ plugins.push(...(await this.resolvePlugins(pluginSpecs)));
1151
+ }
1152
+ if (plugins.length === 0)
1153
+ return;
1154
+ this.logger.info({ plugins: plugins.map((p) => p.name) }, 'Applying global Kysera plugins to all connections');
1155
+ for (const [name, info] of this.connections) {
1156
+ if (info.connected) {
1157
+ await this.setConnectionPlugins(name, plugins);
1158
+ }
822
1159
  }
823
1160
  }
824
1161
  async onModuleDestroy() {
825
1162
  await this.closeAll();
826
1163
  }
827
1164
  };
828
- DatabaseManager = __decorate([
1165
+ DatabaseManager = DatabaseManager_1 = __decorate([
829
1166
  Injectable(),
830
1167
  __metadata("design:paramtypes", [Object, Object])
831
1168
  ], DatabaseManager);