@gravito/ripple 3.1.0 → 4.0.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 (190) hide show
  1. package/README.md +260 -19
  2. package/dist/atlas/src/DB.d.ts +348 -0
  3. package/dist/atlas/src/OrbitAtlas.d.ts +9 -0
  4. package/dist/atlas/src/config/defineConfig.d.ts +14 -0
  5. package/dist/atlas/src/config/index.d.ts +7 -0
  6. package/dist/atlas/src/config/loadConfig.d.ts +41 -0
  7. package/dist/atlas/src/connection/Connection.d.ts +112 -0
  8. package/dist/atlas/src/connection/ConnectionManager.d.ts +180 -0
  9. package/dist/atlas/src/connection/ReplicaConnectionPool.d.ts +54 -0
  10. package/dist/atlas/src/drivers/BunSQLDriver.d.ts +32 -0
  11. package/dist/atlas/src/drivers/BunSQLPreparedStatement.d.ts +118 -0
  12. package/dist/atlas/src/drivers/MongoDBDriver.d.ts +36 -0
  13. package/dist/atlas/src/drivers/MySQLDriver.d.ts +79 -0
  14. package/dist/atlas/src/drivers/PostgresDriver.d.ts +96 -0
  15. package/dist/atlas/src/drivers/RedisDriver.d.ts +43 -0
  16. package/dist/atlas/src/drivers/SQLiteDriver.d.ts +45 -0
  17. package/dist/atlas/src/drivers/types.d.ts +260 -0
  18. package/dist/atlas/src/errors/index.d.ts +45 -0
  19. package/dist/atlas/src/grammar/Grammar.d.ts +342 -0
  20. package/dist/atlas/src/grammar/MongoGrammar.d.ts +47 -0
  21. package/dist/atlas/src/grammar/MySQLGrammar.d.ts +54 -0
  22. package/dist/atlas/src/grammar/NullGrammar.d.ts +35 -0
  23. package/dist/atlas/src/grammar/PostgresGrammar.d.ts +62 -0
  24. package/dist/atlas/src/grammar/SQLiteGrammar.d.ts +32 -0
  25. package/dist/atlas/src/index.d.ts +79 -0
  26. package/dist/atlas/src/migration/Migration.d.ts +64 -0
  27. package/dist/atlas/src/migration/MigrationRepository.d.ts +65 -0
  28. package/dist/atlas/src/migration/Migrator.d.ts +110 -0
  29. package/dist/atlas/src/migration/index.d.ts +6 -0
  30. package/dist/atlas/src/observability/AtlasMetrics.d.ts +33 -0
  31. package/dist/atlas/src/observability/AtlasObservability.d.ts +15 -0
  32. package/dist/atlas/src/observability/AtlasTracer.d.ts +12 -0
  33. package/dist/atlas/src/observability/index.d.ts +9 -0
  34. package/dist/atlas/src/orm/Repository.d.ts +247 -0
  35. package/dist/atlas/src/orm/index.d.ts +6 -0
  36. package/dist/atlas/src/orm/model/DirtyTracker.d.ts +121 -0
  37. package/dist/atlas/src/orm/model/Model.d.ts +458 -0
  38. package/dist/atlas/src/orm/model/ModelRegistry.d.ts +20 -0
  39. package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +150 -0
  40. package/dist/atlas/src/orm/model/concerns/HasEvents.d.ts +36 -0
  41. package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +92 -0
  42. package/dist/atlas/src/orm/model/concerns/HasRelationships.d.ts +117 -0
  43. package/dist/atlas/src/orm/model/concerns/HasSerialization.d.ts +64 -0
  44. package/dist/atlas/src/orm/model/concerns/applyMixins.d.ts +15 -0
  45. package/dist/atlas/src/orm/model/concerns/index.d.ts +12 -0
  46. package/dist/atlas/src/orm/model/decorators.d.ts +138 -0
  47. package/dist/atlas/src/orm/model/errors.d.ts +52 -0
  48. package/dist/atlas/src/orm/model/index.d.ts +10 -0
  49. package/dist/atlas/src/orm/model/relationships.d.ts +207 -0
  50. package/dist/atlas/src/orm/model/types.d.ts +12 -0
  51. package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +124 -0
  52. package/dist/atlas/src/orm/schema/SchemaSniffer.d.ts +54 -0
  53. package/dist/atlas/src/orm/schema/index.d.ts +6 -0
  54. package/dist/atlas/src/orm/schema/types.d.ts +85 -0
  55. package/dist/atlas/src/pool/AdaptivePoolManager.d.ts +98 -0
  56. package/dist/atlas/src/pool/PoolHealthChecker.d.ts +91 -0
  57. package/dist/atlas/src/pool/PoolStrategy.d.ts +129 -0
  58. package/dist/atlas/src/pool/PoolWarmer.d.ts +92 -0
  59. package/dist/atlas/src/query/Expression.d.ts +60 -0
  60. package/dist/atlas/src/query/NPlusOneDetector.d.ts +10 -0
  61. package/dist/atlas/src/query/QueryBuilder.d.ts +643 -0
  62. package/dist/atlas/src/query/RelationshipResolver.d.ts +23 -0
  63. package/dist/atlas/src/query/clauses/GroupByClause.d.ts +51 -0
  64. package/dist/atlas/src/query/clauses/HavingClause.d.ts +70 -0
  65. package/dist/atlas/src/query/clauses/JoinClause.d.ts +87 -0
  66. package/dist/atlas/src/query/clauses/LimitClause.d.ts +82 -0
  67. package/dist/atlas/src/query/clauses/OrderByClause.d.ts +69 -0
  68. package/dist/atlas/src/query/clauses/SelectClause.d.ts +71 -0
  69. package/dist/atlas/src/query/clauses/WhereClause.d.ts +167 -0
  70. package/dist/atlas/src/query/clauses/index.d.ts +11 -0
  71. package/dist/atlas/src/schema/Blueprint.d.ts +276 -0
  72. package/dist/atlas/src/schema/ColumnDefinition.d.ts +154 -0
  73. package/dist/atlas/src/schema/ForeignKeyDefinition.d.ts +37 -0
  74. package/dist/atlas/src/schema/MigrationGenerator.d.ts +45 -0
  75. package/dist/atlas/src/schema/Schema.d.ts +131 -0
  76. package/dist/atlas/src/schema/SchemaDiff.d.ts +73 -0
  77. package/dist/atlas/src/schema/TypeGenerator.d.ts +57 -0
  78. package/dist/atlas/src/schema/TypeWriter.d.ts +42 -0
  79. package/dist/atlas/src/schema/grammars/MySQLSchemaGrammar.d.ts +23 -0
  80. package/dist/atlas/src/schema/grammars/PostgresSchemaGrammar.d.ts +26 -0
  81. package/dist/atlas/src/schema/grammars/SQLiteSchemaGrammar.d.ts +28 -0
  82. package/dist/atlas/src/schema/grammars/SchemaGrammar.d.ts +97 -0
  83. package/dist/atlas/src/schema/grammars/index.d.ts +7 -0
  84. package/dist/atlas/src/schema/index.d.ts +8 -0
  85. package/dist/atlas/src/seed/Factory.d.ts +90 -0
  86. package/dist/atlas/src/seed/Seeder.d.ts +28 -0
  87. package/dist/atlas/src/seed/SeederRunner.d.ts +74 -0
  88. package/dist/atlas/src/seed/index.d.ts +6 -0
  89. package/dist/atlas/src/sharding/ShardingManager.d.ts +59 -0
  90. package/dist/atlas/src/types/index.d.ts +1182 -0
  91. package/dist/atlas/src/utils/CursorEncoding.d.ts +63 -0
  92. package/dist/atlas/src/utils/levenshtein.d.ts +9 -0
  93. package/dist/core/src/CommandKernel.d.ts +33 -0
  94. package/dist/core/src/ConfigManager.d.ts +39 -0
  95. package/dist/core/src/Container/RequestScopeManager.d.ts +62 -0
  96. package/dist/core/src/Container/RequestScopeMetrics.d.ts +144 -0
  97. package/dist/core/src/Container.d.ts +86 -11
  98. package/dist/core/src/ErrorHandler.d.ts +3 -0
  99. package/dist/core/src/HookManager.d.ts +511 -4
  100. package/dist/core/src/PlanetCore.d.ts +89 -0
  101. package/dist/core/src/RequestContext.d.ts +97 -0
  102. package/dist/core/src/Router.d.ts +1 -5
  103. package/dist/core/src/ServiceProvider.d.ts +22 -0
  104. package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +1 -0
  105. package/dist/core/src/adapters/PhotonAdapter.d.ts +5 -0
  106. package/dist/core/src/adapters/bun/BunContext.d.ts +4 -0
  107. package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +1 -0
  108. package/dist/core/src/adapters/types.d.ts +27 -0
  109. package/dist/core/src/cli/queue-commands.d.ts +6 -0
  110. package/dist/core/src/engine/AOTRouter.d.ts +7 -12
  111. package/dist/core/src/engine/FastContext.d.ts +27 -2
  112. package/dist/core/src/engine/Gravito.d.ts +0 -1
  113. package/dist/core/src/engine/MinimalContext.d.ts +25 -2
  114. package/dist/core/src/engine/types.d.ts +9 -1
  115. package/dist/core/src/error-handling/RequestScopeErrorContext.d.ts +126 -0
  116. package/dist/core/src/events/BackpressureManager.d.ts +215 -0
  117. package/dist/core/src/events/CircuitBreaker.d.ts +229 -0
  118. package/dist/core/src/events/DeadLetterQueue.d.ts +219 -0
  119. package/dist/core/src/events/EventBackend.d.ts +12 -0
  120. package/dist/core/src/events/EventOptions.d.ts +204 -0
  121. package/dist/core/src/events/EventPriorityQueue.d.ts +301 -0
  122. package/dist/core/src/events/FlowControlStrategy.d.ts +109 -0
  123. package/dist/core/src/events/IdempotencyCache.d.ts +60 -0
  124. package/dist/core/src/events/MessageQueueBridge.d.ts +184 -0
  125. package/dist/core/src/events/PriorityEscalationManager.d.ts +82 -0
  126. package/dist/core/src/events/RetryScheduler.d.ts +104 -0
  127. package/dist/core/src/events/WorkerPool.d.ts +98 -0
  128. package/dist/core/src/events/WorkerPoolConfig.d.ts +153 -0
  129. package/dist/core/src/events/WorkerPoolMetrics.d.ts +65 -0
  130. package/dist/core/src/events/aggregation/AggregationWindow.d.ts +77 -0
  131. package/dist/core/src/events/aggregation/DeduplicationManager.d.ts +135 -0
  132. package/dist/core/src/events/aggregation/EventAggregationManager.d.ts +108 -0
  133. package/dist/core/src/events/aggregation/EventBatcher.d.ts +99 -0
  134. package/dist/core/src/events/aggregation/types.d.ts +117 -0
  135. package/dist/core/src/events/index.d.ts +25 -0
  136. package/dist/core/src/events/observability/EventMetrics.d.ts +132 -0
  137. package/dist/core/src/events/observability/EventTracer.d.ts +68 -0
  138. package/dist/core/src/events/observability/EventTracing.d.ts +161 -0
  139. package/dist/core/src/events/observability/OTelEventMetrics.d.ts +332 -0
  140. package/dist/core/src/events/observability/ObservableHookManager.d.ts +108 -0
  141. package/dist/core/src/events/observability/StreamWorkerMetrics.d.ts +76 -0
  142. package/dist/core/src/events/observability/index.d.ts +24 -0
  143. package/dist/core/src/events/observability/metrics-types.d.ts +16 -0
  144. package/dist/core/src/events/types.d.ts +134 -0
  145. package/dist/core/src/exceptions/CircularDependencyException.d.ts +9 -0
  146. package/dist/core/src/exceptions/index.d.ts +1 -0
  147. package/dist/core/src/health/HealthProvider.d.ts +67 -0
  148. package/dist/core/src/http/types.d.ts +40 -0
  149. package/dist/core/src/index.d.ts +25 -4
  150. package/dist/core/src/instrumentation/index.d.ts +35 -0
  151. package/dist/core/src/instrumentation/opentelemetry.d.ts +178 -0
  152. package/dist/core/src/instrumentation/types.d.ts +182 -0
  153. package/dist/core/src/observability/Metrics.d.ts +244 -0
  154. package/dist/core/src/observability/QueueDashboard.d.ts +136 -0
  155. package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +350 -0
  156. package/dist/core/src/reliability/RetryPolicy.d.ts +217 -0
  157. package/dist/core/src/reliability/index.d.ts +6 -0
  158. package/dist/core/src/router/ControllerDispatcher.d.ts +12 -0
  159. package/dist/core/src/router/RequestValidator.d.ts +20 -0
  160. package/dist/index.js +6709 -9888
  161. package/dist/index.js.map +64 -62
  162. package/dist/photon/src/index.d.ts +19 -0
  163. package/dist/photon/src/middleware/ratelimit-redis.d.ts +50 -0
  164. package/dist/photon/src/middleware/ratelimit.d.ts +161 -0
  165. package/dist/photon/src/openapi.d.ts +19 -0
  166. package/dist/proto/ripple.proto +120 -0
  167. package/dist/ripple/src/RippleServer.d.ts +64 -445
  168. package/dist/ripple/src/channels/ChannelManager.d.ts +25 -10
  169. package/dist/ripple/src/drivers/NATSDriver.d.ts +87 -0
  170. package/dist/ripple/src/drivers/RedisDriver.d.ts +30 -1
  171. package/dist/ripple/src/drivers/index.d.ts +1 -0
  172. package/dist/ripple/src/engines/BunEngine.d.ts +98 -0
  173. package/dist/ripple/src/engines/IRippleEngine.d.ts +205 -0
  174. package/dist/ripple/src/engines/UWebSocketsEngine.d.ts +97 -0
  175. package/dist/ripple/src/engines/WsEngine.d.ts +69 -0
  176. package/dist/ripple/src/engines/index.d.ts +15 -0
  177. package/dist/ripple/src/index.d.ts +2 -0
  178. package/dist/ripple/src/middleware/InterceptorManager.d.ts +21 -0
  179. package/dist/ripple/src/observability/RippleMetrics.d.ts +24 -0
  180. package/dist/ripple/src/reliability/AckManager.d.ts +48 -0
  181. package/dist/ripple/src/serializers/ISerializer.d.ts +39 -0
  182. package/dist/ripple/src/serializers/JsonSerializer.d.ts +19 -0
  183. package/dist/ripple/src/serializers/ProtobufSerializer.d.ts +41 -0
  184. package/dist/ripple/src/serializers/index.d.ts +3 -0
  185. package/dist/ripple/src/tracking/SessionManager.d.ts +104 -0
  186. package/dist/ripple/src/tracking/index.d.ts +1 -0
  187. package/dist/ripple/src/types.d.ts +188 -12
  188. package/dist/ripple/src/utils/MessageSerializer.d.ts +33 -23
  189. package/dist/ripple/src/utils/TokenBucket.d.ts +25 -0
  190. package/package.json +25 -8
