nowity 1.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.
Files changed (192) hide show
  1. package/README.md +751 -0
  2. package/dist/api/contract.d.ts +186 -0
  3. package/dist/api/contract.d.ts.map +1 -0
  4. package/dist/api/contract.js +251 -0
  5. package/dist/api/errors.d.ts +50 -0
  6. package/dist/api/errors.d.ts.map +1 -0
  7. package/dist/api/errors.js +50 -0
  8. package/dist/api/index.d.ts +13 -0
  9. package/dist/api/index.d.ts.map +1 -0
  10. package/dist/api/index.js +32 -0
  11. package/dist/client/confirmed-store.d.ts +33 -0
  12. package/dist/client/confirmed-store.d.ts.map +1 -0
  13. package/dist/client/confirmed-store.js +123 -0
  14. package/dist/client/connection.d.ts +102 -0
  15. package/dist/client/connection.d.ts.map +1 -0
  16. package/dist/client/connection.js +285 -0
  17. package/dist/client/list.d.ts +102 -0
  18. package/dist/client/list.d.ts.map +1 -0
  19. package/dist/client/list.js +257 -0
  20. package/dist/client/live-query.d.ts +153 -0
  21. package/dist/client/live-query.d.ts.map +1 -0
  22. package/dist/client/live-query.js +244 -0
  23. package/dist/client/nowity-client.d.ts +188 -0
  24. package/dist/client/nowity-client.d.ts.map +1 -0
  25. package/dist/client/nowity-client.js +580 -0
  26. package/dist/client/null-transport.d.ts +16 -0
  27. package/dist/client/null-transport.d.ts.map +1 -0
  28. package/dist/client/null-transport.js +17 -0
  29. package/dist/client/overlay.d.ts +44 -0
  30. package/dist/client/overlay.d.ts.map +1 -0
  31. package/dist/client/overlay.js +90 -0
  32. package/dist/client/query-source.d.ts +78 -0
  33. package/dist/client/query-source.d.ts.map +1 -0
  34. package/dist/client/query-source.js +57 -0
  35. package/dist/client/ssr-source.d.ts +70 -0
  36. package/dist/client/ssr-source.d.ts.map +1 -0
  37. package/dist/client/ssr-source.js +134 -0
  38. package/dist/client/transport.d.ts +19 -0
  39. package/dist/client/transport.d.ts.map +1 -0
  40. package/dist/client/transport.js +2 -0
  41. package/dist/client/ws-transport.d.ts +30 -0
  42. package/dist/client/ws-transport.d.ts.map +1 -0
  43. package/dist/client/ws-transport.js +89 -0
  44. package/dist/client.d.ts +47 -0
  45. package/dist/client.d.ts.map +1 -0
  46. package/dist/client.js +80 -0
  47. package/dist/core/diff.d.ts +14 -0
  48. package/dist/core/diff.d.ts.map +1 -0
  49. package/dist/core/diff.js +56 -0
  50. package/dist/core/normalize.d.ts +16 -0
  51. package/dist/core/normalize.d.ts.map +1 -0
  52. package/dist/core/normalize.js +67 -0
  53. package/dist/core/protocol.d.ts +215 -0
  54. package/dist/core/protocol.d.ts.map +1 -0
  55. package/dist/core/protocol.js +14 -0
  56. package/dist/core/ssr-bridge.d.ts +69 -0
  57. package/dist/core/ssr-bridge.d.ts.map +1 -0
  58. package/dist/core/ssr-bridge.js +40 -0
  59. package/dist/core/types.d.ts +96 -0
  60. package/dist/core/types.d.ts.map +1 -0
  61. package/dist/core/types.js +17 -0
  62. package/dist/index.d.ts +47 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +98 -0
  65. package/dist/ironbean/client.d.ts +7 -0
  66. package/dist/ironbean/client.d.ts.map +1 -0
  67. package/dist/ironbean/client.js +38 -0
  68. package/dist/ironbean/server.d.ts +42 -0
  69. package/dist/ironbean/server.d.ts.map +1 -0
  70. package/dist/ironbean/server.js +62 -0
  71. package/dist/ironbean/tokens.d.ts +26 -0
  72. package/dist/ironbean/tokens.d.ts.map +1 -0
  73. package/dist/ironbean/tokens.js +23 -0
  74. package/dist/next/config.d.ts +7 -0
  75. package/dist/next/config.d.ts.map +1 -0
  76. package/dist/next/config.js +77 -0
  77. package/dist/next/live.d.ts +69 -0
  78. package/dist/next/live.d.ts.map +1 -0
  79. package/dist/next/live.js +70 -0
  80. package/dist/next/serve.d.ts +44 -0
  81. package/dist/next/serve.d.ts.map +1 -0
  82. package/dist/next/serve.js +107 -0
  83. package/dist/next/server.d.ts +62 -0
  84. package/dist/next/server.d.ts.map +1 -0
  85. package/dist/next/server.js +127 -0
  86. package/dist/react/connection-context.d.ts +4 -0
  87. package/dist/react/connection-context.d.ts.map +1 -0
  88. package/dist/react/connection-context.js +28 -0
  89. package/dist/react/grid-list.d.ts +34 -0
  90. package/dist/react/grid-list.d.ts.map +1 -0
  91. package/dist/react/grid-list.js +62 -0
  92. package/dist/react/hydrate.d.ts +22 -0
  93. package/dist/react/hydrate.d.ts.map +1 -0
  94. package/dist/react/hydrate.js +18 -0
  95. package/dist/react/index.d.ts +35 -0
  96. package/dist/react/index.d.ts.map +1 -0
  97. package/dist/react/index.js +51 -0
  98. package/dist/react/list.d.ts +31 -0
  99. package/dist/react/list.d.ts.map +1 -0
  100. package/dist/react/list.js +35 -0
  101. package/dist/react/prerendered.d.ts +33 -0
  102. package/dist/react/prerendered.d.ts.map +1 -0
  103. package/dist/react/prerendered.js +165 -0
  104. package/dist/react/provider.d.ts +64 -0
  105. package/dist/react/provider.d.ts.map +1 -0
  106. package/dist/react/provider.js +130 -0
  107. package/dist/react/refusals.d.ts +31 -0
  108. package/dist/react/refusals.d.ts.map +1 -0
  109. package/dist/react/refusals.js +51 -0
  110. package/dist/react/restricted.d.ts +76 -0
  111. package/dist/react/restricted.d.ts.map +1 -0
  112. package/dist/react/restricted.js +126 -0
  113. package/dist/react/use-list.d.ts +42 -0
  114. package/dist/react/use-list.d.ts.map +1 -0
  115. package/dist/react/use-list.js +56 -0
  116. package/dist/react/use-query.d.ts +66 -0
  117. package/dist/react/use-query.d.ts.map +1 -0
  118. package/dist/react/use-query.js +126 -0
  119. package/dist/react/virtual.d.ts +60 -0
  120. package/dist/react/virtual.d.ts.map +1 -0
  121. package/dist/react/virtual.js +131 -0
  122. package/dist/server/api/auth.d.ts +37 -0
  123. package/dist/server/api/auth.d.ts.map +1 -0
  124. package/dist/server/api/auth.js +54 -0
  125. package/dist/server/api/bridge.d.ts +28 -0
  126. package/dist/server/api/bridge.d.ts.map +1 -0
  127. package/dist/server/api/bridge.js +330 -0
  128. package/dist/server/api/collector.d.ts +25 -0
  129. package/dist/server/api/collector.d.ts.map +1 -0
  130. package/dist/server/api/collector.js +184 -0
  131. package/dist/server/api/configure.d.ts +31 -0
  132. package/dist/server/api/configure.d.ts.map +1 -0
  133. package/dist/server/api/configure.js +67 -0
  134. package/dist/server/api/execution.d.ts +77 -0
  135. package/dist/server/api/execution.d.ts.map +1 -0
  136. package/dist/server/api/execution.js +97 -0
  137. package/dist/server/api/list.d.ts +141 -0
  138. package/dist/server/api/list.d.ts.map +1 -0
  139. package/dist/server/api/list.js +281 -0
  140. package/dist/server/api/prerender.d.ts +32 -0
  141. package/dist/server/api/prerender.d.ts.map +1 -0
  142. package/dist/server/api/prerender.js +69 -0
  143. package/dist/server/api/read-keys.d.ts +128 -0
  144. package/dist/server/api/read-keys.d.ts.map +1 -0
  145. package/dist/server/api/read-keys.js +464 -0
  146. package/dist/server/api/read-set.d.ts +103 -0
  147. package/dist/server/api/read-set.d.ts.map +1 -0
  148. package/dist/server/api/read-set.js +210 -0
  149. package/dist/server/api/runtime.d.ts +36 -0
  150. package/dist/server/api/runtime.d.ts.map +1 -0
  151. package/dist/server/api/runtime.js +49 -0
  152. package/dist/server/api/tracked-client.d.ts +6 -0
  153. package/dist/server/api/tracked-client.d.ts.map +1 -0
  154. package/dist/server/api/tracked-client.js +56 -0
  155. package/dist/server/change-source/change-source.d.ts +41 -0
  156. package/dist/server/change-source/change-source.d.ts.map +1 -0
  157. package/dist/server/change-source/change-source.js +2 -0
  158. package/dist/server/change-source/outbox-change-source.d.ts +106 -0
  159. package/dist/server/change-source/outbox-change-source.d.ts.map +1 -0
  160. package/dist/server/change-source/outbox-change-source.js +237 -0
  161. package/dist/server/change-source/wal-change-source.d.ts +85 -0
  162. package/dist/server/change-source/wal-change-source.d.ts.map +1 -0
  163. package/dist/server/change-source/wal-change-source.js +193 -0
  164. package/dist/server/db.d.ts +51 -0
  165. package/dist/server/db.d.ts.map +1 -0
  166. package/dist/server/db.js +53 -0
  167. package/dist/server/evaluate.d.ts +39 -0
  168. package/dist/server/evaluate.d.ts.map +1 -0
  169. package/dist/server/evaluate.js +61 -0
  170. package/dist/server/invalidation-index.d.ts +64 -0
  171. package/dist/server/invalidation-index.d.ts.map +1 -0
  172. package/dist/server/invalidation-index.js +0 -0
  173. package/dist/server/mutator.d.ts +59 -0
  174. package/dist/server/mutator.d.ts.map +1 -0
  175. package/dist/server/mutator.js +77 -0
  176. package/dist/server/query.d.ts +113 -0
  177. package/dist/server/query.d.ts.map +1 -0
  178. package/dist/server/query.js +35 -0
  179. package/dist/server/setup.d.ts +30 -0
  180. package/dist/server/setup.d.ts.map +1 -0
  181. package/dist/server/setup.js +107 -0
  182. package/dist/server/sql/setup.sql +215 -0
  183. package/dist/server/ssr.d.ts +29 -0
  184. package/dist/server/ssr.d.ts.map +1 -0
  185. package/dist/server/ssr.js +43 -0
  186. package/dist/server/sync-engine.d.ts +256 -0
  187. package/dist/server/sync-engine.d.ts.map +1 -0
  188. package/dist/server/sync-engine.js +805 -0
  189. package/dist/server/ws-server.d.ts +56 -0
  190. package/dist/server/ws-server.d.ts.map +1 -0
  191. package/dist/server/ws-server.js +241 -0
  192. package/package.json +81 -0
