@ultimat3/db 31.29.0 → 31.31.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
@@ -33,6 +33,7 @@ await withTransaction(async (tx) => {
33
33
  | `client.copyIn(target, rows)` / `canCopyIn(client)` | `As of 2026-11-11`: `COPY … FROM STDIN` on `postgresClient()`, its pins, a `DbTx` on one and the replica router (to the primary) → [Bulk load: `copyIn`](#bulk-load-copyin) |
34
34
  | `db()` / `baseClient()` / `setDbClient()` | the ambient client; `db()` returns the open tx if any |
35
35
  | `dbClientInstalled()` | `As of 30.0`: whether `setDbClient` installed a client that is still in place. Never builds the lazy default. `@ultimat3/entity`'s `testDriver()` asks it on every repository call |
36
+ | `ambientClient()` | `As of 31.30`: the client `db()` would use now — installed, or the lazy default once built — else `undefined`. Never builds. A caller that displaces it with `setDbClient` reads it to restore or close it |
36
37
  | `dbExecutor(client = db)` | the client as `@ultimat3/core`'s `PgExecutor` (`query(text, values)`) — what a jobs, idempotency, notify or MCP confirmation store takes. The client is resolved per statement: the default reaches the boot's client even when declared at module scope, and joins an open `withTransaction`; `() => pool` pins one pool whatever transaction is open |
37
38
  | `DbTx.origin` | `As of 2026-08`: the client the transaction was **opened on** — `options.client` or `baseClient()`, never the reservation it runs statements through. `@ultimat3/entity` compares a pinned repository's client against it, so a pinned repo joins its own shard's transaction instead of being refused |
38
39
  | `withTransaction()` / `currentTx()` | transaction scope; `currentTx()` is the outbox seam. `{ retry: n }` (`As of 2026-08`) re-runs `fn` from the top on a `40001`/`40P01` and on nothing else — default 0, so `fn` must be idempotent before you ask for it. Each re-run **waits first**, `As of 2026-08-23`: exponential from 10ms, capped at 500ms, full jitter (`@ultimat3/core`'s `backoffDelay`). A budget of 0 waits not at all. **A transaction the server aborted is reported as one**, `As of 2026-10-02` → [Transactions that end badly](#transactions-that-end-badly) |
@@ -40,6 +41,7 @@ await withTransaction(async (tx) => {
40
41
  | `liveTxConnection()` | the connection of the transaction still **open** on this async context, or `undefined`. `currentTx()` keeps answering a finished scope's handle to a promise chain the body forgot to await; this is the one that says whether a statement sent now is really inside a transaction. `@ultimat3/action` reads it before binding an idempotency settlement to a commit |
41
42
  | `sqlState()` / `isRetryableState()` / `SQLSTATE` | `As of 2026-08`: the SQLSTATE a driver error carries, and the closed table from it to a code. `Bun.SQL` puts it on `errno`; PGlite puts it on `code`; **one** reader answers for both |
42
43
  | `migrate()` / `rollback()` / `readLedger()` | the `x_migrations` ledger |
44
+ | `ledgerAheadOfBuild()` / `ROLLBACK_ALLOW_DESTRUCTIVE_ENV` | `As of 31.30`: the ledger split when it is exactly a rollback onto a newer build's schema (every unknown row newer than the newest shipped, nothing pending), else `undefined` and `auditLedger` refuses it; a destructive row ahead throws `X_MIGRATION_ROLLBACK_DESTRUCTIVE` unless named in the variable. `migrate()`'s own first step, exported so `x db status` judges a ledger as `x db migrate` will |
43
45
  | `statementsOf()` | `As of 2026-08`: a SQL script → the statements a driver sends one at a time. One send is one statement, so `migrate()` splits with this — a `;` inside a literal, an identifier, a dollar-quoted body or a comment is data |
44
46
  | `withReplicaReads()` | `As of 2026-08-24`: the scope inside which a plain read may be served by a replica — until it writes, after which every read in it is the primary's. No scope open, nothing routes |
45
47
  | `poolerFromEnv()` / `POOLER_ENV` / `PoolerMode` / `POOLER_WAIT_MS` / `statementBound()` / `lostStatementBound()` / `StatementBound` / `poolerSessionRequired()` | `As of 2026-11-03`: a connection pooler in the path (`pooler.ts`), and the two bounds as the session reads them. [Pool sizing by role](#pool-sizing-by-role) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/db",
3
- "version": "31.29.0",
3
+ "version": "31.31.0",
4
4
  "description": "Postgres access, transactions, migrations and drift detection",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -45,7 +45,7 @@
45
45
  "test": "bun test"
46
46
  },
47
47
  "dependencies": {
48
- "@ultimat3/core": "31.29.0"
48
+ "@ultimat3/core": "31.31.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@electric-sql/pglite": ">=0.5.0"
package/src/client.ts CHANGED
@@ -360,6 +360,15 @@ export function dbClientInstalled(): boolean {
360
360
  return installed;
361
361
  }
362
362
 
363
+ /**
364
+ * The client `db()` would use now — installed, or the lazy default once something built it — and
365
+ * `undefined` when neither exists. Never builds: a caller that displaces it with `setDbClient`
366
+ * reads it here to close or restore it, where `baseClient()` would open a pool to answer.
367
+ */
368
+ export function ambientClient(): DbClient | undefined {
369
+ return ambient;
370
+ }
371
+
363
372
  /**
364
373
  * The pool, ignoring any open transaction. `withTransaction` must not re-enter `db()`.
365
374
  *
package/src/index.ts CHANGED
@@ -24,6 +24,7 @@ export type {
24
24
  ReservableClient,
25
25
  } from './client';
26
26
  export {
27
+ ambientClient,
27
28
  baseClient,
28
29
  db,
29
30
  dbClientInstalled,
@@ -177,6 +178,8 @@ export {
177
178
  rollback,
178
179
  runningAppVersion,
179
180
  } from './migrate';
181
+ export type { LedgerAhead, LedgerAheadOptions } from './migrate-rollback';
182
+ export { ledgerAheadOfBuild, ROLLBACK_ALLOW_DESTRUCTIVE_ENV } from './migrate-rollback';
180
183
  export {
181
184
  migrationConcurrentPartitioned,
182
185
  migrationDestructive,
package/src/migrate.ts CHANGED
@@ -358,8 +358,8 @@ export async function rollback(options: RollbackOptions): Promise<readonly strin
358
358
  if (migration === undefined) {
359
359
  throw migrationConflict(
360
360
  `migration "${row.id}" is in the ledger but not in this build, so its down SQL is unknown`,
361
- // Same reason as `auditLedger`'s: `x db status` does not exist. The `down` SQL only
362
- // exists in the build that shipped it, so the fix is the read that names that build.
361
+ // Same reason as `auditLedger`'s: psql works where the CLI is absent. The `down` SQL
362
+ // only exists in the build that shipped it, so the fix reads which build that was.
363
363
  `psql "$DATABASE_URL" -c "select id, app_version from ${renderFixShellArg(LEDGER_TABLE, '<ledger table>')} ` +
364
364
  `order by id desc limit 5" # deploy the build that shipped the migration ` +
365
365
  `whose id is base64 ${Buffer.from(row.id, 'utf8').toString('base64')}, ` +
@@ -128,10 +128,9 @@ export function auditLedger(
128
128
  throw migrationConflict(
129
129
  `the ledger records migration "${first.id}" applied by app version "${first.app_version}" ` +
130
130
  `but this build is "${appVersion}" and does not ship it`,
131
- // `x db status` has never existed — the subcommands are gen, migrate, reset, studio, branch
132
- // and backfill — and this is one of the two errors most likely to fire during a real deploy.
133
- // A `fix:` is copied and run verbatim, so it names the ledger read that works anywhere psql
134
- // does, and the one edit that resolves the disagreement.
131
+ // One of the two errors most likely to fire during a real deploy, often on a box with no
132
+ // CLI. A `fix:` is copied and run verbatim, so it names the ledger read that works anywhere
133
+ // psql does (`x db status --json` reads the same rows), and the one edit that resolves it.
135
134
  conflictFix(first),
136
135
  );
137
136
  }