Repository: ponder-sh/ponder
Pull request #1447 (2025-01-17 19:29:41)

feat: pg ssl connection options
See changeset, more discussion in https://github.com/ponder-sh/ponder/pull/1564


Patch:
diff --git a/.changeset/shy-crabs-battle.md b/.changeset/shy-crabs-battle.md
new file mode 100644
index 00000000..752aaffd
--- /dev/null
+++ b/.changeset/shy-crabs-battle.md
@@ -0,0 +1,5 @@
+---
+"ponder": patch
+---
+
+Added support for `ssl` options in Postgres pool configuration.
diff --git a/docs/pages/docs/api-reference/config.mdx b/docs/pages/docs/api-reference/config.mdx
index 4ad56849..fee242b6 100644
--- a/docs/pages/docs/api-reference/config.mdx
+++ b/docs/pages/docs/api-reference/config.mdx
@@ -277,6 +277,7 @@ export default createConfig({
     connectionString: "postgresql://user:password@localhost:5432/dbname",
     poolConfig: {
       max: 100,
+      ssl: true,
     },
   },
   // ... more config
diff --git a/packages/core/src/build/pre.ts b/packages/core/src/build/pre.ts
index 08367f33..83c2f4f4 100644
--- a/packages/core/src/build/pre.ts
+++ b/packages/core/src/build/pre.ts
@@ -65,8 +65,9 @@ export function buildPre({
       });
 
       const poolConfig = {
-        max: config.database.poolConfig?.max ?? 30,
         connectionString,
+        max: config.database.poolConfig?.max ?? 30,
+        ssl: config.database.poolConfig?.ssl ?? false,
       };
 
       databaseConfig = { kind: "postgres", poolConfig };
@@ -96,7 +97,7 @@ export function buildPre({
         msg: `Using Postgres database ${getDatabaseName(connectionString)} (${source})`,
       });
 
-      const poolConfig = { max: 30, connectionString };
+      const poolConfig = { connectionString, max: 30 };
 
       databaseConfig = { kind: "postgres", poolConfig };
     } else {
diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts
index 2e859547..f6a93bb1 100644
--- a/packages/core/src/config/index.ts
+++ b/packages/core/src/config/index.ts
@@ -1,3 +1,4 @@
+import type { ConnectionOptions } from "node:tls";
 import type { Prettify } from "@/types/utils.js";
 import type { Abi } from "abitype";
 import type { Narrow, Transport } from "viem";
@@ -58,6 +59,8 @@ type DatabaseConfig =
       poolConfig?: {
         /** Maximum number of clients in the pool. Default: `30`. */
         max?: number;
+        /** Enable SSL, or provide a custom SSL configuration. Default: `undefined`. */
+        ssl?: boolean | Prettify<ConnectionOptions>;
       };
     };
 


Test patch:
diff --git a/packages/core/src/build/pre.test.ts b/packages/core/src/build/pre.test.ts
index ba171ca9..f0a4a4d8 100644
--- a/packages/core/src/build/pre.test.ts
+++ b/packages/core/src/build/pre.test.ts
@@ -136,18 +136,32 @@ test("buildPre() database with postgres uses pool config", async () => {
     database: {
       kind: "postgres",
       connectionString: "postgres://username@localhost:5432/database",
-      poolConfig: { max: 100 },
+      poolConfig: {
+        max: 100,
+        ssl: {
+          ca: "ca",
+          cert: "cert",
+          key: "key",
+        },
+        // @ts-expect-error
+        unsupported: "unsupported",
+      },
     },
     networks: { mainnet: { chainId: 1, transport: http() } },
     contracts: { a: { network: "mainnet", abi: [] } },
   });
 
   const { databaseConfig } = buildPre({ config, options });
-  expect(databaseConfig).toMatchObject({
+  expect(databaseConfig).toStrictEqual({
     kind: "postgres",
     poolConfig: {
       connectionString: "postgres://username@localhost:5432/database",
       max: 100,
+      ssl: {
+        ca: "ca",
+        cert: "cert",
+        key: "key",
+      },
     },
   });
 });