feat(db): DATABASE_URL и общий Postgres (Postgres_TG_Bots), БД clinic_tests
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Database Connection Module
|
||||
* PostgreSQL connection pool and utility functions
|
||||
*/
|
||||
|
||||
import pg from 'pg';
|
||||
import { getPoolConfig } from './poolConfig.js';
|
||||
|
||||
const { Pool } = pg;
|
||||
|
||||
const pool = new Pool(getPoolConfig());
|
||||
|
||||
// Handle pool errors
|
||||
pool.on('error', (err) => {
|
||||
console.error('Unexpected pool error:', err.message);
|
||||
});
|
||||
|
||||
/**
|
||||
* Execute a query with the connection pool
|
||||
* @param {string} text - SQL query text
|
||||
* @param {Array} params - Query parameters
|
||||
* @returns {Promise<pg.QueryResult>} Query result
|
||||
*/
|
||||
export async function query(text, params) {
|
||||
const start = Date.now();
|
||||
const result = await pool.query(text, params);
|
||||
const duration = Date.now() - start;
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log('Executed query:', { text: text.substring(0, 50), duration, rows: result.rowCount });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query with automatic client release
|
||||
* @param {string} text - SQL query text
|
||||
* @param {Array} params - Query parameters
|
||||
* @returns {Promise<pg.QueryResult>} Query result
|
||||
*/
|
||||
export async function queryWithClient(text, params) {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
return await client.query(text, params);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a transaction
|
||||
* @param {Function} callback - Async function receiving client as parameter
|
||||
* @returns {Promise<any>} Transaction result
|
||||
*/
|
||||
export async function transaction(callback) {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
const result = await callback(client);
|
||||
await client.query('COMMIT');
|
||||
return result;
|
||||
} catch (error) {
|
||||
await client.query('ROLLBACK');
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a client from the pool
|
||||
* @returns {Promise<pg.PoolClient>} Pool client
|
||||
*/
|
||||
export async function getClient() {
|
||||
return pool.connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pool status information
|
||||
* @returns {Object} Pool statistics
|
||||
*/
|
||||
export function getPoolStatus() {
|
||||
return {
|
||||
totalCount: pool.totalCount,
|
||||
idleCount: pool.idleCount,
|
||||
waitingCount: pool.waitingCount,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection pool
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function closePool() {
|
||||
await pool.end();
|
||||
}
|
||||
|
||||
// Default export the pool for direct access if needed
|
||||
export default pool;
|
||||
@@ -6,18 +6,10 @@
|
||||
import { readFileSync, readdirSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import pg from 'pg';
|
||||
import { getPoolConfig } from './poolConfig.js';
|
||||
|
||||
const { Pool } = pg;
|
||||
|
||||
// Database configuration
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: parseInt(process.env.DB_PORT || '5433', 10),
|
||||
database: process.env.DB_NAME || 'clinic_tests',
|
||||
user: process.env.DB_USER || 'developer',
|
||||
password: process.env.DB_PASSWORD || 'dev_password',
|
||||
};
|
||||
|
||||
const MIGRATIONS_DIR = join(process.cwd(), 'src', 'db', 'migrations');
|
||||
|
||||
/**
|
||||
@@ -81,7 +73,7 @@ async function executeMigration(pool, filename) {
|
||||
* Main migration function
|
||||
*/
|
||||
async function migrate() {
|
||||
const pool = new Pool(dbConfig);
|
||||
const pool = new Pool(getPoolConfig());
|
||||
|
||||
try {
|
||||
console.log('Connecting to database...');
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Параметры пула node-postgres, единообразно с HR_TG_Bot / Postgres_TG_Bots:
|
||||
* приоритет у `DATABASE_URL` (postgresql://…), иначе DB_HOST, DB_PORT, DB_NAME, …
|
||||
*/
|
||||
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
/**
|
||||
* @param {import('pg').PoolConfig} [overrides]
|
||||
* @returns {import('pg').PoolConfig}
|
||||
*/
|
||||
export function getPoolConfig(overrides = {}) {
|
||||
const url = process.env.DATABASE_URL?.trim();
|
||||
if (url) {
|
||||
return {
|
||||
connectionString: url,
|
||||
max: parseInt(process.env.DB_POOL_MAX || '20', 10),
|
||||
idleTimeoutMillis: parseInt(process.env.DB_IDLE_TIMEOUT || '30000', 10),
|
||||
connectionTimeoutMillis: parseInt(
|
||||
process.env.DB_CONNECTION_TIMEOUT || '2000',
|
||||
10
|
||||
),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
return {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: parseInt(process.env.DB_PORT || '5432', 10),
|
||||
database: process.env.DB_NAME || 'clinic_tests',
|
||||
user: process.env.DB_USER || 'developer',
|
||||
password: process.env.DB_PASSWORD || 'dev_password',
|
||||
max: parseInt(process.env.DB_POOL_MAX || '20', 10),
|
||||
idleTimeoutMillis: parseInt(process.env.DB_IDLE_TIMEOUT || '30000', 10),
|
||||
connectionTimeoutMillis: parseInt(
|
||||
process.env.DB_CONNECTION_TIMEOUT || '2000',
|
||||
10
|
||||
),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user