@vibeorm/adapter-bun 6.0.2 → 6.2.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 CHANGED
@@ -43,7 +43,7 @@ await db.$disconnect();
43
43
 
44
44
  `planCacheMode` governs every plan the server caches on a connection: named statements, and also the plans of foreign-key checks, triggers and PL/pgSQL functions. An unnamed statement (the default mode) is planned for its own values whatever the setting says, so `force_custom_plan` there only made PostgreSQL replan the foreign-key check of every inserted row: `createMany` of 1000 rows took 20.3 ms with it and 16.2 ms without (adapter-pg, which sends no setting, 17.1 ms). With named statements the default stays `force_custom_plan`, so a named statement never switches to a generic plan after five executions.
45
45
 
46
- `planCacheMode` and `statementTimeout` merge into one `options` startup parameter, with adapter settings taking precedence over matching URL settings (or `PGOPTIONS` settings, for a URL without `options`). 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
+ `planCacheMode` and `statementTimeout` merge into one `options` startup parameter, with adapter settings taking precedence over matching URL settings (or `PGOPTIONS` settings, for a URL without `options`). With neither set (and no `options` in the URL or `PGOPTIONS`), no `options` parameter is sent: PgBouncer before 1.20 refuses one. bun:sql sends `DateStyle = ISO, MDY` as a startup parameter of its own, so timestamps arrive in the ISO layout VibeORM reads (see `docs/dialect-notes.md` "PostgreSQL `DateStyle` and timestamp text"). 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".)
47
47
 
48
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:sql 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
49
 
@@ -1,11 +1,2 @@
1
- /**
2
- * Refuse a Bun whose bun:sql the adapter does not support (see
3
- * {@link MINIMUM_BUN_VERSION}), at construction — before any statement can
4
- * meet one of that driver's known data bugs.
5
- *
6
- * @throws VibeError `VIBE_CONFIG`.
7
- */
8
- export declare function refuseUnsupportedBun(params: {
9
- version: string;
10
- }): void;
1
+ export {};
11
2
  //# sourceMappingURL=bun-version.d.ts.map
package/dist/config.d.ts CHANGED
@@ -1,21 +1,2 @@
1
- /**
2
- * Oldest Bun whose bun:sql the adapter accepts. 1.4.3 and older read a BC
3
- * `timestamptz`/`date` as `Invalid Date` and a 5-digit-year `timestamp`
4
- * without a time zone in host-local time, and with named statements
5
- * (`preparedStatements: true`) never close a statement on the server: every
6
- * SQL text, and every combination of NULL parameters of one text, stays
7
- * prepared on the backend for the connection's lifetime. 1.3.14 also hangs
8
- * after `COPY … TO STDOUT`. 1.4.4 passes the same probes.
9
- */
10
- export declare const MINIMUM_BUN_VERSION: string;
11
- /**
12
- * PostgreSQL's limit on bound parameters in one statement (the Bind message
13
- * carries a 16-bit count).
14
- */
15
- export declare const MAX_BIND_PARAMETERS: number;
16
- /** Migration-only driver instance; never force-close the application's pool. */
17
- export declare const SESSION_CONNECTION: Readonly<{
18
- max: number;
19
- closeGraceSeconds: number;
20
- }>;
1
+ export {};
21
2
  //# sourceMappingURL=config.d.ts.map
@@ -29,22 +29,6 @@
29
29
  * its own values whatever this says.
30
30
  */
31
31
  export type PlanCacheMode = "auto" | "force_custom_plan" | "force_generic_plan";
