@pipeline-builder/pipeline-data 3.1.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.
@@ -0,0 +1,3 @@
1
+ export * from './drizzle-schema';
2
+ export * from './postgres-connection';
3
+ export * from './retry-strategy';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ // Copyright 2026 Pipeline Builder Contributors
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ __exportStar(require("./drizzle-schema"), exports);
20
+ __exportStar(require("./postgres-connection"), exports);
21
+ __exportStar(require("./retry-strategy"), exports);
22
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGF0YWJhc2UvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLCtDQUErQztBQUMvQyxzQ0FBc0M7Ozs7Ozs7Ozs7Ozs7Ozs7QUFFdEMsbURBQWlDO0FBQ2pDLHdEQUFzQztBQUN0QyxtREFBaUMiLCJzb3VyY2VzQ29udGVudCI6WyIvLyBDb3B5cmlnaHQgMjAyNiBQaXBlbGluZSBCdWlsZGVyIENvbnRyaWJ1dG9yc1xuLy8gU1BEWC1MaWNlbnNlLUlkZW50aWZpZXI6IEFwYWNoZS0yLjBcblxuZXhwb3J0ICogZnJvbSAnLi9kcml6emxlLXNjaGVtYSc7XG5leHBvcnQgKiBmcm9tICcuL3Bvc3RncmVzLWNvbm5lY3Rpb24nO1xuZXhwb3J0ICogZnJvbSAnLi9yZXRyeS1zdHJhdGVneSc7XG4iXX0=
@@ -0,0 +1,232 @@
1
+ import { drizzle } from 'drizzle-orm/node-postgres';
2
+ import { Pool, PoolClient } from 'pg';
3
+ /**
4
+ * Database connection statistics for monitoring
5
+ */
6
+ export interface ConnectionStats {
7
+ totalCount: number;
8
+ idleCount: number;
9
+ waitingCount: number;
10
+ }
11
+ /**
12
+ * Options for configuring the database connection
13
+ */
14
+ export interface ConnectionOptions {
15
+ /** Whether to enable connection logging */
16
+ enableLogging?: boolean;
17
+ /** Whether to automatically retry failed connections */
18
+ enableAutoRetry?: boolean;
19
+ /** Maximum number of connection retry attempts */
20
+ maxRetries?: number;
21
+ /** Delay between retry attempts in milliseconds */
22
+ retryDelay?: number;
23
+ /** SSL configuration */
24
+ ssl?: boolean | {
25
+ rejectUnauthorized: boolean;
26
+ };
27
+ }
28
+ /**
29
+ * Singleton database connection class.
30
+ * Manages PostgreSQL connection pooling and Drizzle ORM instance.
31
+ *
32
+ * Features:
33
+ * - Singleton pattern for single connection pool
34
+ * - Automatic connection retry with backoff
35
+ * - Connection health monitoring
36
+ * - Graceful shutdown handling
37
+ * - Comprehensive error handling
38
+ * - Connection statistics tracking
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * import { Connection } from './connection';
43
+ *
44
+ * const connection = Connection.getInstance();
45
+ * const plugins = await connection.db.select().from(schema.plugin);
46
+ *
47
+ * // During shutdown
48
+ * await connection.close();
49
+ * ```
50
+ */
51
+ export declare class Connection {
52
+ private static instance;
53
+ /**
54
+ * Drizzle ORM database instance with schema
55
+ */
56
+ readonly db: ReturnType<typeof drizzle>;
57
+ private readonly pool;
58
+ private readonly options;
59
+ private readonly retryStrategy;
60
+ private isShuttingDown;
61
+ /**
62
+ * Private constructor to enforce singleton pattern.
63
+ * Initializes PostgreSQL connection pool and Drizzle ORM instance.
64
+ *
65
+ * @param options - Optional configuration for the connection
66
+ * @throws {Error} If database initialization fails after all retries
67
+ */
68
+ private constructor();
69
+ /**
70
+ * Gets the singleton instance of the Connection class.
71
+ * Creates a new instance if one doesn't exist.
72
+ *
73
+ * @param options - Optional configuration (only used on first call)
74
+ * @returns The singleton Connection instance
75
+ */
76
+ static getInstance(options?: ConnectionOptions): Connection;
77
+ /**
78
+ * Resets the singleton instance.
79
+ * Useful for testing or reconfiguring the connection.
80
+ *
81
+ * @param closeExisting - Whether to close existing connection before reset
82
+ */
83
+ static reset(closeExisting?: boolean): Promise<void>;
84
+ /**
85
+ * Tests the database connection
86
+ *
87
+ * @returns Promise that resolves to true if connection is healthy
88
+ */
89
+ testConnection(): Promise<boolean>;
90
+ /**
91
+ * Gets connection pool statistics
92
+ *
93
+ * @returns Current connection pool statistics
94
+ */
95
+ getStats(): ConnectionStats;
96
+ /**
97
+ * Executes a database transaction
98
+ *
99
+ * @param callback - Function to execute within the transaction
100
+ * @returns Result of the transaction
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * const result = await connection.transaction(async (tx) => {
105
+ * await tx.insert(schema.plugin).values({ ... });
106
+ * await tx.insert(schema.metadata).values({ ... });
107
+ * return { success: true };
108
+ * });
109
+ * ```
110
+ */
111
+ transaction<T>(callback: Parameters<typeof this.db.transaction>[0], timeoutMs?: number): Promise<T>;
112
+ /**
113
+ * Acquires a client from the pool for manual query execution
114
+ * Remember to release the client when done
115
+ *
116
+ * @returns PostgreSQL client from the pool
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * const client = await connection.getClient();
121
+ * try {
122
+ * await client.query('BEGIN');
123
+ * await client.query('INSERT INTO ...');
124
+ * await client.query('COMMIT');
125
+ * } finally {
126
+ * client.release();
127
+ * }
128
+ * ```
129
+ */
130
+ getClient(): Promise<PoolClient>;
131
+ /**
132
+ * Closes the database connection pool gracefully.
133
+ * Should be called during application shutdown.
134
+ *
135
+ * @param timeout - Maximum time to wait for connections to close (ms)
136
+ * @returns Promise that resolves when pool is closed
137
+ */
138
+ close(timeout?: number): Promise<void>;
139
+ /**
140
+ * Checks if the connection is shutting down
141
+ *
142
+ * @returns true if connection is in shutdown state
143
+ */
144
+ isClosing(): boolean;
145
+ /**
146
+ * Sets up event handlers for the connection pool
147
+ */
148
+ private setupEventHandlers;
149
+ /**
150
+ * Logs connection configuration (sanitized)
151
+ */
152
+ private logConnectionConfig;
153
+ }
154
+ /**
155
+ * Proxy-based lazy database instance
156
+ * The actual connection is only created when first accessed
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * import { db } from './connection';
161
+ *
162
+ * // Connection is created here on first use, not on import
163
+ * const plugins = await db.select().from(schema.plugin);
164
+ * ```
165
+ */
166
+ export declare const db: import("drizzle-orm/node-postgres").NodePgDatabase<Record<string, unknown>> & {
167
+ $client: Pool;
168
+ };
169
+ /**
170
+ * Gets the Connection instance for advanced operations
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * import { getConnection } from './connection';
175
+ *
176
+ * const connection = getConnection();
177
+ * const stats = connection.getStats();
178
+ * console.log(`Active connections: ${stats.totalCount}`);
179
+ * ```
180
+ */
181
+ export declare function getConnection(): Connection;
182
+ /**
183
+ * Closes the database connection
184
+ * Should be called during application shutdown
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * import { closeConnection } from './connection';
189
+ *
190
+ * process.on('SIGTERM', async () => {
191
+ * await closeConnection();
192
+ * process.exit(0);
193
+ * });
194
+ * ```
195
+ */
196
+ export declare function closeConnection(): Promise<void>;
197
+ /**
198
+ * Tests the database connection
199
+ *
200
+ * @returns Promise that resolves to true if connection is healthy
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * import { testConnection } from './connection';
205
+ *
206
+ * if (await testConnection()) {
207
+ * console.log('Database is ready');
208
+ * } else {
209
+ * console.error('Database connection failed');
210
+ * process.exit(1);
211
+ * }
212
+ * ```
213
+ */
214
+ export declare function testConnection(): Promise<boolean>;
215
+ /**
216
+ * Initialize the database connection explicitly
217
+ * Call this during application startup after environment is configured
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * import { initializeDatabase } from './connection';
222
+ *
223
+ * async function bootstrap() {
224
+ * // Load environment variables first
225
+ * dotenv.config();
226
+ *
227
+ * // Then initialize database
228
+ * await initializeDatabase();
229
+ * }
230
+ * ```
231
+ */
232
+ export declare function initializeDatabase(): Promise<void>;