Skip to content

postgres

Source: src/Drizzle/Postgres.ts

Open a Drizzle/Postgres database from a connection URL using the drizzle-orm/effect-postgres integration.

Returns a chainable Proxy over EffectPgDatabase (via proxyChain) — every property read records a step, every call records args, and the chain is replayed against the resolved drizzle db when it’s finally yielded as an Effect. Callers don’t need a separate yield* conn step:

const db = yield* Drizzle.postgres(hd.connectionString);
fetch: Effect.gen(function* () {
const rows = yield* db.select().from(users);
});

The connect work is deferred until the first query and memoized on the current ExecutionContext (ctx.cache), so the pool is built at most once per execution — a Worker fetch/queue/scheduled event or a Workflow run — and reused across every query and task step in that execution. Yielding the connection string is likewise deferred, so deploy / plan-time invocations (where WorkerEnvironment isn’t provided) never trigger a real connection attempt.

The pool is built against the execution’s Scope (ctx.scope), so its end finalizer fires when that scope closes — when the request / run settles, not when the Cloudflare.Worker init scope closes. Init runs inside Effect.scoped and closes after returning the exports object; building lazily against ctx.scope (rather than that init scope) is what keeps later requests from seeing “Cannot use a pool after end”.