@palbase/backend 10.3.0 → 12.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.
@@ -1,5 +1,5 @@
1
1
  import { Tables, TableTypes } from './db/env.js';
2
- import { D as DBClient } from './endpoint-92kVepng.js';
2
+ import { D as DBClient, ba as TxPlanHandle, bk as TxTable, M as Materialized } from './endpoint-Ck4hER_7.js';
3
3
 
4
4
  /** On delete action for foreign key references. */
5
5
  type OnDeleteAction = 'cascade' | 'set null' | 'restrict' | 'no action';
@@ -536,29 +536,27 @@ interface TypedDB<S extends SchemaDef> {
536
536
  tables: {
537
537
  [K in keyof S["tables"]]: TypedTable<S["tables"][K]>;
538
538
  };
539
- transaction<T>(fn: (tx: TypedTx<S>) => Promise<T>): Promise<T>;
540
- }
541
- /** Transaction-scoped typed facade: same typed tables, no nested transaction. */
542
- interface TypedTx<S extends SchemaDef> {
543
- tables: {
544
- [K in keyof S["tables"]]: TypedTable<S["tables"][K]>;
545
- };
539
+ /** Run a transaction plan. See {@link EnvTypedDatabase.transaction}. */
540
+ transaction<T>(fn: (tx: TypedTx<S>) => T extends Promise<unknown> ? never : T): Promise<Materialized<T>>;
546
541
  }
542
+ /** The plan-building handle a `TypedDB<S>` transaction callback receives: the
543
+ * schema's tables, expressed as plan operations rather than awaited calls. */
544
+ type TypedTx<S extends SchemaDef> = TxPlanHandle<{
545
+ [K in keyof S["tables"]]: TxTable<RowShape<S["tables"][K]>, InsertShape<S["tables"][K]>>;
546
+ }>;
547
547
  /**
548
548
  * Wraps a raw `DBClient` with the type-safe `TypedDB<S>` facade derived from
549
- * the provided schema. No behavior change — all calls delegate to `raw` with
550
- * the table name as a plain string.
549
+ * the provided schema. No behavior change for the direct ops — all calls
550
+ * delegate to `raw` with the table name as a plain string.
551
551
  *
552
- * `buildTables` is the reusable factory that wraps any op-bearing client
553
- * (`TxClient` — the surface shared by `DBClient` and the transaction-scoped
554
- * client) into the typed tables map. It is used both for the top-level db
555
- * (wrapping `raw`) and inside `transaction`, where it wraps the raw `TxClient`
556
- * the runtime yields so the callback sees the same typed `.tables` API.
552
+ * `transaction` does NOT delegate to a per-op client: the callback describes a
553
+ * plan against a fresh {@link TxPlanBuilder}, and the whole plan travels in one
554
+ * `raw.txPlan` call. The schema is used only for its table NAMES; the values
555
+ * are typed by `S` at compile time and are plain strings at run time.
557
556
  *
558
- * `transaction` delegates straight to `raw.transaction`; the two narrow
559
- * `as TypedTx<S>` / `as TypedDB<S>` casts are single structural narrowings
560
- * from the dynamically-built tables object to the precise mapped type (TS
561
- * cannot infer through `Object.keys` iteration) — see module-level doc comment.
557
+ * The `as` casts are single structural narrowings from a dynamically-built
558
+ * object to the precise mapped type (TS cannot infer the mapped-type result
559
+ * through `Object.keys` iteration) — see the module-level doc comment.
562
560
  */
563
561
  declare function makeTypedDB<S extends SchemaDef>(schema: S, raw: DBClient): TypedDB<S>;
564
562
  /** A typed table accessor derived from one env `Tables` entry's flat shapes. */
