Skip to main content

apps/gateway/src/services/postgres.ts

Metadata

Indexed Symbols

  • requireDatabaseUrl (line 6, function) - Implements require database url for service-layer operations.
  • createPool (line 14, function) - Implements create pool for service-layer operations.
  • getPool (line 31, function) - Implements get pool for service-layer operations.

Markdown Headings (if applicable)

No markdown headings detected.

Source Preview

import { Pool, type PoolClient, type QueryResult, type QueryResultRow } from "pg";
import { env } from "../config/env.js";

let pool: Pool | null = null;

function requireDatabaseUrl(): string {
const databaseUrl = env.databaseUrl;
if (!databaseUrl) {
throw new Error("DATABASE_URL is required for gateway persistence");
}
return databaseUrl;
}

function createPool(): Pool {
const connectionString = requireDatabaseUrl();
const sslMode = process.env.PGSSLMODE ?? "disable";
const isProductionLike = env.nodeEnv === "production" || env.nodeEnv === "staging";
const rejectUnauthorized = process.env.PGSSLREJECTUNAUTHORIZED
? process.env.PGSSLREJECTUNAUTHORIZED === "1" || process.env.PGSSLREJECTUNAUTHORIZED.toLowerCase() === "true"
: isProductionLike;

return new Pool({
connectionString,
ssl: sslMode === "require" ? { rejectUnauthorized } : undefined,
max: Number(process.env.PGPOOL_MAX ?? 20),