@pgxsinkit/contracts 0.2.7 → 0.5.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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/dist/apply-strategy.d.ts +13 -1
  3. package/dist/apply-strategy.d.ts.map +1 -1
  4. package/dist/config.d.ts +256 -87
  5. package/dist/config.d.ts.map +1 -1
  6. package/dist/convergence-model.d.ts +2 -2
  7. package/dist/convergence-model.d.ts.map +1 -1
  8. package/dist/event-stream.d.ts +1 -1
  9. package/dist/fingerprint.d.ts +31 -11
  10. package/dist/fingerprint.d.ts.map +1 -1
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +480 -202
  14. package/dist/index.js.map +16 -14
  15. package/dist/predicate.d.ts +162 -0
  16. package/dist/predicate.d.ts.map +1 -0
  17. package/dist/projection.d.ts +2 -2
  18. package/dist/projection.d.ts.map +1 -1
  19. package/dist/registry-diff.d.ts.map +1 -1
  20. package/dist/registry-invariant.d.ts +26 -14
  21. package/dist/registry-invariant.d.ts.map +1 -1
  22. package/dist/registry.d.ts +74 -48
  23. package/dist/registry.d.ts.map +1 -1
  24. package/dist/sql-role-guard.d.ts +1 -1
  25. package/dist/stream-envelope.d.ts +84 -0
  26. package/dist/stream-envelope.d.ts.map +1 -0
  27. package/dist/supabase-rls.d.ts +32 -36
  28. package/dist/supabase-rls.d.ts.map +1 -1
  29. package/package.json +3 -2
  30. package/skills/registry-authoring/SKILL.md +185 -185
  31. package/skills/registry-authoring/references/storage-engine-and-build.md +29 -0
  32. package/src/apply-strategy.ts +23 -4
  33. package/src/config.ts +340 -116
  34. package/src/convergence-model.ts +2 -2
  35. package/src/event-stream.ts +1 -1
  36. package/src/fingerprint.ts +76 -14
  37. package/src/index.ts +2 -0
  38. package/src/predicate.ts +274 -0
  39. package/src/projection.ts +6 -2
  40. package/src/registry-diff.ts +15 -4
  41. package/src/registry-invariant.ts +34 -19
  42. package/src/registry.ts +324 -78
  43. package/src/sql-role-guard.ts +1 -1
  44. package/src/stream-envelope.ts +91 -0
  45. package/src/supabase-rls.ts +76 -74
@@ -1,7 +1,8 @@
1
1
  import type { SQL } from "drizzle-orm";
2
2
  import { getTableConfig, PgDialect } from "drizzle-orm/pg-core";
3
3
 
4
- import { buildRowFilterShape, type JwtClaims, type RowFilterShape } from "./config";
4
+ import { DENY_ALL_PREDICATE, type JwtClaims } from "./config";
5
+ import { isOrPredicate, type Predicate } from "./predicate";
5
6
  import { getSyncRegistryRowClasses, type SyncTableEntry, type SyncTableRegistry } from "./registry";
6
7
 
7
8
  /**
@@ -12,7 +13,7 @@ import { getSyncRegistryRowClasses, type SyncTableEntry, type SyncTableRegistry
12
13
  * It exists because a privacy/visibility invariant spanning many entries drifts in two structural ways that
13
14
  * per-entry review cannot catch:
14
15
  *
15
- * 1. **Read/write asymmetry** — the Electric row filter and the Postgres RLS policies are two engines, so one
16
+ * 1. **Read/write asymmetry** — the shape's row filter and the Postgres RLS policies are two engines, so one
16
17
  * can be tightened (or loosened) without the other. The assertion audits BOTH surfaces of every bound
17
18
  * entry in one predicate, so "readable but unwritable" (or the reverse) is a test failure, not a bug report.
18
19
  * 2. **Under-enumeration** — an invariant expressed over a hand-maintained list of tables silently stops
@@ -20,11 +21,23 @@ import { getSyncRegistryRowClasses, type SyncTableEntry, type SyncTableRegistry
20
21
  * inverts that: with a declared `rowClasses` vocabulary, a new entry cannot even be registered without
21
22
  * being classified, so it joins its class's invariants by construction.
22
23
  *
23
- * It audits RENDERED output, so it sees things the fingerprint structurally cannot (the `customWhere` BODY
24
- * is a closure — only its presence and `RowFilterSpec.revision` are hashed). The trade: it sees exactly the
25
- * claims fixtures you give it, and nothing else.
24
+ * It audits RESOLVED output, so it sees things the fingerprint structurally cannot (the `customPredicate`
25
+ * BODY is a closure — only its presence and `RowFilterSpec.revision` are hashed). The trade: it sees exactly
26
+ * the claims fixtures you give it, and nothing else.
26
27
  */
