Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.1 KB
Raw Blame
import { AppConnectionValueForAuthProperty } from "@activepieces/pieces-framework";
import { Client } from "pg";
import type { postgresAuth } from "./auth";

export const pgClient = async (auth: AppConnectionValueForAuthProperty<typeof postgresAuth>, query_timeout = 30000, application_name: string | undefined = undefined , connectionTimeoutMillis = 30000) => {
    const {
        host,
        user,
        database,
        password,
        port,
        enable_ssl,
        reject_unauthorized: rejectUnauthorized,
        certificate,
    } = auth.props;

    const sslConf = {
        rejectUnauthorized: rejectUnauthorized,
        ca: certificate && certificate.length > 0 ? certificate : undefined,
    };
    const client = new Client({
        host,
        port: Number(port),
        user,
        password,
        database,
        ssl: enable_ssl ? sslConf : undefined,
        query_timeout: Number(query_timeout),
        statement_timeout: Number(query_timeout),
        application_name,
        connectionTimeoutMillis: Number(connectionTimeoutMillis),
    });
    await client.connect();

    return client;
}