Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 28x 28x 28x | import { HyperdriveBinding } from '@/core/bindings/HyperdriveBinding'
import { HyperdriveCredentials } from '@/types/hyperdrive/HyperdriveCredentials'
import type { SqlConnectionProvider, SqlTarget } from '@bayudwiyansatria/core'
import type { CloudflareEnv } from '@/types/CloudflareEnv'
/**
* Hyperdrive binding.
*
* Every setting it needs — the binding name — arrives resolved from the
* configuration layer, so nothing about it is decided here. The binding itself
* is read from `env` per call, which is what makes one module-scope instance
* safe to share.
*/
const hyperdrive = new HyperdriveBinding()
/**
* External-database capability, backed by Hyperdrive.
*
* @remarks
* Hyperdrive supplies a pooled, cached connection — not a client. The query
* itself runs through a driver (`postgres`, `pg`, `mysql2`, …) that this
* boilerplate does not depend on, so this capability hands the connection out
* and stops there:
*
* ```ts
* const sql = postgres(hyperdrive.connectionString(env), { max: 5 })
* ```
*
* Keep the pool small — each isolate opens its own, and Hyperdrive is already
* pooling on the far side.
*
* @class
*
* @author Bayu Dwiyan Satria
* @version 1.0.0
* @since 1.0.0
*/
export class HyperdriveService<E extends CloudflareEnv = CloudflareEnv> implements SqlConnectionProvider<E> {
/**
* Returns a connection string for the pooled database.
*
* @remarks
* The credentials in it are scoped to this Worker instance — never log it or
* return it to a client.
*
* @param env The Worker environment.
* @returns A connection string accepted by the usual drivers and ORMs.
*/
public connectionString(env: E): string {
return hyperdrive.connectionString(env)
}
/**
* Returns the connection details as discrete fields.
*
* For drivers that take a credentials object rather than a URL.
*
* @param env The Worker environment.
* @returns Host, port, user, password, and database name.
*/
public credentials(env: E): HyperdriveCredentials {
return hyperdrive.credentials(env)
}
/**
* Reports which database the binding points at.
*
* Deliberately excludes the password and the connection string, so this is
* safe to surface in a health check.
*
* @param env The Worker environment.
* @returns The target host and database name, or `null` when unbound.
*/
public target(env: E): SqlTarget | null {
if (!hyperdrive.isBound(env)) {
return null
}
const { host, database } = hyperdrive.credentials(env)
return { host, database }
}
/**
* Reports whether Hyperdrive is bound to this Worker.
*
* @param env The Worker environment.
* @returns `true` when the binding is present.
*/
public isAvailable(env: E): boolean {
return hyperdrive.isBound(env)
}
}
|