@vibeorm/adapter-bun 4.3.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -3
- package/dist/connection-url.d.ts +24 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +545 -42
- package/dist/index.js.map +5 -5
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -33,7 +33,8 @@ await db.$disconnect();
|
|
|
33
33
|
| Option | Type | Default | Meaning |
|
|
34
34
|
|---|---|---|---|
|
|
35
35
|
| `url` | `string` | — (required) | PostgreSQL connection URL |
|
|
36
|
-
| `max` | `number` | `10` | Maximum pooled connections |
|
|
36
|
+
| `max` | `number` | `10` | Maximum pooled connections. bun:sql keeps an idle connection open until the server or network closes it (its `idleTimeout` defaults to `0`, and the adapter sets none), so no reconnect follows a quiet spell |
|
|
37
|
+
| `min` | `number` | `0` | Connections `db.$connect()` reserves at once before it returns (at least one), so the first burst of parallel queries finds them ready instead of paying TCP + startup + authentication (on a far database about three round trips each — see adapter-pg's "A database far away"). bun:sql opens the pool's connections at its first use and closes none while idle, so they stay open. An integer from `0` through `max`; other values throw `VIBE_CONFIG` |
|
|
37
38
|
| `statementTimeout` | `number` (ms) | — | Pool-wide `statement_timeout`, sent as a startup parameter so every connection inherits it; a per-transaction `timeout` overrides it |
|
|
38
39
|
| `connectionTimeout` | `number` (ms) | URL `connect_timeout`, else none | Wait for a connection to be established. bun:sql expects seconds; the adapter converts |
|
|
39
40
|
| `preparedStatements` | `boolean` | `false` | Use bun:sql's **named** prepared statements and run ORM-path queries as tagged templates (on Bun 1.4.3 and newer, a statement whose placeholders are `$1 … $n` in order binds by `$n` through `sql.unsafe`, which names it the same way). Off, every statement is an unnamed statement with text results — the node-postgres model. Read "Statements and values" before you turn it on |
|
|
@@ -44,6 +45,8 @@ await db.$disconnect();
|
|
|
44
45
|
|
|
45
46
|
`planCacheMode` and `statementTimeout` merge into one `options` startup parameter, with adapter settings taking precedence over matching URL settings. Other URL settings are preserved. bun:sql forwards unrecognised URL parameters to the server, which rejects a client-side libpq option. The adapter therefore takes libpq's `connect_timeout` (whole seconds) out of the URL and uses it as `connectionTimeout`, so a URL written for node-postgres or `psql` connects here too; an explicit `connectionTimeout` option wins. Other client-side libpq options must not go in the URL. (The same forwarding is how a server setting reaches every connection: `?jit=off` on the URL disables JIT compilation for the pool — see `docs/dialect-notes.md` "PostgreSQL JIT and statistics".)
|
|
46
47
|
|
|
48
|
+
libpq's `sslrootcert=system` ("trust the CA roots of the system", as in PlanetScale's `?sslmode=verify-full&sslrootcert=system`) works as well. The adapter replaces it with `sslmode=verify-full`, so bun:sql verifies the server certificate's chain and host name against its default trust store (Bun's bundled roots plus `NODE_EXTRA_CA_CERTS`). Without that, Bun 1.4.1 forwards `sslrootcert` to the server, which refuses it, and Bun 1.4.4 lets `sslmode=verify-ca` stand next to it. As in libpq, an omitted `sslmode` means `verify-full`, and a weaker mode next to `sslrootcert=system` (also bun:sql's spellings such as `ssl=false`) is refused at construction with `VIBE_CONFIG` (`meta.reason: "sslrootcert-system-weak-sslmode"`).
|
|
49
|
+
|
|
47
50
|
`connectionTimeout` does **not** bound waiting for a busy pool connection. The adapter therefore declares `budgets.acquireTimeout: false`. Newer Bun versions support cancellable explicit reservations, but implicit query and transaction checkout does not expose the same guarantee across supported versions. There is no timer race that leaves an abandoned checkout running.
|
|
48
51
|
|
|
49
52
|
`Date` parameters are spelled as UTC ISO text on both paths: bun:sql's own serializer for a `date`-typed parameter is `Date#toString()`, which the server rejects in every host time zone. Zone-less columns (`@db.Timestamp(n)`, `@db.Date`) therefore hold the UTC wall-clock on write — the contract shared with node-postgres, PGlite and Prisma. On read, bun:sql is UTC from **Bun 1.4.1**; earlier Bun versions parse zone-less timestamp text as host-local time (1.3.x: scalars on parameterless statements and every array element; 1.4.0: array elements), and bun:sql has no per-query parser hook the adapter could use to correct it. On Bun < 1.4.1 keep the process on `TZ=UTC` when the schema has zone-less columns, or upgrade (`docs/dialect-notes.md` "DateTime without time zone on postgres").
|
|
@@ -54,7 +57,7 @@ By default the adapter runs bun:sql with `prepare: false`: every statement is an
|
|
|
54
57
|
|
|
55
58
|
`preparedStatements: true` switches bun:sql to **named** statements: one per SQL text per connection, pipelined, with results in the binary format for some types. On the repository benchmark (Bun 1.4.3, PostgreSQL 18, small scale, 27 scenarios, three interleaved rounds) the default mode, named mode and adapter-pg were within 3 % of each other (geometric mean of the median p50s); single rounds varied by up to 10 %, so there is no measured speed reason to turn named statements on where the database is local. Across a network, named mode saves round trips because Bun pipelines only named statements: transaction control (`BEGIN`, `SAVEPOINT`, a deadline bound) rides with the next read, and concurrent statements inside one transaction share a round trip (a one-read `$transaction` costs 2 round trips instead of 3; `tests/round-trip-wire.live.test.ts` pins both modes). Named mode also changes behaviour:
|
|
56
59
|
|
|
57
|
-
- After DDL that changes a statement's result columns (`ALTER COLUMN … TYPE`, or `ADD COLUMN` under `SELECT *`), that statement fails with SQLSTATE `0A000` ("cached plan must not change result type") on every connection that prepared it, until the pool is replaced. Run migrations with the application stopped, or restart it after. node-postgres named statements behave the same way.
|
|
60
|
+
- After DDL that changes a statement's result columns (`ALTER COLUMN … TYPE`, or `ADD COLUMN` under `SELECT *`), that statement fails with SQLSTATE `0A000` ("cached plan must not change result type") on every connection that prepared it, until the pool is replaced — measured on Bun 1.4.3. Bun 1.4.4-canary.1 re-prepares the statement and returns the row (`tests/node-postgres-parity.live.test.ts`). Run migrations with the application stopped, or restart it after. node-postgres named statements behave the same way.
|
|
58
61
|
- `int4[]` and `float4[]` values with a NULL element or more than one dimension are refused by bun:sql (`ERR_POSTGRES_NULLS_IN_ARRAY_NOT_SUPPORTED_YET`, `ERR_POSTGRES_MULTIDIMENSIONAL_ARRAY_NOT_SUPPORTED_YET`).
|
|
59
62
|
- A string bound to a raw `::jsonb` slot is JSON-encoded again (see "Raw parameters").
|
|
60
63
|
- A plain object bound to a raw slot that is not `json`/`jsonb` (for example `$1::text`) arrives as `[object Object]`; the default mode and node-postgres send its `JSON.stringify` text.
|
|
@@ -107,7 +110,17 @@ Every top-level `transaction()`, with or without options, runs on a reserved con
|
|
|
107
110
|
|
|
108
111
|
Nested transactions become savepoints: the adapter issues `SAVEPOINT` / `RELEASE SAVEPOINT` / `ROLLBACK TO SAVEPOINT` SQL by hand on the reserved connection, with a name counter shared across the whole top-level transaction.
|
|
109
112
|
|
|
110
|
-
With `preparedStatements: true`, the array form `$transaction([...])` sends its operations in bursts, as on adapter-pg: after
|
|
113
|
+
With `preparedStatements: true`, the array form `$transaction([...])` sends its operations in bursts, as on adapter-pg: after an operation sent as one statement, the next operation's first statement goes out right behind it, before its reply. A `timeout` does not switch bursts off (its bound rides behind `BEGIN`; PostgreSQL times each statement on its own). `BEGIN` is still confirmed before the first write (bun:sql ends every query with its own Sync, so a write sent behind a failed BEGIN would commit on its own). When a statement of the burst fails on the server, the statements already sent behind it reach a transaction that is already aborted: PostgreSQL refuses them without running them (SQLSTATE `25P02`, reported as the aborted-transaction refusal naming the first failure), so nothing they would do happens — no row, no trigger, no sequence value. A statement whose failure bun:sql may raise before sending it (more than 65,535 parameters, a JS array, an invalid `Date` or one outside the years 1–9999, a document object) and one without bound values keep their own round trip instead. Not used in the default unnamed mode (bun:sql does not pipeline unnamed statements), under a `deadline`, for an array inside a transaction, or with an `onQuery` / `$on("query")` listener. Results, error codes and meta, and what commits are the same as one operation after another; the timing differences the runtime names for bursts (an operation's arguments and JavaScript defaults are read up to one round trip earlier) apply here too.
|
|
114
|
+
|
|
115
|
+
Also with `preparedStatements: true` and without a `deadline` (round-trip parity with adapter-pg, 5.1):
|
|
116
|
+
|
|
117
|
+
- **COMMIT travels with the last statement** (`withCommit`): behind the last operation of an array, behind the last read of `$all`, behind a `commitWith` statement and behind the last relation wave of a read that ends a reads-only transaction, wherever the runtime asks adapter-pg for it. `[findUnique, count]` and `$all { a, b }` cost 1 round trip instead of 2; `[deleteMany, createMany, updateMany]` 2 instead of 3. COMMIT rides only behind a statement whose every failure the server answers (as above), with no savepoint open and nothing else of the transaction still to be sent; then a failure before it aborts the transaction and the server answers COMMIT with ROLLBACK, so nothing commits. A deferred constraint refused at COMMIT rejects the transaction with that error (`VIBE_FK_VIOLATION`, …), as adapter-pg. A statement after the riding COMMIT is refused (`VIBE_TRANSACTION`, `meta.reason: "commit-sent"`).
|
|
118
|
+
- **Sibling relation loads** of a `"query"`-strategy read share one round trip inside a transaction (lane groups, `PipelineLane.group`) and in autocommit (`connectionGroup`; in the default mode they run on idle pooled connections in parallel instead). A callback transaction reading a row with 3 sibling relations costs 3 round trips instead of 5.
|
|
119
|
+
- **Bound statements nobody reads** (`deferStatement`: native-RLS claims, the `advisoryLock` option) ride with the next statement; ahead of a write, behind the confirmed `BEGIN`.
|
|
120
|
+
- **The transaction `timeout`** (`statement_timeout` bound) and, with `deferSettings: true`, the **transaction `settings`** wait behind `BEGIN` and ride with the first statement, in that order, ahead of the claims. A read callback with a `timeout` costs 2 round trips instead of 3, `[findMany, count]` with a `timeout` 1 instead of 2, settings + one read 2 instead of 4, settings + one write 3 instead of 4; a callback that sends nothing sends nothing. A refused deferred setting rejects the first statement with the `setting-refused` error (`meta.deferred: true`), as on adapter-pg. A `timeout` above PostgreSQL's limit keeps the eager opening. With the default unnamed statements both stay eager: Bun pipelines nothing unnamed, so deferring would save no round trip (and an unnamed deferred `timeout` would cost one more than the one eager message).
|
|
121
|
+
- In both modes a successful nested transaction's `RELEASE SAVEPOINT` rides with the next statement, and `COMMIT` drops it.
|
|
122
|
+
|
|
123
|
+
What adapter-bun cannot match: `BEGIN` ahead of a write (one round trip more than adapter-pg for every transaction whose first statement writes, so a write with `include` keeps 2 round trips and opens no transaction of its own); a first execution of a named statement on a connection costs 2 round trips (bun:sql describes it before binding); and `connectionIdentity` (bun:sql exposes no stable per-connection object).
|
|
111
124
|
|
|
112
125
|
Nested handles share the root budget and expire when their own callback finishes. Deadline-bearing statements are serialized on the connection, so each checks the remaining time after earlier work completes. Server-cancellation mode installs a fresh `statement_timeout` before each adapter statement and before COMMIT. This does not cancel JavaScript callbacks, network waits or pool acquisition. Trusted raw SQL can change server settings, and a multi-command script applies PostgreSQL's timeout separately to its commands; it is not a hard wall-clock limit for the whole script. Use individual adapter calls when the total deadline must be checked between commands.
|
|
113
126
|
|
package/dist/connection-url.d.ts
CHANGED
|
@@ -12,6 +12,12 @@
|
|
|
12
12
|
* configuration parameter (42704). A URL written for libpq or node-postgres
|
|
13
13
|
* still works here: {@link takeConnectTimeout} removes `connect_timeout` and
|
|
14
14
|
* hands its seconds to bun:sql's `connectionTimeout` constructor option.
|
|
15
|
+
*
|
|
16
|
+
* libpq's `sslrootcert=system` (PlanetScale's URLs carry it) is the same
|
|
17
|
+
* hazard on the minimum supported Bun: 1.4.1 forwards it as a startup
|
|
18
|
+
* parameter (and without `sslmode` connects in plain text), while 1.4.4 reads
|
|
19
|
+
* it but lets `sslmode=verify-ca` stand. {@link takeSystemRootCert} turns it
|
|
20
|
+
* into `sslmode=verify-full` with libpq's rule on weaker modes.
|
|
15
21
|
*/
|
|
16
22
|
/**
|
|
17
23
|
* PostgreSQL plan-cache behaviour for every plan the server caches on the
|
|
@@ -64,6 +70,24 @@ export declare function takeConnectTimeout(params: {
|
|
|
64
70
|
url: string;
|
|
65
71
|
seconds?: number;
|
|
66
72
|
};
|
|
73
|
+
/**
|
|
74
|
+
* Rewrite a URL carrying libpq's `sslrootcert=system` ("trust the system CA
|
|
75
|
+
* roots") into `sslmode=verify-full` without it, so bun:sql verifies the
|
|
76
|
+
* server certificate's chain and host name against its default trust store
|
|
77
|
+
* (Bun's bundled roots plus `NODE_EXTRA_CA_CERTS`) on every supported Bun, and
|
|
78
|
+
* nothing reaches the server as a startup parameter. Any other URL is
|
|
79
|
+
* returned unchanged.
|
|
80
|
+
*
|
|
81
|
+
* Follows libpq: with `system`, an omitted mode means `verify-full` and every
|
|
82
|
+
* weaker one is an error — also bun:sql's own spellings (`ssl=false`,
|
|
83
|
+
* `tls=require`, `ssl-mode=prefer`, …).
|
|
84
|
+
*
|
|
85
|
+
* @throws VibeError `VIBE_CONFIG` (`meta.reason: "sslrootcert-system-weak-sslmode"`)
|
|
86
|
+
* for a weaker mode. Neither the message nor `meta` carries the URL.
|
|
87
|
+
*/
|
|
88
|
+
export declare function takeSystemRootCert(params: {
|
|
89
|
+
url: string;
|
|
90
|
+
}): string;
|
|
67
91
|
/** Merge startup settings into one `options` key; adapter settings take precedence. */
|
|
68
92
|
export declare function applyStartupOptions(params: {
|
|
69
93
|
url: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,16 @@ export type BunAdapterOptions = {
|
|
|
22
22
|
readonly url: string;
|
|
23
23
|
/** Maximum pooled connections (default 10). */
|
|
24
24
|
readonly max?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Connections `adapter.connect()` (`db.$connect()`) opens at once before it
|
|
27
|
+
* returns (default 0: it waits for one), so a burst of parallel queries
|
|
28
|
+
* finds them warm instead of paying TCP + startup + authentication per
|
|
29
|
+
* connection — about three round trips each, more with TLS. bun:sql opens
|
|
30
|
+
* the pool's connections at its first use and closes none while idle (the
|
|
31
|
+
* adapter sets no idle timeout), so they stay open. Must be an integer from
|
|
32
|
+
* 0 through `max`, else the factory throws `VIBE_CONFIG`.
|
|
33
|
+
*/
|
|
34
|
+
readonly min?: number;
|
|
25
35
|
/**
|
|
26
36
|
* Pool-wide `statement_timeout` in milliseconds, sent as a startup parameter
|
|
27
37
|
* so every connection inherits it. A per-transaction `timeout` overrides it
|