@@ -578,34 +576,66 @@ interface EnvTypedTable<T extends TableTypes> {
578
576
  type EnvTables = {
579
577
  [K in keyof Tables]: EnvTypedTable<Tables[K]>;
580
578
  };
581
- /** Transaction-scoped typed facade for the env-augmented surface: same typed
582
- * tables, no nested transaction. */
583
- interface EnvTypedTx {
584
- tables: EnvTables;
585
- }
579
+ /** The project's tables as PLAN operations, keyed by the env `Tables`
580
+ * interface. The transaction twin of {@link EnvTables}. */
581
+ type TxTables = {
582
+ [K in keyof Tables]: TxTable<Tables[K]["row"], Tables[K]["insert"]>;
583
+ };
584
+ /**
585
+ * The handle a `Database.transaction(…)` callback receives.
586
+ *
587
+ * Tables only — no `query`, no `findById`, no `asService`. A read whose value
588
+ * the plan does not write belongs outside the transaction, where it costs one
589
+ * round trip and is an ordinary value you can branch on.
590
+ */
591
+ type TxPlan = TxPlanHandle<TxTables>;
586
592
  /**
587
593
  * The RLS-bypass sibling returned by `Database.asService()`. Same typed surface
588
594
  * as {@link EnvTypedDatabase} — `tables`, the raw string ops, and a typed
589
595
  * `transaction` — but it does NOT re-expose `asService` (no double-bypass).
590
596
  * Every op it performs runs as the `service_role` (BYPASSRLS).
591
597
  */
592
- interface EnvServiceDatabase extends Omit<DBClient, "transaction" | "asService"> {
598
+ interface EnvServiceDatabase extends Omit<DBClient, "txPlan" | "asService"> {
593
599
  tables: EnvTables;
594
- transaction<T>(fn: (tx: EnvTypedTx) => Promise<T>): Promise<T>;
600
+ transaction<T>(fn: (tx: TxPlan) => T extends Promise<unknown> ? never : T): Promise<Materialized<T>>;
595
601
  }
596
602
  /**
597
603
  * The typed-by-default Database surface: the raw string-keyed `DBClient` ops
598
604
  * PLUS a `tables` map typed against the project's generated `palbase-env.d.ts`,
599
- * a `transaction` whose callback receives the typed tables, and `asService()`
600
- * for the explicit RLS-bypass sibling.
605
+ * a `transaction` that runs a whole plan in one request, and `asService()` for
606
+ * the explicit RLS-bypass sibling.
601
607
  *
602
- * `transaction` is declared here (overriding `DBClient["transaction"]`) so the
603
- * `tx` the callback receives carries the typed `.tables` API. `asService` is
604
- * re-typed to return the typed {@link EnvServiceDatabase} sibling.
608
+ * The low-level `txPlan` op is deliberately NOT re-exposed here: `transaction`
609
+ * is the surface, and a hand-built plan would bypass the ref/guard machinery
610
+ * that makes one safe to write.
605
611
  */
606
- interface EnvTypedDatabase extends Omit<DBClient, "transaction" | "asService"> {
612
+ interface EnvTypedDatabase extends Omit<DBClient, "txPlan" | "asService"> {
607
613
  tables: EnvTables;
608
- transaction<T>(fn: (tx: EnvTypedTx) => Promise<T>): Promise<T>;
614
+ /**
615
+ * Run a transaction. The callback DESCRIBES the operations; the whole
616
+ * description travels in one request and the broker runs it inside a single
617
+ * transaction — committing when it finishes, rolling back on any failure.
618
+ *
619
+ * The callback is SYNCHRONOUS: nothing has run when it returns, so there is
620
+ * nothing to await. `async` on it and `await` inside it are compile errors.
621
+ * Values a later operation needs are {@link Ref}s, written straight into the
622
+ * next operation; values the CALLER needs are returned and substituted before
623
+ * this promise resolves.
624
+ *
625
+ * @example
626
+ * const { statementId } = await Database.transaction((tx) => {
627
+ * const st = tx.tables.statements
628
+ * .insert({ household_id: hid, file_sha256: sha, status: "reviewing" })
629
+ * .expectOne(new Internal("statement insert failed"));
630
+ *
631
+ * tx.tables.statement_lines.insertMany(
632
+ * lines.map((l) => ({ statement_id: st.id, category: resolveCategory(l) })),
633
+ * );
634
+ *
635
+ * return { statementId: st.id };
636
+ * });
637
+ */
638
+ transaction<T>(fn: (tx: TxPlan) => T extends Promise<unknown> ? never : T): Promise<Materialized<T>>;
609
639
  /**
610
640
  * Return a sibling that bypasses RLS by running as the `service_role`. Use
611
641
  * sparingly and explicitly — the default `Database.*` path is RLS-enforced.
@@ -617,4 +647,4 @@ interface EnvTypedDatabase extends Omit<DBClient, "transaction" | "asService"> {
617
647
  asService(): EnvServiceDatabase;
618
648
  }
619
649
 
620
- export { makeTypedDB as A, numeric as B, ColumnBuilder as C, policy as D, type EnvTypedDatabase as E, raw as F, text as G, timestamp as H, type InsertShape as I, uuid as J, type OnDeleteAction as O, PALBASE_EXTENSIONS as P, type RawConstraintDef as R, type SchemaDef as S, type TableDef as T, type ColumnDef as a, type ColumnMap as b, type ColumnType as c, EXTENSION_DEPENDENCIES as d, type EnvServiceDatabase as e, type EnvTables as f, type EnvTypedTable as g, type EnvTypedTx as h, type PalbaseExtension as i, PolicyBuilder as j, type PolicyCommand as k, type PolicyDef as l, type PolicyMode as m, type RowShape as n, type SchemaInput as o, type TableInput as p, type TypedDB as q, type TypedTable as r, type TypedTx as s, bigint as t, boolean as u, defineSchema as v, enumType as w, integer as x, isPalbaseExtension as y, jsonb as z };
650
+ export { jsonb as A, makeTypedDB as B, ColumnBuilder as C, numeric as D, type EnvTypedDatabase as E, policy as F, raw as G, text as H, type InsertShape as I, timestamp as J, uuid as K, type OnDeleteAction as O, PALBASE_EXTENSIONS as P, type RawConstraintDef as R, type SchemaDef as S, type TableDef as T, type ColumnDef as a, type ColumnMap as b, type ColumnType as c, EXTENSION_DEPENDENCIES as d, type EnvServiceDatabase as e, type EnvTables as f, type EnvTypedTable as g, type PalbaseExtension as h, PolicyBuilder as i, type PolicyCommand as j, type PolicyDef as k, type PolicyMode as l, type RowShape as m, type SchemaInput as n, type TableInput as o, type TxPlan as p, type TxTables as q, type TypedDB as r, type TypedTable as s, type TypedTx as t, bigint as u, boolean as v, defineSchema as w, enumType as x, integer as y, isPalbaseExtension as z };