@@ -0,0 +1,464 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KeyCollector = exports.ANY = void 0;
4
+ exports.keysOf = keysOf;
5
+ exports.mergeKeySets = mergeKeySets;
6
+ /**
7
+ * Which *rows* a query read, not just which tables.
8
+ *
9
+ * {@link ReadSetTracker} answers "did this query read `Document`?", and that is
10
+ * enough to be correct: a change to `Document` recomputes the query, the diff
11
+ * comes out empty, nobody notices. It is not enough to be fast. Every open game
12
+ * reads `Game`, so every move recomputed every game — the cost of one change
13
+ * grew with the number of subscriptions watching the same table, and the total
14
+ * cost with their product.
15
+ *
16
+ * A read key is the missing half: the equalities the query's own `where` already
17
+ * states. `findUnique({ where: { id: "42" } })` reads `Game`, and it reads the
18
+ * row whose `id` is `"42"`. A change to any other row cannot alter that result,
19
+ * so it does not need to touch the database to find that out.
20
+ *
21
+ * ## The rule everything here obeys
22
+ *
23
+ * A key set is sound when every change that *could* alter the result matches at
24
+ * least one key. Over-approximation costs a recomputation; under-approximation
25
+ * costs a subscription that silently stops updating, which is the failure mode
26
+ * this codebase spends work to avoid everywhere else. So every construct that is
27
+ * not understood — an operator added in a future Prisma release, a filter whose
28
+ * semantics are not plain value equality, a raw query — widens to {@link ANY},
29
+ * which means "any change to this table". That is exactly the behaviour this
30
+ * module replaced, so the worst case of not understanding something is the old
31
+ * cost, never a wrong answer.
32
+ *
33
+ * ## Why negation is not narrowed
34
+ *
35
+ * `NOT` inverts which rows matter, and the inversion is not expressible as a set
36
+ * of equalities: under `NOT: { status: "open" }` it is the rows that *stop*
37
+ * being open that change the result. Both images of a change are checked, so
38
+ * some negations would in fact be safe — but "some" is not a rule anyone can
39
+ * apply while reading a where-clause, and a nearly-right invalidation rule is
40
+ * worse than an honest one. Everything under a negation widens.
41
+ */
42
+ const types_1 = require("../../core/types");
43
+ /** Everything in the table is potentially read. */
44
+ exports.ANY = types_1.ANY_ROWS;
45
+ const ANY_EXPR = { kind: "any" };
46
+ /**
47
+ * How many alternatives an `in` may have before it is cheaper to watch the
48
+ * whole table.
49
+ *
50
+ * Each one becomes an index entry that has to be maintained on every
51
+ * re-evaluation, so a `where: { id: { in: [...200 ids] } }` would spend more on
52
+ * bookkeeping than the recomputation it saves.
53
+ */
54
+ const MAX_IN = 32;
55
+ /**
56
+ * Prisma scalar types whose values survive the round trip to a change event
57
+ * unchanged.
58
+ *
59
+ * The comparison happens between a value written in a `where` clause and a value
60
+ * that came back through `to_jsonb(NEW)` or wal2json, so the two have to agree
61
+ * on representation. They do for these three (and for enums, which arrive as
62
+ * their string label). They do not for `BigInt` (`to_jsonb` yields a JSON number
63
+ * for small values, the Prisma argument is a `bigint`), `Decimal` and `Float`
64
+ * (precision and formatting), `DateTime` (`to_jsonb` has no `Z`, `toISOString`
65
+ * does), `Bytes` or `Json`. A mismatch there would not be a slow subscription,
66
+ * it would be a subscription that never fires, so those types are never indexed.
67
+ */
68
+ const INDEXABLE_TYPES = new Set(["String", "Boolean", "Int"]);
69
+ /**
70
+ * Relation filters whose match can only narrow the related rows.
71
+ *
72
+ * `some` and `is` say "there exists a related row satisfying this", so a related
73
+ * row that satisfies it in neither image cannot decide the outcome. `every`,
74
+ * `none` and `isNot` are negations and are handled with the rest of them.
75
+ */
76
+ const POSITIVE_RELATION_FILTERS = new Set(["is", "some"]);
77
+ const NEGATIVE_RELATION_FILTERS = new Set(["isNot", "every", "none"]);
78
+ /**
79
+ * Prisma argument keys that hold a predicate over the model's own rows.
80
+ *
81
+ * `cursor` is deliberately absent: it selects a starting point in an ordering
82
+ * rather than a membership, so the rows it lets through are not the rows it
83
+ * names.
84
+ */
85
+ const PREDICATE_ARGS = ["where"];
86
+ /** Argument keys that read *other* models without constraining this one. */
87
+ const TRAVERSAL_ARGS = ["select", "include", "orderBy", "distinct", "by", "having", "data"];
88
+ /**
89
+ * Records what each Prisma call read, at row granularity where it can tell.
90
+ *
91
+ * One instance per execution. Calls accumulate: an endpoint that reads `Game`
92
+ * twice with different filters has read the union of both, so the table's keys
93
+ * are the union — and if either call could not be narrowed, the table is
94
+ * {@link ANY} regardless of how precise the other one was.
95
+ */
96
+ class KeyCollector {
97
+ constructor(deps) {
98
+ /** table → the model that names it, and every predicate placed on it. */
99
+ this.perTable = new Map();
100
+ this.tracker = deps.tracker;
101
+ }
102
+ /**
103
+ * Record one Prisma operation.
104
+ *
105
+ * `model` is the model the operation was called on; `args` its arguments as
106
+ * Prisma received them. An operation without a `where` reads the model
107
+ * unconstrained, which is the honest answer for `findMany({})`.
108
+ */
109
+ record(model, args) {
110
+ if (!model)
111
+ return;
112
+ if (!isPlainObject(args)) {
113
+ this.push(model, ANY_EXPR);
114
+ return;
115
+ }
116
+ let constrained = false;
117
+ for (const key of PREDICATE_ARGS) {
118
+ if (!(key in args))
119
+ continue;
120
+ constrained = true;
121
+ this.push(model, this.exprFor(model, args[key], false, 0));
122
+ }
123
+ if (!constrained)
124
+ this.push(model, ANY_EXPR);
125
+ // `select`, `include` and friends reach other models. They say nothing
126
+ // about which rows of *those* models are read unless they carry a
127
+ // `where` of their own, which `exprFor` picks up on the way through.
128
+ for (const key of TRAVERSAL_ARGS) {
129
+ if (key in args)
130
+ this.traverse(model, args[key], false, 0);
131
+ }
132
+ }
133
+ /** Everything read so far, collapsed to keys per table. */
134
+ result() {
135
+ const out = new Map();
136
+ for (const [table, entry] of this.perTable) {
137
+ out.set(table, keysOf(entry.exprs, this.tracker, entry.model));
138
+ }
139
+ return out;
140
+ }
141
+ /** Mark a model's table as read without knowing anything about which rows. */
142
+ widen(model) {
143
+ this.push(model, ANY_EXPR);
144
+ }
145
+ push(model, expr) {
146
+ const table = this.tracker.tableFor(model);
147
+ const entry = this.perTable.get(table);
148
+ if (entry)
149
+ entry.exprs.push(expr);
150
+ else
151
+ this.perTable.set(table, { model, exprs: [expr] });
152
+ }
153
+ /**
154
+ * The predicate one `where` places on `model`'s own columns.
155
+ *
156
+ * Nested models it mentions are pushed into the collector as it goes, since
157
+ * a filter on a relation is a read of that relation's table.
158
+ *
159
+ * `pessimistic` is set once anything above has inverted the meaning of the
160
+ * subtree; from then on nothing below may narrow anything.
161
+ */
162
+ exprFor(model, where, pessimistic, depth) {
163
+ if (depth > 24 || !isPlainObject(where))
164
+ return ANY_EXPR;
165
+ const parts = [];
166
+ for (const [key, value] of Object.entries(where)) {
167
+ if (value === undefined)
168
+ continue;
169
+ if (key === "AND") {
170
+ for (const item of asArray(value)) {
171
+ parts.push(this.exprFor(model, item, pessimistic, depth + 1));
172
+ }
173
+ continue;
174
+ }
175
+ if (key === "OR") {
176
+ const branches = asArray(value).map((item) => this.exprFor(model, item, pessimistic, depth + 1));
177
+ // An empty `OR: []` matches nothing in Prisma, but saying "any"
178
+ // about a query that returns no rows costs one recomputation and
179
+ // spares this module a special case whose only test would be
180
+ // itself.
181
+ parts.push(branches.length ? some(branches) : ANY_EXPR);
182
+ continue;
183
+ }
184
+ if (key === "NOT") {
185
+ for (const item of asArray(value))
186
+ this.traverse(model, item, true, depth + 1);
187
+ parts.push(ANY_EXPR);
188
+ continue;
189
+ }
190
+ const relation = this.tracker.relation(model, key);
191
+ if (relation) {
192
+ this.relationFilter(relation.target, value, pessimistic, depth + 1);
193
+ // A relation filter constrains the *related* rows, never this
194
+ // model's own columns, so it narrows nothing here.
195
+ parts.push(ANY_EXPR);
196
+ continue;
197
+ }
198
+ const type = this.tracker.fieldType(model, key);
199
+ if (type !== undefined) {
200
+ parts.push(pessimistic
201
+ ? ANY_EXPR
202
+ : this.scalarFilter(model, key, type, value, depth + 1));
203
+ continue;
204
+ }
205
+ // Not a scalar and not a relation: a compound unique
206
+ // (`where: { projectId_userId: { … } }`), or something this version
207
+ // does not know about. The former is an AND of equalities on this
208
+ // very model, and reads exactly like one.
209
+ parts.push(pessimistic ? ANY_EXPR : this.exprFor(model, value, pessimistic, depth + 1));
210
+ }
211
+ return all(parts);
212
+ }
213
+ /** A filter on one scalar column, as far as it is plain value equality. */
214
+ scalarFilter(model, field, type, value, depth) {
215
+ const column = this.tracker.columnFor(model, field);
216
+ if (!this.isIndexableType(model, field, type))
217
+ return ANY_EXPR;
218
+ if (!isPlainObject(value)) {
219
+ const key = asKeyValue(value);
220
+ return key === undefined ? ANY_EXPR : { kind: "eq", column, value: key };
221
+ }
222
+ const operators = Object.keys(value).filter((key) => value[key] !== undefined);
223
+ // `mode: "insensitive"` turns equality into collation-dependent
224
+ // equality, and an index keyed by the exact value would miss every row
225
+ // that differs only in case.
226
+ if (operators.includes("mode"))
227
+ return ANY_EXPR;
228
+ if (operators.length === 1 && operators[0] === "equals") {
229
+ const key = asKeyValue(value.equals);
230
+ return key === undefined ? ANY_EXPR : { kind: "eq", column, value: key };
231
+ }
232
+ if (operators.length === 1 && operators[0] === "in") {
233
+ const items = value.in;
234
+ if (!Array.isArray(items) || items.length === 0 || items.length > MAX_IN) {
235
+ return ANY_EXPR;
236
+ }
237
+ const branches = [];
238
+ for (const item of items) {
239
+ const key = asKeyValue(item);
240
+ if (key === undefined)
241
+ return ANY_EXPR;
242
+ branches.push({ kind: "eq", column, value: key });
243
+ }
244
+ return some(branches);
245
+ }
246
+ // `not`, `notIn`, `lt`, `lte`, `gt`, `gte`, `contains`, `startsWith`,
247
+ // `endsWith`, `search`, the list operators, the JSON operators, and
248
+ // whatever Prisma adds next. None of them is value equality; all of them
249
+ // widen. `depth` is carried so a future narrowing of one of these has a
250
+ // budget to spend.
251
+ void depth;
252
+ return ANY_EXPR;
253
+ }
254
+ /** Whether a column's values survive the trip through a change event intact. */
255
+ isIndexableType(model, field, type) {
256
+ if (INDEXABLE_TYPES.has(type))
257
+ return true;
258
+ // An enum arrives as its string label on both sides.
259
+ return this.tracker.isEnum(model, field);
260
+ }
261
+ relationFilter(target, value, pessimistic, depth) {
262
+ if (depth > 24) {
263
+ this.widen(target);
264
+ return;
265
+ }
266
+ if (isPlainObject(value)) {
267
+ const operators = Object.keys(value).filter((key) => value[key] !== undefined);
268
+ const positive = operators.filter((key) => POSITIVE_RELATION_FILTERS.has(key));
269
+ const negative = operators.some((key) => NEGATIVE_RELATION_FILTERS.has(key));
270
+ if (!negative && positive.length === operators.length && positive.length > 0) {
271
+ for (const key of positive) {
272
+ this.push(target, this.exprFor(target, value[key], pessimistic, depth + 1));
273
+ }
274
+ return;
275
+ }
276
+ if (!negative && positive.length === 0) {
277
+ // The to-one shorthand: `{ project: { id: p } }`.
278
+ this.push(target, this.exprFor(target, value, pessimistic, depth + 1));
279
+ return;
280
+ }
281
+ }
282
+ // A negation, a mixture, or `{ project: null }`. Read, not narrowed —
283
+ // and the table has to be marked read here, because the walk below only
284
+ // records the relations it finds *inside* the value.
285
+ this.widen(target);
286
+ this.traverse(target, value, true, depth + 1);
287
+ }
288
+ /**
289
+ * Walk a subtree for the tables it mentions, narrowing nothing.
290
+ *
291
+ * This is what `select`, `include`, `orderBy` and everything under a
292
+ * negation get: the tables they reach are read, and which of their rows is
293
+ * not something this walk claims to know. A nested `where` inside an
294
+ * `include` is still picked up — the rows it selects are the only ones that
295
+ * relation can contribute.
296
+ */
297
+ traverse(model, value, pessimistic, depth) {
298
+ if (depth > 24 || value === null || typeof value !== "object")
299
+ return;
300
+ if (Array.isArray(value)) {
301
+ for (const item of value)
302
+ this.traverse(model, item, pessimistic, depth + 1);
303
+ return;
304
+ }
305
+ for (const [key, child] of Object.entries(value)) {
306
+ const relation = this.tracker.relation(model, key);
307
+ if (!relation) {
308
+ this.traverse(model, child, pessimistic, depth + 1);
309
+ continue;
310
+ }
311
+ const target = relation.target;
312
+ // `include: { documents: { where: { status: "open" } } }` reads only
313
+ // the documents that filter admits — a row that is closed in both
314
+ // images was never in the result and cannot leave it. Under a
315
+ // negation the same clause proves nothing, so it is not used.
316
+ if (!pessimistic && isPlainObject(child) && isPlainObject(child.where)) {
317
+ this.push(target, this.exprFor(target, child.where, false, depth + 1));
318
+ }
319
+ else {
320
+ this.widen(target);
321
+ }
322
+ this.traverse(target, child, pessimistic, depth + 1);
323
+ }
324
+ }
325
+ }
326
+ exports.KeyCollector = KeyCollector;
327
+ /**
328
+ * The index entries one table's expressions collapse to.
329
+ *
330
+ * `AND` keeps only its narrowest conjunct, because a row that fails one of them
331
+ * fails the conjunction; `OR` has to keep all of its branches, because any of
332
+ * them alone can put a row in the result.
333
+ */
334
+ function keysOf(exprs, tracker, model) {
335
+ const keys = [];
336
+ const seen = new Set();
337
+ for (const expr of exprs) {
338
+ const terms = termsOf(expr, tracker, model);
339
+ if (terms === exports.ANY)
340
+ return exports.ANY;
341
+ for (const key of terms) {
342
+ const token = `${key.column}${String(key.value)}${typeof key.value}`;
343
+ if (seen.has(token))
344
+ continue;
345
+ seen.add(token);
346
+ keys.push(key);
347
+ }
348
+ }
349
+ // Nothing at all was read from the table. It cannot be watched by value,
350
+ // and it cannot be dropped either — the table is in the read set because
351
+ // something looked at it.
352
+ return keys.length ? keys : exports.ANY;
353
+ }
354
+ function termsOf(expr, tracker, model) {
355
+ switch (expr.kind) {
356
+ case "any":
357
+ return exports.ANY;
358
+ case "eq":
359
+ return [{ column: expr.column, value: expr.value }];
360
+ case "some": {
361
+ const out = [];
362
+ for (const part of expr.parts) {
363
+ const terms = termsOf(part, tracker, model);
364
+ if (terms === exports.ANY)
365
+ return exports.ANY;
366
+ out.push(...terms);
367
+ }
368
+ return out.length ? out : exports.ANY;
369
+ }
370
+ case "all": {
371
+ let best = null;
372
+ for (const part of expr.parts) {
373
+ const terms = termsOf(part, tracker, model);
374
+ if (terms === exports.ANY)
375
+ continue;
376
+ if (best === null || better(terms, best, tracker, model))
377
+ best = terms;
378
+ }
379
+ return best ?? exports.ANY;
380
+ }
381
+ }
382
+ }
383
+ /**
384
+ * Which of two candidate narrowings to keep for a conjunction.
385
+ *
386
+ * Fewer index entries first — one key beats an `in` of ten. A tie goes to the
387
+ * identity column, which is the one a database is guaranteed to have an index
388
+ * on and the one whose values are unique, so it matches the fewest changes.
389
+ */
390
+ function better(candidate, incumbent, tracker, model) {
391
+ if (candidate.length !== incumbent.length)
392
+ return candidate.length < incumbent.length;
393
+ if (!model)
394
+ return false;
395
+ const id = tracker.columnFor(model, tracker.idField(model));
396
+ const candidateIsId = candidate.every((key) => key.column === id);
397
+ const incumbentIsId = incumbent.every((key) => key.column === id);
398
+ return candidateIsId && !incumbentIsId;
399
+ }
400
+ function all(parts) {
401
+ const kept = parts.filter((part) => part.kind !== "any");
402
+ if (kept.length === 0)
403
+ return ANY_EXPR;
404
+ if (kept.length === 1)
405
+ return kept[0];
406
+ return { kind: "all", parts: kept };
407
+ }
408
+ function some(parts) {
409
+ if (parts.some((part) => part.kind === "any"))
410
+ return ANY_EXPR;
411
+ if (parts.length === 1)
412
+ return parts[0];
413
+ return { kind: "some", parts };
414
+ }
415
+ function asArray(value) {
416
+ return Array.isArray(value) ? value : [value];
417
+ }
418
+ /**
419
+ * A `where` value as an index key, or `undefined` when it is not one.
420
+ *
421
+ * Deliberately narrow: a `Date`, a `bigint`, a `Decimal` or a `Buffer` is a
422
+ * value whose textual form on the two sides of the comparison is not guaranteed
423
+ * to agree, and this is the place where that disagreement would turn into a
424
+ * subscription that never updates.
425
+ */
426
+ function asKeyValue(value) {
427
+ if (value === null)
428
+ return null;
429
+ if (typeof value === "string" || typeof value === "boolean")
430
+ return value;
431
+ if (typeof value === "number")
432
+ return Number.isSafeInteger(value) ? value : undefined;
433
+ return undefined;
434
+ }
435
+ function isPlainObject(value) {
436
+ return typeof value === "object" && value !== null && !Array.isArray(value);
437
+ }
438
+ /** Merge one read key set into another, widening on conflict. */
439
+ function mergeKeySets(into, from) {
440
+ for (const [table, keys] of from) {
441
+ const existing = into.get(table);
442
+ if (existing === undefined) {
443
+ into.set(table, keys);
444
+ continue;
445
+ }
446
+ if (existing === exports.ANY || keys === exports.ANY) {
447
+ into.set(table, exports.ANY);
448
+ continue;
449
+ }
450
+ into.set(table, dedupe([...existing, ...keys]));
451
+ }
452
+ }
453
+ function dedupe(keys) {
454
+ const seen = new Set();
455
+ const out = [];
456
+ for (const key of keys) {
457
+ const token = `${key.column}${String(key.value)}${typeof key.value}`;
458
+ if (seen.has(token))
459
+ continue;
460
+ seen.add(token);
461
+ out.push(key);
462
+ }
463
+ return out;
464
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * What a query read, derived rather than declared.
3
+ *
4
+ * The old `QueryDef.tables` was a hand-maintained list of invalidation
5
+ * dependencies, and a wrong one is invisible: the query simply stops updating.
6
+ * Here the dependency set is what the query *asked for*, recorded while it runs —
7
+ * the same relationship a MobX computed has with the observables it touched.
8
+ *
9
+ * The read set is taken from the Prisma arguments, not from the rows that came
10
+ * back, and that distinction is the whole reason this works for permissions. A
11
+ * user who is not a member reads `ProjectMember` and finds nothing; recording
12
+ * the *intent* still puts `ProjectMember` in the dependency set, so adding them
13
+ * to the project later invalidates the subscription. Recording results would
14
+ * not — there were none.
15
+ */
16
+ export interface DataModelField {
17
+ name: string;
18
+ kind: string;
19
+ type: string;
20
+ isList?: boolean;
21
+ isId?: boolean;
22
+ /**
23
+ * Column name when it differs from the field name (`@map`).
24
+ *
25
+ * Read keys compare a value written in a `where` clause against a value that
26
+ * came out of a change event, and the event is keyed by *column*. Without
27
+ * this, a mapped field would be indexed under a column the events never
28
+ * mention and the subscription would never fire.
29
+ */
30
+ dbName?: string | null;
31
+ /** Local scalar field(s) holding the relation, on the owning side. */
32
+ relationFromFields?: readonly string[] | null;
33
+ }
34
+ /** A relation as the client needs to know it to resolve it through the identity map. */
35
+ export interface RelationInfo {
36
+ field: string;
37
+ target: string;
38
+ isList: boolean;
39
+ /** Local foreign key, for a to-one relation that owns it. */
40
+ localKey?: string;
41
+ }
42
+ export interface DataModelModel {
43
+ name: string;
44
+ dbName?: string | null;
45
+ fields: readonly DataModelField[];
46
+ }
47
+ /** Structurally `Prisma.dmmf.datamodel`, which is deeply readonly. */
48
+ export interface DataModel {
49
+ models: readonly DataModelModel[];
50
+ }
51
+ /**
52
+ * The schema, as the Prisma client itself carries it.
53
+ *
54
+ * `Prisma.dmmf.datamodel` says the same thing, but requires the application to
55
+ * import and pass it. The client already knows, so asking it is one less thing
56
+ * to configure and one less thing to get wrong.
57
+ */
58
+ export declare function datamodelOf(prisma: any): DataModel;
59
+ export declare class ReadSetTracker {
60
+ /** model → (relation field → relation) */
61
+ private readonly relations;
62
+ /** model → (field → the kind a browser can compare it as) */
63
+ private readonly fieldKinds;
64
+ /** model → (scalar field → what it is and where it lives) */
65
+ private readonly scalars;
66
+ /** model → table name */
67
+ private readonly tables;
68
+ /** model → identity field */
69
+ private readonly idFields;
70
+ constructor(datamodel: DataModel);
71
+ /** How a browser may compare a field, or undefined if there is no such field. */
72
+ fieldKind(model: string, field: string): "number" | "date" | "boolean" | "text" | undefined;
73
+ /**
74
+ * Prisma's type name for a scalar field, or undefined when the field is not
75
+ * a scalar of this model — a relation, a list, or something the schema does
76
+ * not have at all.
77
+ */
78
+ fieldType(model: string, field: string): string | undefined;
79
+ /** True when the field is an enum, whose values travel as their string label. */
80
+ isEnum(model: string, field: string): boolean;
81
+ /** The column a field is stored in — the name a change event will use. */
82
+ columnFor(model: string, field: string): string;
83
+ relation(model: string, field: string): RelationInfo | undefined;
84
+ idField(model: string): string;
85
+ models(): string[];
86
+ relationsOf(model: string): RelationInfo[];
87
+ tableFor(model: string): string;
88
+ isWrite(operation: string): boolean;
89
+ isRaw(operation: string): boolean;
90
+ /**
91
+ * Record everything one Prisma call touches.
92
+ *
93
+ * Relation names in `where`, `select`, `include`, `orderBy` and friends are
94
+ * followed to their models, so a permission check expressed as a nested
95
+ * filter — `where: { project: { members: { some: { userId } } } }` — lands
96
+ * `Project` and `ProjectMember` in the set without anyone declaring them.
97
+ */
98
+ record(model: string | undefined, args: unknown, into: Set<string>): void;
99
+ private walk;
100
+ /** Entity name for the rows an operation returns, when it has one. */
101
+ entityFor(model: string | undefined): string | undefined;
102
+ }
103
+ //# sourceMappingURL=read-set.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-set.d.ts","sourceRoot":"","sources":["../../../src/server/api/read-set.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,MAAM,WAAW,cAAc;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,sEAAsE;IACtE,kBAAkB,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;CACjD;AAED,wFAAwF;AACxF,MAAM,WAAW,YAAY;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,MAAM,EAAE,SAAS,cAAc,EAAE,CAAC;CACrC;AAED,sEAAsE;AACtE,MAAM,WAAW,SAAS;IACtB,MAAM,EAAE,SAAS,cAAc,EAAE,CAAC;CACrC;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,GAAG,GAAG,SAAS,CAclD;AAiDD,qBAAa,cAAc;IACvB,0CAA0C;IAC1C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAgD;IAC1E,6DAA6D;IAC7D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA0E;IACrG,6DAA6D;IAC7D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8C;IACtE,yBAAyB;IACzB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA6B;IACpD,6BAA6B;IAC7B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA6B;gBAE1C,SAAS,EAAE,SAAS;IAuChC,iFAAiF;IACjF,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS;IAI3F;;;;OAIG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAI3D,iFAAiF;IACjF,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO;IAI7C,0EAA0E;IAC1E,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IAI/C,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAIhE,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI9B,MAAM,IAAI,MAAM,EAAE;IAIlB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,EAAE;IAI1C,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI/B,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAInC,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAIjC;;;;;;;OAOG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI;IAMzE,OAAO,CAAC,IAAI;IAyBZ,sEAAsE;IACtE,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS;CAG3D"}