stock-bot/libs/data/postgres
2025-06-22 23:48:01 -04:00
..
src refactored monorepo for more projects 2025-06-22 23:48:01 -04:00
package.json moved folders around 2025-06-21 18:27:00 -04:00
README.md moved folders around 2025-06-21 18:27:00 -04:00
tsconfig.json format 2025-06-22 17:55:51 -04:00

PostgreSQL Client Library

A comprehensive PostgreSQL client library for the Stock Bot trading platform, designed for operational data, transactions, and relational queries.

Features

  • Connection Pooling: Robust connection pool management
  • Type Safety: Full TypeScript support with typed queries
  • Transaction Support: Multi-statement transactions with rollback
  • Schema Management: Database schema validation and migrations
  • Query Builder: Fluent query building interface
  • Health Monitoring: Connection health monitoring and metrics
  • Performance Tracking: Query performance monitoring and optimization

Usage

import { PostgreSQLClient } from '@stock-bot/postgres';

// Initialize client
const pgClient = new PostgreSQLClient();
await pgClient.connect();

// Execute a query
const users = await pgClient.query('SELECT * FROM users WHERE active = $1', [true]);

// Use query builder
const trades = await pgClient
  .select('*')
  .from('trades')
  .where('symbol', '=', 'AAPL')
  .orderBy('created_at', 'DESC')
  .limit(10)
  .execute();

// Execute in transaction
await pgClient.transaction(async (tx) => {
  await tx.query('INSERT INTO trades (...) VALUES (...)', []);
  await tx.query('UPDATE portfolio SET balance = balance - $1', [amount]);
});

Database Schemas

The client provides typed access to the following schemas:

  • trading: Core trading operations (trades, orders, positions)
  • strategy: Strategy definitions and performance
  • risk: Risk management and compliance
  • audit: Audit trails and logging

Configuration

Configure using environment variables:

POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DATABASE=stockbot
POSTGRES_USERNAME=stockbot
POSTGRES_PASSWORD=your_password

Query Builder

The fluent query builder supports:

  • SELECT, INSERT, UPDATE, DELETE operations
  • Complex WHERE conditions with AND/OR logic
  • JOINs (INNER, LEFT, RIGHT, FULL)
  • Aggregations (COUNT, SUM, AVG, etc.)
  • Subqueries and CTEs
  • Window functions

Health Monitoring

The client includes built-in health monitoring:

const health = await pgClient.getHealth();
console.log(health.status); // 'healthy' | 'degraded' | 'unhealthy'