Skip to main content

@rtorcato/cf-common

Interfaces​

HyperdriveOptions​

Defined in: index.ts:3

Type Parameters​

C​

C

Properties​

connect​

connect: (connectionString) => C | Promise<C>

Defined in: index.ts:5

Open a client from the connection string, e.g. (cs) => postgres(cs) or async (cs) => { const c = new Client({ connectionString: cs }); await c.connect(); return c }.

Parameters​
connectionString​

string

Returns​

C | Promise<C>

close​

close: (client) => unknown

Defined in: index.ts:7

Close the client, e.g. (sql) => sql.end().

Parameters​
client​

C

Returns​

unknown

ctx?​

optional ctx?: ExecutionContext<unknown>

Defined in: index.ts:9

When passed, close runs in ctx.waitUntil after work settles instead of being awaited.

Functions​

withHyperdrive()​

withHyperdrive<C, R>(hyperdrive, options, work): Promise<R>

Defined in: index.ts:26

Open a per-request client over a Hyperdrive binding, run work, and always close the client, whether work resolves or throws. Hyperdrive pools the connections, so don't cache the client across requests. The driver is yours: this module doesn't bundle one.

A failed connect throws a non-exposed 502 with code HYPERDRIVE_CONNECT_FAILED. Errors from work pass through unchanged.

Example​

const hd = getBinding<Hyperdrive>(env, 'HYPERDRIVE')
const rows = await withHyperdrive(hd, { connect: (cs) => postgres(cs), close: (sql) => sql.end(), ctx },
(sql) => sql`SELECT * FROM users WHERE id = ${id}`)

Type Parameters​

C​

C

R​

R

Parameters​

hyperdrive​

Hyperdrive

options​

HyperdriveOptions<C>

work​

(client) => Promise<R>

Returns​

Promise<R>