27
28
 
29
+ /**
30
+ * Whether a predicate admits no row at all — the read half of "this persona sees nothing".
31
+ *
32
+ * Recognises the {@link DENY_ALL_PREDICATE} singleton by reference AND any structurally empty `or`,
33
+ * because both are always-FALSE on the wire and an invariant is asking about the outcome, not about
34
+ * which spelling produced it.
35
+ */
36
+ export function deniesAllRows(predicate: Predicate | null): boolean {
37
+ if (predicate === DENY_ALL_PREDICATE) return true;
38
+ return predicate != null && isOrPredicate(predicate) && predicate.or.length === 0;
39
+ }
40
+
28
41
  /** One rendered write policy on a bound entry's Postgres table, as the invariant predicate sees it. */
29
42
  export interface RenderedPolicy {
30
43
  /** The policy name as declared (e.g. `widgets_select_owner_or_admin`). */
@@ -48,16 +61,19 @@ export interface RegistryInvariantCell {
48
61
  fixtureName: string;
49
62
  claims: JwtClaims;
50
63
  /**
51
- * The read filter this entry sends to Electric FOR THESE CLAIMS — the real read pipeline's output
52
- * (`buildRowFilterShape`, exactly what the proxy calls per shape request).
64
+ * The read predicate this entry resolves to FOR THESE CLAIMS — the real read pipeline's output, exactly
65
+ * what the control plane compiles into the shape at subscribe.
53
66
  *
54
67
  * `null` means **unfiltered**, and covers both ways that arises: the entry declares no shape/`rowFilter`
55
- * at all, or its `customWhere` returned `null` for these claims (the documented "bypass filtering, every
56
- * row is visible" answer — e.g. an admin persona). Both are the same statement about what the client
57
- * receives, which is what an invariant reasons about; distinguish them via `entry.shape?.rowFilter` if a
58
- * predicate genuinely needs to.
68
+ * at all, or its `customPredicate` returned `null` for these claims (the documented "bypass filtering,
69
+ * every row is visible" answer — e.g. an admin persona). Both are the same statement about what the
70
+ * client receives, which is what an invariant reasons about; distinguish them via
71
+ * `entry.shape?.rowFilter` if a predicate genuinely needs to.
72
+ *
73
+ * An AST, not SQL text: an invariant matches on structure ({@link deniesAllRows}, the `isXPredicate`
74
+ * guards) rather than on a rendered string that could drift on formatting alone.
59
75
  */
60
- renderedWhere: RowFilterShape | null;
76
+ readPredicate: Predicate | null;
61
77
  /** Every RLS policy attached to the entry's Postgres table, rendered to inline SQL text. */
62
78
  renderedPolicies: RenderedPolicy[];
63
79
  }
@@ -111,10 +127,9 @@ function renderPolicies(entry: SyncTableEntry): RenderedPolicy[] {
111
127
  }));
112
128
  }
113
129
 
114
- /** The read filter for these claims, through the real pipeline the proxy uses. */
115
- function renderWhere(entry: SyncTableEntry, claims: JwtClaims): RowFilterShape | null {
116
- const rowFilter = entry.shape?.rowFilter;
117
- return rowFilter == null ? null : buildRowFilterShape(rowFilter, claims);
130
+ /** The read predicate for these claims, through the real pipeline the control plane uses. */
131
+ function resolvePredicate(entry: SyncTableEntry, claims: JwtClaims): Predicate | null {
132
+ return entry.shape?.rowFilter?.customPredicate?.(claims) ?? null;
118
133
  }
119
134
 
120
135
  function boundEntries(
@@ -142,8 +157,8 @@ function boundEntries(
142
157
  * name: "private rows are never visible to an anonymous caller",
143
158
  * appliesTo: ["private"],
144
159
  * claimsFixtures: { anonymous: {}, member: { sub: "u-1" } },
145
- * holds: ({ fixtureName, renderedWhere }) =>
146
- * fixtureName !== "anonymous" || renderedWhere?.where === "false" || "anonymous read is not denied",
160
+ * holds: ({ fixtureName, readPredicate }) =>
161
+ * fixtureName !== "anonymous" || deniesAllRows(readPredicate) || "anonymous read is not denied",
147
162
  * });
148
163
  * ```
149
164
  *
@@ -199,7 +214,7 @@ export function assertRegistryInvariant(registry: SyncTableRegistry, spec: Regis
199
214
  rowClass: entry.rowClass,
200
215
  fixtureName,
201
216
  claims,
202
- renderedWhere: renderWhere(entry, claims),
217
+ readPredicate: resolvePredicate(entry, claims),
203
218
  renderedPolicies,
204
219
  });
205
220
  if (verdict === true) {