commit 2 refactor

This commit is contained in:
Boki 2026-10-20 14:35:45 -04:00
parent 588f666943
commit 32e43c9243
310 changed files with 33318 additions and 31189 deletions

View file

@ -1,5 +1,7 @@
import type { PoolConfig, QueryResultRow } from 'pg';
import { Pool } from 'pg';
import type { QueryResultRow } from 'pg';
import { at } from '@stock-bot/invariants';
import type { LoggerLike } from '@stock-bot/logger';
import { PostgreSQLHealthMonitor } from './health';
import { PostgreSQLQueryBuilder } from './query-builder';
import { PostgreSQLTransactionManager } from './transactions';
@ -23,7 +25,7 @@ export class PostgreSQLClient {
private pool: Pool | null = null;
private readonly config: PostgreSQLClientConfig;
private readonly options: PostgreSQLConnectionOptions;
private readonly logger: any;
private readonly logger: LoggerLike;
private readonly healthMonitor: PostgreSQLHealthMonitor;
private readonly transactionManager: PostgreSQLTransactionManager;
private isConnected = false;
@ -34,7 +36,7 @@ export class PostgreSQLClient {
constructor(
config: PostgreSQLClientConfig,
logger?: any,
logger?: LoggerLike,
options?: PostgreSQLConnectionOptions,
events?: ConnectionEvents
) {
@ -183,9 +185,9 @@ export class PostgreSQLClient {
/**
* Execute a query
*/
async query<T extends QueryResultRow = any>(
async query<T extends QueryResultRow = QueryResultRow>(
text: string,
params?: any[]
params?: unknown[]
): Promise<QueryResult<T>> {
if (!this.pool) {
throw new Error('PostgreSQL client not connected');
@ -262,9 +264,9 @@ export class PostgreSQLClient {
/**
* Execute a stored procedure or function
*/
async callFunction<T extends QueryResultRow = any>(
async callFunction<T extends QueryResultRow = QueryResultRow>(
functionName: string,
params?: any[]
params?: unknown[]
): Promise<QueryResult<T>> {
const placeholders = params ? params.map((_, i) => `$${i + 1}`).join(', ') : '';
const query = `SELECT * FROM ${functionName}(${placeholders})`;
@ -375,13 +377,16 @@ export class PostgreSQLClient {
)`,
[schemaName, tableName]
);
return result.rows[0].exists;
return at(result.rows, 0).exists;
}
/**
* Get table schema information
*/
async getTableSchema(tableName: string, schemaName: string = 'public'): Promise<any[]> {
async getTableSchema(
tableName: string,
schemaName: string = 'public'
): Promise<QueryResultRow[]> {
const result = await this.query(
`SELECT
column_name,
@ -400,16 +405,16 @@ export class PostgreSQLClient {
/**
* Execute EXPLAIN for query analysis
*/
async explain(query: string, params?: any[]): Promise<any[]> {
async explain(query: string, params?: unknown[]): Promise<QueryResultRow[]> {
const explainQuery = `EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ${query}`;
const result = await this.query(explainQuery, params);
return result.rows[0]['QUERY PLAN'];
return at(result.rows, 0)['QUERY PLAN'];
}
/**
* Get database statistics
*/
async getStats(): Promise<any> {
async getStats(): Promise<QueryResultRow> {
const result = await this.query(`
SELECT
(SELECT count(*) FROM pg_stat_activity WHERE state = 'active') as active_connections,
@ -417,7 +422,7 @@ export class PostgreSQLClient {
(SELECT setting FROM pg_settings WHERE name = 'max_connections') as max_connections,
pg_size_pretty(pg_database_size(current_database())) as database_size
`);
return result.rows[0];
return result.rows[0] ?? {};
}
/**
@ -434,7 +439,7 @@ export class PostgreSQLClient {
return this.pool;
}
private buildPoolConfig(): any {
private buildPoolConfig(): PoolConfig {
this.logger.debug('Building PostgreSQL pool config:', {
host: this.config.host,
port: this.config.port,