Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | /** * PostgreSQL Database Adapter for Coherent.js * * @fileoverview PostgreSQL adapter implementation with connection pooling and advanced features. */ /** * Create PostgreSQL adapter instance * * @returns {Object} PostgreSQL adapter instance */ export function createPostgreSQLAdapter() { let pg = null; async function initializePostgreSQL() { if (!pg) { try { const pgModule = await import('pg'); pg = pgModule.default || pgModule; } catch { throw new Error('pg package is required for PostgreSQL adapter. Install with: npm install pg'); } } } function convertPlaceholders(sql) { let index = 1; return sql.replace(/\?/g, () => `$${index++}`); } function extractInsertId(result) { if (result.rows && result.rows.length > 0) { const row = result.rows[0]; return row.id || row.insertId || row.lastval || null; } return null; } return { /** * Create connection pool */ async createPool(config) { await initializePostgreSQL(); const poolConfig = { host: config.host, port: config.port, database: config.database, user: config.username, password: config.password, min: config.pool.min, max: config.pool.max, acquireTimeoutMillis: config.pool.acquireTimeoutMillis, createTimeoutMillis: config.pool.createTimeoutMillis, destroyTimeoutMillis: config.pool.destroyTimeoutMillis, idleTimeoutMillis: config.pool.idleTimeoutMillis, reapIntervalMillis: config.pool.reapIntervalMillis, createRetryIntervalMillis: config.pool.createRetryIntervalMillis, ssl: config.ssl || false }; const pool = new pg.Pool(poolConfig); pool.on('error', (err) => { console.error('PostgreSQL pool error:', err); }); return pool; }, /** * Test database connection */ async testConnection(pool) { const client = await pool.connect(); try { await client.query('SELECT 1'); } finally { client.release(); } }, /** * Execute database query */ async query(pool, sql, params = [], options = {}) { const client = await pool.connect(); try { const pgSql = convertPlaceholders(sql); const result = await client.query(pgSql, params); if (options.single) { return result.rows[0] || null; } return { rows: result.rows, rowCount: result.rowCount, affectedRows: result.rowCount, insertId: extractInsertId(result) }; } finally { client.release(); } }, /** * Start database transaction */ async transaction(pool, options = {}) { const client = await pool.connect(); let beginSql = 'BEGIN'; if (options.isolationLevel) { beginSql += ` ISOLATION LEVEL ${options.isolationLevel}`; } if (options.readOnly) { beginSql += ' READ ONLY'; } await client.query(beginSql); const transaction = { client, pool, isCommitted: false, isRolledBack: false, query: async (sql, params, queryOptions) => { if (transaction.isCommitted || transaction.isRolledBack) { throw new Error('Cannot execute query on completed transaction'); } const pgSql = convertPlaceholders(sql); const result = await client.query(pgSql, params); if (queryOptions && queryOptions.single) { return result.rows[0] || null; } return { rows: result.rows, rowCount: result.rowCount, affectedRows: result.rowCount, insertId: extractInsertId(result) }; }, commit: async () => { if (transaction.isCommitted || transaction.isRolledBack) { throw new Error('Transaction already completed'); } try { await client.query('COMMIT'); transaction.isCommitted = true; } finally { client.release(); } }, rollback: async () => { if (transaction.isCommitted || transaction.isRolledBack) { throw new Error('Transaction already completed'); } try { await client.query('ROLLBACK'); transaction.isRolledBack = true; } finally { client.release(); } } }; return transaction; }, /** * Get pool statistics */ getPoolStats(pool) { return { total: pool.totalCount, available: pool.idleCount, acquired: pool.totalCount - pool.idleCount, waiting: pool.waitingCount }; }, /** * Close connection pool */ async closePool(pool) { await pool.end(); } }; } |