@ultimat3/db 31.7.0 → 31.9.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.
@@ -22,3 +22,12 @@ export function setTransactionSettings(next: SettingsProvider | undefined): void
22
22
  export function transactionSettings(): LocalSettings | undefined {
23
23
  return provider?.();
24
24
  }
25
+
26
+ /**
27
+ * Whether a provider is installed at all, whatever it answers now. `copy-in.ts` asks: Postgres
28
+ * refuses `COPY FROM` on a table under row-level security (`0A000`) with or without a tenant in
29
+ * scope, so the question is "is row security armed", never "is there a tenant right now".
30
+ */
31
+ export function transactionSettingsInstalled(): boolean {
32
+ return provider !== undefined;
33
+ }
@@ -5,6 +5,7 @@
5
5
 
6
6
  import { assert, finiteCount, nanoid } from '@ultimat3/core';
7
7
  import { baseClient, type DbClient, type DbConnection, isReservable } from './client';
8
+ import type { CopyInClient } from './copy-in';
8
9
  import { DbError, serializationExhausted } from './errors';
9
10
  import { setLocal } from './local-settings';
10
11
  import { createTurnQueue, type TurnQueue } from './pglite-turns';
@@ -145,7 +146,7 @@ function makeTx(
145
146
  refuseAbandoned(completing);
146
147
  return watched(abort, send());
147
148
  };
148
- return {
149
+ const tx: DbTx = {
149
150
  id,
150
151
  origin,
151
152
  query: <T>(fragment: SqlFragment) => guarded(() => connection.query<T>(fragment)),
@@ -161,6 +162,15 @@ function makeTx(
161
162
  else if (outcome.value === 'open' || outcome.value === 'ending') commits.push(effect);
162
163
  },
163
164
  };
165
+ // Only when the connection copies (`postgresClient`'s pin): a COPY on it is this transaction's,
166
+ // guarded like every statement. Absent, `canCopyIn(tx)` answers false and callers insert.
167
+ const pin = connection as DbClient & Partial<CopyInClient>;
168
+ if (typeof pin.copyIn !== 'function') return tx;
169
+ const copyIn = pin.copyIn.bind(pin);
170
+ return Object.assign(tx, {
171
+ copyIn: (target: SqlFragment, rows: Iterable<readonly unknown[]>) =>
172
+ guarded(() => copyIn(target, rows)),
173
+ });
164
174
  }
165
175
 
166
176
  const isAborted = (error: unknown): boolean =>