@@ -0,0 +1,9 @@
1
+ import type { GravitoOrbit, PlanetCore } from '@gravito/core';
2
+ /**
3
+ * Atlas Orbit - Database & ORM Integration
4
+ * Integrates the Atlas ORM engine into the Gravito Core ecosystem.
5
+ * @public
6
+ */
7
+ export declare class OrbitAtlas implements GravitoOrbit {
8
+ install(core: PlanetCore): Promise<void>;
9
+ }
@@ -0,0 +1,14 @@
1
+ import type { AtlasConfig, AtlasObservabilityConfig, ConnectionConfig, PostgresConfig } from '../types';
2
+ export type { AtlasConfig, AtlasObservabilityConfig, ConnectionConfig, PostgresConfig };
3
+ /**
4
+ * Define configuration with type checking
5
+ */
6
+ export declare function defineConfig(config: AtlasConfig): AtlasConfig;
7
+ /**
8
+ * Load configuration from environment variables with optional prefix
9
+ * Supports both DATABASE_URL and individual DB_* variables
10
+ *
11
+ * @param connectionName - Name for the connection
12
+ * @param prefix - Optional prefix for environment variables (e.g., 'READ' for DB_READ_*)
13
+ */
14
+ export declare function fromEnv(connectionName?: string, prefix?: string): AtlasConfig;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Configuration Module
3
+ * @description Database configuration utilities
4
+ */
5
+ export type { AtlasConfig, ConnectionConfig } from '../types';
6
+ export { defineConfig, fromEnv } from './defineConfig';
7
+ export { loadConfig, loadConfigFile } from './loadConfig';
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Configuration Loader
3
+ * @description Load database configuration from various sources
4
+ */
5
+ import type { AtlasConfig } from '../types';
6
+ /**
7
+ * Load configuration from a TypeScript/JavaScript config file
8
+ *
9
+ * @param configPath - Path to the config file (default: 'config/database.ts' or 'config/database.js')
10
+ * @returns Atlas configuration
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // config/database.ts
15
+ * import { defineConfig } from '@gravito/atlas'
16
+ *
17
+ * export default defineConfig({
18
+ * default: 'default',
19
+ * connections: {
20
+ * default: {
21
+ * driver: 'postgres',
22
+ * host: 'localhost',
23
+ * database: 'myapp'
24
+ * }
25
+ * }
26
+ * })
27
+ * ```
28
+ */
29
+ export declare function loadConfigFile(configPath?: string): Promise<AtlasConfig>;
30
+ /**
31
+ * Load configuration from multiple sources with priority
32
+ * Priority: 1. Config file, 2. Environment variables, 3. Default
33
+ *
34
+ * @param options - Loading options
35
+ * @returns Atlas configuration
36
+ */
37
+ export declare function loadConfig(options?: {
38
+ configPath?: string;
39
+ useEnv?: boolean;
40
+ envPrefix?: string;
41
+ }): Promise<AtlasConfig>;
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Connection
3
+ * @description Represents a database connection
4
+ */
5
+ import type { AtlasMetrics, AtlasTracer } from '../observability';
6
+ import type { ConnectionConfig, ConnectionContract, DriverContract, ExecuteResult, GrammarContract, PoolStats, QueryBuilderContract, QueryResult } from '../types';
7
+ /**
8
+ * Database Connection
9
+ * Wraps a driver and grammar for query building and execution
10
+ */
11
+ export declare class Connection implements ConnectionContract {
12
+ protected readonly name: string;
13
+ protected readonly config: ConnectionConfig;
14
+ protected driver: DriverContract;
15
+ protected grammar: GrammarContract;
16
+ protected connected: boolean;
17
+ protected proxy?: ConnectionContract;
18
+ protected tracer: AtlasTracer | undefined;
19
+ protected metrics: AtlasMetrics | undefined;
20
+ /**
21
+ * Static query listeners for global observation (e.g. debugging)
22
+ */
23
+ static queryListeners: Array<(query: {
24
+ connection: string;
25
+ sql: string;
26
+ bindings: unknown[];
27
+ duration: number;
28
+ timestamp: number;
29
+ }) => void>;
30
+ constructor(name: string, config: ConnectionConfig);
31
+ /**
32
+ * Set the proxy instance for this connection
33
+ */
34
+ setProxy(proxy: ConnectionContract): void;
35
+ /**
36
+ * Get connection name
37
+ */
38
+ getName(): string;
39
+ /**
40
+ * Get the underlying driver
41
+ */
42
+ getDriver(): DriverContract;
43
+ /**
44
+ * Get connection configuration
45
+ */
46
+ getConfig(): ConnectionConfig;
47
+ /**
48
+ * Get the grammar
49
+ */
50
+ getGrammar(): GrammarContract;
51
+ /**
52
+ * Get the tracer
53
+ */
54
+ getTracer(): AtlasTracer | undefined;
55
+ /**
56
+ * Get the metrics
57
+ */
58
+ getMetrics(): AtlasMetrics | undefined;
59
+ /**
60
+ * Create a new query builder for a table
61
+ */
62
+ table<T = Record<string, unknown>>(tableName: string): QueryBuilderContract<T>;
63
+ /**
64
+ * Alias for table() for NoSQL connections
65
+ */
66
+ collection<T = Record<string, unknown>>(name: string): QueryBuilderContract<T>;
67
+ /**
68
+ * Execute raw SQL
69
+ */
70
+ raw<T = Record<string, unknown>>(sql: string, bindings?: unknown[]): Promise<QueryResult<T>>;
71
+ /**
72
+ * Execute raw SQL statement (INSERT/UPDATE/DELETE)
73
+ */
74
+ execute(sql: string, bindings?: unknown[]): Promise<ExecuteResult>;
75
+ /**
76
+ * Run a callback within a transaction
77
+ */
78
+ transaction<T>(callback: (connection: ConnectionContract) => Promise<T>): Promise<T>;
79
+ /**
80
+ * Stream query results for processing large datasets
81
+ * @param sql - SQL query
82
+ * @param bindings - Query parameters
83
+ * @returns Async iterable of result rows
84
+ */
85
+ stream<T = Record<string, unknown>>(sql: string, bindings?: unknown[]): AsyncIterable<T>;
86
+ /**
87
+ * Get connection pool statistics (if supported by driver)
88
+ * @returns Pool statistics or null if not supported
89
+ */
90
+ getPoolStats(): PoolStats | null;
91
+ /**
92
+ * Disconnect from the database
93
+ */
94
+ disconnect(): Promise<void>;
95
+ /**
96
+ * Connect to the database
97
+ */
98
+ connect(): Promise<void>;
99
+ /**
100
+ * Ensure connection is established
101
+ */
102
+ protected ensureConnected(): Promise<void>;
103
+ /**
104
+ * Create the driver instance based on config
105
+ * Automatically prefers Bun.sql native driver when available (unless explicitly disabled)
106
+ */
107
+ protected createDriver(): DriverContract;
108
+ /**
109
+ * Create the grammar instance based on config
110
+ */
111
+ protected createGrammar(): GrammarContract;
112
+ }
@@ -0,0 +1,180 @@
1
+ /**
2
+ * Connection Manager
3
+ * @description Manages multiple database connections
4
+ */
5
+ import { type AdaptivePoolConfig, AdaptivePoolManager } from '../pool/AdaptivePoolManager';
6
+ import { type PoolHealthCheckConfig } from '../pool/PoolHealthChecker';
7
+ import { type PoolWarmerConfig } from '../pool/PoolWarmer';
8
+ import type { AtlasConnectionEntry, ConnectionContract, PoolHealth } from '../types';
9
+ /**
10
+ * Connection Manager
11
+ *
12
+ * Responsible for managing the lifecycle of database connections.
13
+ * It handles connection pooling (via drivers), lazy initialization,
14
+ * and automatic cleanup of idle connections.
15
+ *
16
+ * Connections are stored in a Map and retrieved by name. If a connection
17
+ * hasn't been initialized, the manager creates it using the provided configuration.
18
+ */
19
+ export declare class ConnectionManager {
20
+ private readonly configs;
21
+ private connections;
22
+ private defaultConnectionName;
23
+ private lastUsed;
24
+ private cleanupInterval?;
25
+ private healthChecker?;
26
+ private warmer?;
27
+ private adaptiveManager?;
28
+ /** Replica pools keyed by connection name */
29
+ private replicaPools;
30
+ private readonly MAX_IDLE_TIME;
31
+ private readonly CLEANUP_INTERVAL;
32
+ constructor(configs?: Record<string, AtlasConnectionEntry>);
33
+ /**
34
+ * Get a connection instance by name.
35
+ * If the connection is not already initialized, it will be created.
36
+ *
37
+ * @param name - Optional connection name (defaults to 'default').
38
+ * @returns The connection instance.
39
+ * @throws Error if the connection is not configured.
40
+ */
41
+ connection(name?: string): ConnectionContract;
42
+ /**
43
+ * Get the read replica connection for a named connection.
44
+ * Returns the round-robin selected read replica, or the write connection
45
+ * if no replicas are configured.
46
+ *
47
+ * @param name - Connection name (defaults to default)
48
+ */
49
+ readConnection(name?: string): ConnectionContract;
50
+ /**
51
+ * Get the write (primary) connection for a named connection.
52
+ *
53
+ * @param name - Connection name (defaults to default)
54
+ */
55
+ writeConnection(name?: string): ConnectionContract;
56
+ /**
57
+ * Check whether a given connection has read replicas.
58
+ */
59
+ hasReplicas(name?: string): boolean;
60
+ /**
61
+ * Build and register a Connection instance, wrapping it in a Proxy.
62
+ * @internal
63
+ */
64
+ private _buildAndRegisterConnection;
65
+ /**
66
+ * Build a proxy-wrapped Connection from config without registering.
67
+ * @internal
68
+ */
69
+ private _buildConnection;
70
+ /**
71
+ * Add a connection configuration.
72
+ *
73
+ * @param name - Unique name for the connection.
74
+ * @param config - Connection configuration settings (standard or read/write replica).
75
+ */
76
+ addConnection(name: string, config: AtlasConnectionEntry): void;
77
+ /**
78
+ * Set the default connection name.
79
+ *
80
+ * @param name - The name of the default connection.
81
+ */
82
+ setDefaultConnection(name: string): void;
83
+ /**
84
+ * Get the default connection name.
85
+ *
86
+ * @returns The default connection name.
87
+ */
88
+ getDefaultConnection(): string;
89
+ /**
90
+ * Check if a connection configuration exists.
91
+ *
92
+ * @param name - The connection name to check.
93
+ * @returns True if the connection is configured.
94
+ */
95
+ hasConnection(name: string): boolean;
96
+ /**
97
+ * Get all configured connection names.
98
+ *
99
+ * @returns Array of connection names.
100
+ */
101
+ getConnectionNames(): string[];
102
+ /**
103
+ * Get the configuration for a specific connection.
104
+ *
105
+ * @param name - The connection name.
106
+ * @returns The connection configuration or undefined if not found.
107
+ */
108
+ getConfig(name: string): AtlasConnectionEntry | undefined;
109
+ /**
110
+ * Disconnect all active connections.
111
+ *
112
+ * @returns A promise that resolves when all connections are closed.
113
+ */
114
+ disconnectAll(): Promise<void>;
115
+ /**
116
+ * Disconnect a specific connection by name.
117
+ *
118
+ * @param name - Optional connection name (defaults to default).
119
+ * @returns A promise that resolves when the connection is closed.
120
+ */
121
+ disconnect(name?: string): Promise<void>;
122
+ /**
123
+ * Purge a connection from the manager's cache.
124
+ * This will NOT disconnect the connection if it's already active.
125
+ *
126
+ * @param name - Optional connection name.
127
+ */
128
+ purge(name?: string): void;
129
+ /**
130
+ * Reconnect to a connection.
131
+ * This will disconnect the existing connection and create a new one.
132
+ *
133
+ * @param name - Optional connection name.
134
+ * @returns The new connection instance.
135
+ */
136
+ reconnect(name?: string): Promise<ConnectionContract>;
137
+ private cleanupIdleConnections;
138
+ private startCleanup;
139
+ stopCleanup(): void;
140
+ /**
141
+ * Enable connection pool health checking
142
+ */
143
+ enableHealthCheck(config?: Partial<PoolHealthCheckConfig>): void;
144
+ /**
145
+ * Disable connection pool health checking
146
+ */
147
+ disableHealthCheck(): void;
148
+ /**
149
+ * Get connection pool health status
150
+ */
151
+ getHealthStatus(connectionName?: string): PoolHealth | Map<string, PoolHealth>;
152
+ /**
153
+ * Enable connection pool warming
154
+ */
155
+ enableWarmup(config?: Partial<PoolWarmerConfig>): void;
156
+ /**
157
+ * Warm up all connection pools
158
+ */
159
+ warmup(): Promise<any>;
160
+ /**
161
+ * Enable adaptive connection pool management
162
+ * Automatically adjusts pool sizes based on load patterns
163
+ */
164
+ enableAdaptive(config?: Partial<AdaptivePoolConfig>): void;
165
+ /**
166
+ * Disable adaptive connection pool management
167
+ */
168
+ disableAdaptive(): void;
169
+ /**
170
+ * Get adaptive pool manager instance (if enabled)
171
+ */
172
+ getAdaptiveManager(): AdaptivePoolManager | undefined;
173
+ /**
174
+ * Shutdown the connection manager.
175
+ * Stops the idle cleanup interval, health checking, adaptive management, and disconnects all connections.
176
+ *
177
+ * @returns A promise that resolves when shutdown is complete.
178
+ */
179
+ shutdown(): Promise<void>;
180
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @gravito/atlas - Replica Connection Pool
3
+ * @description Manages read/write replica connections with Round-robin load balancing.
4
+ *
5
+ * Wraps a write (primary) connection and a pool of read (replica) connections.
6
+ * Transparently routes SELECT queries to read replicas and mutations to the primary.
7
+ */
8
+ import type { ConnectionContract } from '../types';
9
+ /**
10
+ * ReplicaConnectionPool
11
+ *
12
+ * Holds one authoritative write connection and N read replica connections.
13
+ * Applies Round-robin selection across replicas to balance read load.
14
+ *
15
+ * Usage:
16
+ * ```typescript
17
+ * const pool = new ReplicaConnectionPool(writeConn, [replica1, replica2])
18
+ * const readConn = pool.getReadConnection() // Round-robin
19
+ * const writeConn = pool.getWriteConnection() // Always primary
20
+ * ```
21
+ */
22
+ export declare class ReplicaConnectionPool {
23
+ private readonly writeConnection;
24
+ private readonly readConnections;
25
+ private roundRobinIndex;
26
+ constructor(writeConnection: ConnectionContract, readConnections?: ConnectionContract[]);
27
+ /**
28
+ * Get the primary write connection (always the same node).
29
+ */
30
+ getWriteConnection(): ConnectionContract;
31
+ /**
32
+ * Get a read replica connection using Round-robin selection.
33
+ *
34
+ * Falls back to the write connection if no replicas are configured.
35
+ */
36
+ getReadConnection(): ConnectionContract;
37
+ /**
38
+ * Whether this pool has any read replicas configured.
39
+ */
40
+ hasReplicas(): boolean;
41
+ /**
42
+ * Total number of replica connections in the pool.
43
+ */
44
+ replicaCount(): number;
45
+ /**
46
+ * Disconnect all connections (write + all replicas).
47
+ */
48
+ disconnectAll(): Promise<void>;
49
+ /**
50
+ * Reset round-robin index (useful for testing).
51
+ * @internal
52
+ */
53
+ _resetIndex(): void;
54
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Bun Native SQL Driver
3
+ */
4
+ import type { ConnectionConfig, DriverContract, DriverType, ExecuteResult, PoolStats, QueryResult } from '../types';
5
+ export declare class BunSQLDriver implements DriverContract {
6
+ private readonly config;
7
+ private client;
8
+ private sqliteClient;
9
+ private connected;
10
+ private transactionActive;
11
+ private preparedManager?;
12
+ constructor(config: ConnectionConfig);
13
+ getDriverName(): DriverType;
14
+ connect(): Promise<void>;
15
+ disconnect(): Promise<void>;
16
+ isConnected(): boolean;
17
+ query<T = Record<string, unknown>>(sql: string, bindings?: unknown[]): Promise<QueryResult<T>>;
18
+ execute(sql: string, bindings?: unknown[]): Promise<ExecuteResult>;
19
+ private normalizeBindings;
20
+ beginTransaction(): Promise<void>;
21
+ commit(): Promise<void>;
22
+ rollback(): Promise<void>;
23
+ inTransaction(): boolean;
24
+ private ensureConnection;
25
+ private getConnectionUrl;
26
+ prepare(sql: string): Promise<string>;
27
+ executePrepared<T = Record<string, unknown>>(name: string, bindings?: unknown[]): Promise<QueryResult<T>>;
28
+ clearPreparedStatements(): Promise<void>;
29
+ stream<T = Record<string, unknown>>(sql: string, bindings?: unknown[]): AsyncIterable<T>;
30
+ getPoolStats(): PoolStats | null;
31
+ private normalizeError;
32
+ }
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Bun SQL Prepared Statement Manager
3
+ * @description Manages prepared statement caching, lifecycle, and optimization
4
+ */
5
+ import type { BunSQLClient } from './types';
6
+ /**
7
+ * Configuration options for prepared statement manager
8
+ */
9
+ export interface PreparedStatementManagerConfig {
10
+ /**
11
+ * Maximum number of prepared statements to cache
12
+ * @default 100
13
+ */
14
+ maxStatements?: number;
15
+ /**
16
+ * Idle timeout in milliseconds before a statement is removed
17
+ * @default 60000 (1 minute)
18
+ */
19
+ idleTimeout?: number;
20
+ }
21
+ /**
22
+ * Prepared Statement Manager for BunSQL
23
+ *
24
+ * Manages statement caching and lifecycle to optimize query performance.
25
+ * Features:
26
+ * - LRU (Least Recently Used) cache eviction
27
+ * - Idle timeout cleanup
28
+ * - Usage statistics tracking
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const manager = new BunSQLPreparedStatementManager(client)
33
+ *
34
+ * // Prepare a statement
35
+ * const stmtId = await manager.prepare('SELECT * FROM users WHERE id = ?')
36
+ *
37
+ * // Execute multiple times
38
+ * const users1 = await manager.execute(stmtId, [1])
39
+ * const users2 = await manager.execute(stmtId, [2])
40
+ *
41
+ * // Clean up
42
+ * await manager.clear()
43
+ * ```
44
+ */
45
+ export declare class BunSQLPreparedStatementManager {
46
+ private readonly client;
47
+ private statements;
48
+ private sqlToName;
49
+ private readonly config;
50
+ private cleanupTimer?;
51
+ constructor(client: BunSQLClient, config?: PreparedStatementManagerConfig);
52
+ /**
53
+ * Prepare a SQL statement for repeated execution
54
+ *
55
+ * @param sql - SQL query to prepare
56
+ * @returns Prepared statement identifier
57
+ */
58
+ prepare(sql: string): Promise<string>;
59
+ /**
60
+ * Execute a prepared statement
61
+ *
62
+ * @param name - Prepared statement identifier
63
+ * @param bindings - Query parameters
64
+ * @returns Query result rows
65
+ */
66
+ execute<T = Record<string, unknown>>(name: string, bindings?: unknown[]): Promise<T[]>;
67
+ /**
68
+ * Clear all prepared statements
69
+ */
70
+ clear(): Promise<void>;
71
+ /**
72
+ * Clean up idle statements
73
+ */
74
+ cleanup(): Promise<void>;
75
+ /**
76
+ * Get statement usage statistics
77
+ *
78
+ * @param name - Prepared statement identifier
79
+ * @returns Usage statistics or null if not found
80
+ */
81
+ getStats(name: string): {
82
+ useCount: number;
83
+ lastUsed: number;
84
+ } | null;
85
+ /**
86
+ * Get the number of cached statements
87
+ */
88
+ getSize(): number;
89
+ /**
90
+ * Destroy the manager and clean up resources
91
+ */
92
+ destroy(): Promise<void>;
93
+ /**
94
+ * Evict the least recently used statement
95
+ * @private
96
+ */
97
+ private evictLeastRecentlyUsed;
98
+ /**
99
+ * Generate a unique statement name
100
+ * @private
101
+ */
102
+ private generateStatementName;
103
+ /**
104
+ * Simple hash function for SQL strings
105
+ * @private
106
+ */
107
+ private simpleHash;
108
+ /**
109
+ * Start periodic cleanup timer
110
+ * @private
111
+ */
112
+ private startCleanupTimer;
113
+ /**
114
+ * Stop cleanup timer
115
+ * @private
116
+ */
117
+ private stopCleanupTimer;
118
+ }
@@ -0,0 +1,36 @@
1
+ import type { ConnectionConfig, DriverContract, DriverType, ExecuteResult, QueryResult } from '../types';
2
+ /**
3
+ * MongoDB Driver
4
+ * Provides a document interface via DB.connection('mongodb')
5
+ */
6
+ import type { MongoClient } from './types';
7
+ export declare class MongoDBDriver implements DriverContract {
8
+ private config;
9
+ private client;
10
+ private db;
11
+ private MongoClientCtor?;
12
+ constructor(config: ConnectionConfig, deps?: {
13
+ MongoClient?: new (url: string, options?: Record<string, unknown>) => MongoClient;
14
+ });
15
+ getDriverName(): DriverType;
16
+ connect(): Promise<void>;
17
+ /**
18
+ * Dynamically load mongodb module
19
+ */
20
+ private loadMongoModule;
21
+ disconnect(): Promise<void>;
22
+ isConnected(): boolean;
23
+ /**
24
+ * Execute a query (Protocol: JSON String)
25
+ */
26
+ query<T = Record<string, unknown>>(protocolJson: string, _bindings?: unknown[]): Promise<QueryResult<T>>;
27
+ /**
28
+ * Execute a write operation (Protocol: JSON String)
29
+ */
30
+ execute(protocolJson: string, _bindings?: unknown[]): Promise<ExecuteResult>;
31
+ beginTransaction(): Promise<void>;
32
+ commit(): Promise<void>;
33
+ rollback(): Promise<void>;
34
+ inTransaction(): boolean;
35
+ private mapDocument;
36
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * MySQL/MariaDB Driver
3
+ * @description Database driver for MySQL and MariaDB using mysql2 package
4
+ */
5
+ import type { ConnectionConfig, DriverContract, DriverType, ExecuteResult, PoolHealth, PoolStats, QueryResult } from '../types';
6
+ export declare class MySQLDriver implements DriverContract {
7
+ private pool;
8
+ private transactionConnection;
9
+ private connected;
10
+ private mysql;
11
+ private poolConfig;
12
+ private readonly driverType;
13
+ constructor(config: ConnectionConfig, driverType?: 'mysql' | 'mariadb');
14
+ /**
15
+ * Get the driver name
16
+ */
17
+ getDriverName(): DriverType;
18
+ /**
19
+ * Connect to MySQL/MariaDB
20
+ */
21
+ connect(): Promise<void>;
22
+ /**
23
+ * Dynamically load mysql2 module
24
+ */
25
+ private loadMySQLModule;
26
+ /**
27
+ * Disconnect from MySQL
28
+ */
29
+ disconnect(): Promise<void>;
30
+ /**
31
+ * Check if connected
32
+ */
33
+ isConnected(): boolean;
34
+ /**
35
+ * Execute a query
36
+ */
37
+ query<T = Record<string, unknown>>(sql: string, bindings?: unknown[]): Promise<QueryResult<T>>;
38
+ /**
39
+ * Execute a statement (INSERT/UPDATE/DELETE)
40
+ */
41
+ execute(sql: string, bindings?: unknown[]): Promise<ExecuteResult>;
42
+ /**
43
+ * Begin a transaction
44
+ */
45
+ beginTransaction(): Promise<void>;
46
+ /**
47
+ * Commit the current transaction
48
+ */
49
+ commit(): Promise<void>;
50
+ /**
51
+ * Rollback the current transaction
52
+ */
53
+ rollback(): Promise<void>;
54
+ /**
55
+ * Check if in transaction
56
+ */
57
+ inTransaction(): boolean;
58
+ /**
59
+ * Get connection pool statistics
60
+ */
61
+ getPoolStats(): PoolStats | null;
62
+ /**
63
+ * Get connection pool health status
64
+ */
65
+ getPoolHealth(): PoolHealth;
66
+ /**
67
+ * Adjust the connection pool size
68
+ * Note: mysql2 Pool doesn't support dynamic resizing, so we reconnect with new size
69
+ */
70
+ adjustPoolSize(targetSize: number): Promise<void>;
71
+ /**
72
+ * Get a connection from the pool
73
+ */
74
+ private getConnection;
75
+ /**
76
+ * Normalize MySQL/MariaDB errors
77
+ */
78
+ private normalizeError;
79
+ }