Add initial shared code packages
This commit is contained in:
commit
838a97d344
54 files changed
+6265
No files matched your search
@@ -0,0 +1,92 @@
|
||||
import pg from 'pg';
|
||||
import { databaseConfigFromEnv } from './config.js';
|
||||
|
||||
const { Pool } = pg;
|
||||
|
||||
const TRANSIENT_CODES = new Set(['ECONNRESET', 'EPIPE', '57P01', '08006', '08003']);
|
||||
const TRANSIENT_MESSAGE = /connection terminated|connection reset|econnreset|epipe|server closed the connection/i;
|
||||
|
||||
export function isTransientConnectionError(error) {
|
||||
return Boolean(
|
||||
error &&
|
||||
(TRANSIENT_CODES.has(error.code) || TRANSIENT_MESSAGE.test(String(error.message || '')))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Owns one pg pool and exposes bounded startup, retry, transaction, and
|
||||
* shutdown behavior. Domain repositories should depend on this abstraction
|
||||
* instead of creating independent pools.
|
||||
*/
|
||||
export class Database {
|
||||
constructor(options = {}) {
|
||||
const { pool, ...poolOptions } = options;
|
||||
this.pool = pool || new Pool(poolOptions);
|
||||
this.ownsPool = !pool;
|
||||
this.ready = false;
|
||||
}
|
||||
|
||||
static fromEnv(env = process.env) {
|
||||
return new Database(databaseConfigFromEnv(env));
|
||||
}
|
||||
|
||||
async connect({ retries = 10, retryDelayMs = 2000 } = {}) {
|
||||
if (this.ready) return this;
|
||||
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= Math.max(1, retries); attempt += 1) {
|
||||
try {
|
||||
await this.pool.query('SELECT 1');
|
||||
this.ready = true;
|
||||
return this;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt >= retries || !isTransientConnectionError(error)) throw error;
|
||||
const delay = Math.min(Math.max(100, retryDelayMs) * 2 ** (attempt - 1), 30000);
|
||||
await new Promise(resolve => setTimeout(resolve, delay));
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async query(text, values = []) {
|
||||
await this.connect();
|
||||
try {
|
||||
return await this.pool.query(text, values);
|
||||
} catch (error) {
|
||||
if (!isTransientConnectionError(error)) throw error;
|
||||
return this.pool.query(text, values);
|
||||
}
|
||||
}
|
||||
|
||||
async transaction(callback) {
|
||||
if (typeof callback !== 'function') throw new TypeError('transaction callback is required');
|
||||
await this.connect();
|
||||
const client = await this.pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
const result = await callback(client);
|
||||
await client.query('COMMIT');
|
||||
return result;
|
||||
} catch (error) {
|
||||
try {
|
||||
await client.query('ROLLBACK');
|
||||
} catch {
|
||||
// Preserve the original application error.
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async close() {
|
||||
if (!this.pool || !this.ownsPool) return;
|
||||
await this.pool.end();
|
||||
this.ready = false;
|
||||
}
|
||||
}
|
||||
|
||||
export function createDatabaseFromEnv(env = process.env) {
|
||||
return Database.fromEnv(env);
|
||||
}
|
||||
Reference in new issue
Block a user