32
- /**
33
- * The `plan_cache_mode` a connection gets when the caller sets none
34
- * (performance audit round 2, B-01).
35
- *
36
- * - Unnamed statements (the default): `auto`. Every ORM statement is re-parsed
37
- * and gets a custom plan anyway, so `force_custom_plan` changed only the SPI
38
- * plans — it replanned the foreign-key check of every inserted row (measured:
39
- * `createMany` of 1000 rows 20.3 → 16.2 ms, the same as adapter-pg, which
40
- * sends no setting).
41
- * - Named statements (`preparedStatements: true`): `force_custom_plan`, so a
42
- * named statement never switches to a generic plan after five executions
43
- * (reads with skewed filters were not measured under `auto`).
44
- */
45
- export declare function defaultPlanCacheMode(params: {
46
- preparedStatements: boolean;
47
- }): PlanCacheMode;
48
32
  /**
49
33
  * Build the `options` startup string, or `undefined` when nothing needs setting.
50
34
  *
@@ -55,38 +39,6 @@ export declare function buildStartupOptions(params: {
55
39
  planCacheMode: PlanCacheMode;
56
40
  statementTimeout?: number;
57
41
  }): string | undefined;
58
- /**
59
- * Remove libpq's client-side `connect_timeout` (whole seconds) from a URL and
60
- * return its value, so the URL bun:sql forwards to the server no longer carries
61
- * it. `0` or a negative value means "no limit" in libpq and yields
62
- * `undefined`. A URL bun cannot parse is returned unchanged.
63
- *
64
- * @throws VibeError `VIBE_VALIDATION` when the value is not an integer.
65
- */
66
- export declare function takeConnectTimeout(params: {
67
- url: string;
68
- }): {
69
- url: string;
70
- seconds?: number;
71
- };
72
- /**
73
- * Rewrite a URL carrying libpq's `sslrootcert=system` ("trust the system CA
74
- * roots") into `sslmode=verify-full` without it, so bun:sql verifies the
75
- * server certificate's chain and host name against its default trust store
76
- * (Bun's bundled roots plus `NODE_EXTRA_CA_CERTS`) on every supported Bun, and
77
- * nothing reaches the server as a startup parameter. Any other URL is
78
- * returned unchanged.
79
- *
80
- * Follows libpq: with `system`, an omitted mode means `verify-full` and every
81
- * weaker one is an error — also bun:sql's own spellings (`ssl=false`,
82
- * `tls=require`, `ssl-mode=prefer`, …).
83
- *
84
- * @throws VibeError `VIBE_CONFIG` (`meta.reason: "sslrootcert-system-weak-sslmode"`)
85
- * for a weaker mode. Neither the message nor `meta` carries the URL.
86
- */
87
- export declare function takeSystemRootCert(params: {
88
- url: string;
89
- }): string;
90
42
  /**
91
43
  * Merge startup settings into one `options` key; adapter settings take
92
44
  * precedence. The URL's own `options` come first. A URL without that key
package/dist/driver.d.ts CHANGED
@@ -1,11 +1,2 @@
1
- import { SQL } from "bun";
2
- /**
3
- * Internal constructor boundary; keeps the driver double out of the public
4
- * adapter API. Always the URL form: bun:sql's options-only form fills the
5
- * whole connection from the environment.
6
- */
7
- export declare function createBunSql(params: {
8
- url: string;
9
- options: Record<string, unknown>;
10
- }): SQL;
1
+ export {};
11
2
  //# sourceMappingURL=driver.d.ts.map
