Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pg-client-connect-error-handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/sql-pg": patch
---

Prevent unhandled `pg` client error events while `PgClient.makeClient` is connecting.
12 changes: 9 additions & 3 deletions packages/sql/pg/src/PgClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ export const makeClient = (
*/
readonly acquireForStream?: boolean | undefined
}
): Effect.Effect<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
fromClient({
): Effect.Effect<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> => {
function onError() {}
return fromClient({
...options,
acquire: Effect.acquireRelease(
Effect.tryPromise({
Expand All @@ -237,13 +238,17 @@ export const makeClient = (
application_name: options.applicationName ?? "@effect/sql-pg",
types: options.types
})
client.on("error", onError)
await client.connect()
return client
},
catch: (cause) => new SqlError({ reason: classifyError(cause, "PgClient: Failed to connect", "connect") })
}),
(client) =>
Effect.promise(() => client.end()).pipe(
Effect.promise(() => {
client.off("error", onError)
return client.end()
}).pipe(
Effect.timeoutOption(1000)
),
{ interruptible: true }
Expand All @@ -264,6 +269,7 @@ export const makeClient = (
),
acquireForStream: options.acquireForStream ?? false
})
}

/**
* Builds a PostgreSQL client from a scoped `pg` pool acquisition effect, deriving transaction, streaming, and LISTEN/NOTIFY support from that pool.
Expand Down
31 changes: 31 additions & 0 deletions packages/sql/pg/test/Client.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { TestClock } from "effect/testing"
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
import { SqlClient } from "effect/unstable/sql"
import * as Statement from "effect/unstable/sql/Statement"
import * as Pg from "pg"
import { parse as parsePgConnectionString } from "pg-connection-string"
import { vi } from "vitest"
import { PgContainer } from "./utils.ts"

const compilerTransform = PgClient.makeCompiler(String.camelToSnake)
Expand Down Expand Up @@ -295,6 +297,35 @@ it.layer(PgContainer.layerMakeClient, { timeout: "30 seconds" })("PgClient.makeC
}))
})

it.effect("PgClient.makeClient handles errors emitted while connecting", () =>
Effect.acquireUseRelease(
Effect.sync(() => ({
connect: vi.spyOn(Pg.Client.prototype, "connect").mockImplementation(function(this: Pg.Client) {
return new Promise<void>((resolve, reject) => {
queueMicrotask(() => {
try {
this.emit("error", new Error("connection failed"))
resolve()
} catch (cause) {
reject(cause)
}
})
})
}),
end: vi.spyOn(Pg.Client.prototype, "end").mockResolvedValue(undefined)
})),
() =>
PgClient.makeClient({ host: "localhost" }).pipe(
Effect.scoped,
Effect.provide(Reactivity.layer)
),
({ connect, end }) =>
Effect.sync(() => {
connect.mockRestore()
end.mockRestore()
})
))

it.layer(PgContainer.layerClientWithTransforms, { timeout: "30 seconds" })("PgClient transforms", (it) => {
it.effect("insert helper", () =>
Effect.gen(function*() {
Expand Down
Loading