package/dist/errors.d.ts CHANGED
@@ -33,6 +33,4 @@ export declare function bunErrorSqlstate(params: {
33
33
  export declare function mapBunDriverError(params: {
34
34
  error: unknown;
35
35
  }): VibeError;
36
- /** Run a driver call, mapping anything it throws onto a `VibeError`. */
37
- export declare function runMapped<T>(fn: () => Promise<T>): Promise<T>;
38
36
  //# sourceMappingURL=errors.d.ts.map
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ import {
34
34
  createDeferredControl,
35
35
  observeRowChanges,
36
36
  readRowChanges,
37
+ holdRowCapture,
37
38
  statementRowChanges,
38
39
  createAdapterLifecycleGate,
39
40
  runPinnedSession,
@@ -145,6 +146,7 @@ function applyStartupOptions(params) {
145
146
  }
146
147
 
147
148
  // packages/adapter-bun/src/errors.ts
149
+ import { POSTGRES_CONNECTION_SQLSTATES, socketConnectionFact } from "@vibeorm/runtime/adapter-kit";
148
150
  import { VibeError as VibeError3 } from "@vibeorm/schema";
149
151
  var BUN_SQLSTATE_ERROR_CODES = Object.freeze({
150
152
  "23P01": "VIBE_EXCLUSION_VIOLATION",
@@ -173,6 +175,14 @@ var BUN_SQLSTATE_REASONS = Object.freeze({
173
175
  "40001": "serialization-failure",
174
176
  "25006": "read-only-transaction"
175
177
  });
178
+ var BUN_CONNECTION_FAILURE_CODES = Object.freeze({
179
+ ERR_POSTGRES_CONNECTION_REFUSED: { reason: "connection-refused", mayHaveReachedServer: false },
180
+ ERR_POSTGRES_CONNECTION_TIMEOUT: { reason: "connection-timeout", mayHaveReachedServer: false },
181
+ ERR_POSTGRES_CONNECTION_FAILED: { reason: "connection-refused", mayHaveReachedServer: true },
182
+ ERR_POSTGRES_CONNECTION_CLOSED: { reason: "connection-lost", mayHaveReachedServer: true },
183
+ ERR_POSTGRES_IDLE_TIMEOUT: { reason: "connection-lost", mayHaveReachedServer: true },
184
+ ERR_POSTGRES_LIFETIME_TIMEOUT: { reason: "connection-lost", mayHaveReachedServer: true }
185
+ });
176
186
  var SQLSTATE_PATTERN = /^(?=[A-Z]*\d)[0-9A-Z]{5}$/;
177
187
  function readStringField(params) {
178
188
  const value = params.source[params.key];
@@ -215,6 +225,9 @@ function mapBunDriverError(params) {
215
225
  const driverCode = readStringField({ source, key: "code" });
216
226
  if (driverCode !== undefined && driverCode !== sqlstate)
217
227
  meta.driverCode = driverCode;
228
+ const connection = reason !== undefined ? undefined : sqlstate !== undefined ? POSTGRES_CONNECTION_SQLSTATES[sqlstate] : driverCode !== undefined && Object.hasOwn(BUN_CONNECTION_FAILURE_CODES, driverCode) ? BUN_CONNECTION_FAILURE_CODES[driverCode] : socketConnectionFact({ error });
229
+ if (connection !== undefined)
230
+ Object.assign(meta, connection);
218
231
  const message = readStringField({ source, key: "message" }) ?? (typeof error === "string" ? error : "unknown bun:sql driver error");
219
232
  return new VibeError3({ code, message, meta, cause: error });
220
233
  }
@@ -228,6 +241,7 @@ async function runMapped(fn) {
228
241
 
229
242
  // packages/adapter-bun/src/params.ts
230
243
  import { Buffer } from "buffer";
244
+ import { postgresExtendedDateText } from "@vibeorm/runtime/adapter-kit";
231
245
  import { VibeError as VibeError4 } from "@vibeorm/schema";
232
246
  function quoteElement(value) {
233
247
  return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
@@ -240,7 +254,7 @@ function toPgArrayLiteral(values) {
240
254
  return String(value);
241
255
  }
242
256
  if (value instanceof Date)
243
- return quoteElement(value.toISOString());
257
+ return quoteElement(postgresExtendedDateText({ value }) ?? value.toISOString());
244
258
  if (value instanceof Uint8Array) {
245
259
  return quoteElement(`\\x${Buffer.from(value.buffer, value.byteOffset, value.byteLength).toString("hex")}`);
246
260
  }
@@ -261,12 +275,10 @@ function toPgArrayLiteral(values) {
261
275
  }
262
276
  function serializeDate(params) {
263
277
  const { value } = params;
264
- if (Number.isNaN(value.getTime()))
265
- return value;
266
- const year = value.getUTCFullYear();
267
- if (year < 1 || year > 9999)
268
- return value;
269
- return value.toISOString();
278
+ const extended = postgresExtendedDateText({ value });
279
+ if (extended !== null)
280
+ return extended;
281
+ return Number.isNaN(value.getTime()) ? value : value.toISOString();
270
282
  }
271
283
  function serializeScalar(value) {
272
284
  if (typeof value === "number" || typeof value === "bigint" || typeof value === "boolean")
@@ -1600,10 +1612,13 @@ function bunAdapter(options) {
1600
1612
  return control.ordered.open(count).map(laneHandle);
1601
1613
  }
1602
1614
  },
1603
- ...parentMode === "pool" ? {} : { rowChanges: () => {
1604
- assertHandle();
1605
- return readRowChanges({ scope });
1606
- } },
1615
+ ...parentMode === "pool" ? {} : {
1616
+ rowChanges: () => {
1617
+ assertHandle();
1618
+ return readRowChanges({ scope });
1619
+ },
1620
+ holdRowCapture: (capture) => holdRowCapture({ scope, capture })
1621
+ },
1607
1622
  ...control === undefined || params.commits !== true || !control.commits || scope.depth !== 0 || final !== undefined ? {} : {
1608
1623
  withCommit: (options) => createAdapter({
1609
1624
  source,
@@ -1902,4 +1917,4 @@ export {
1902
1917
  validateTransactionDeadline2 as validateTransactionDeadline
1903
1918
  };
1904
1919
 
1905
- //# debugId=EC0EB3CE1B302FCB64756E2164756E21
1920
+ //# debugId=6DA4669CD493BB7A64756E2164756E21