nowity 1.6.0 → 1.8.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 (61) hide show
  1. package/README.md +78 -6
  2. package/dist/api/contract.d.ts +11 -0
  3. package/dist/api/contract.d.ts.map +1 -1
  4. package/dist/api/contract.js +5 -1
  5. package/dist/client/confirmed-store.d.ts +2 -11
  6. package/dist/client/confirmed-store.d.ts.map +1 -1
  7. package/dist/client/confirmed-store.js +5 -0
  8. package/dist/client/nowity-client.d.ts +85 -0
  9. package/dist/client/nowity-client.d.ts.map +1 -1
  10. package/dist/client/nowity-client.js +213 -14
  11. package/dist/client/overlay.d.ts +2 -0
  12. package/dist/client/overlay.d.ts.map +1 -1
  13. package/dist/client/overlay.js +4 -0
  14. package/dist/client.d.ts +2 -1
  15. package/dist/client.d.ts.map +1 -1
  16. package/dist/client.js +2 -1
  17. package/dist/core/derived.d.ts +96 -0
  18. package/dist/core/derived.d.ts.map +1 -0
  19. package/dist/core/derived.js +127 -0
  20. package/dist/core/protocol.d.ts +23 -1
  21. package/dist/core/protocol.d.ts.map +1 -1
  22. package/dist/core/types.d.ts +9 -1
  23. package/dist/core/types.d.ts.map +1 -1
  24. package/dist/index.d.ts +2 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +3 -1
  27. package/dist/server/api/bridge.d.ts +8 -0
  28. package/dist/server/api/bridge.d.ts.map +1 -1
  29. package/dist/server/api/bridge.js +20 -1
  30. package/dist/server/api/configure.d.ts.map +1 -1
  31. package/dist/server/api/configure.js +1 -0
  32. package/dist/server/api/derived.d.ts +143 -0
  33. package/dist/server/api/derived.d.ts.map +1 -0
  34. package/dist/server/api/derived.js +377 -0
  35. package/dist/server/api/execution.d.ts +6 -0
  36. package/dist/server/api/execution.d.ts.map +1 -1
  37. package/dist/server/api/execution.js +1 -0
  38. package/dist/server/api/read-keys.d.ts +20 -0
  39. package/dist/server/api/read-keys.d.ts.map +1 -1
  40. package/dist/server/api/read-keys.js +103 -0
  41. package/dist/server/api/runtime.d.ts +26 -0
  42. package/dist/server/api/runtime.d.ts.map +1 -1
  43. package/dist/server/api/runtime.js +3 -1
  44. package/dist/server/api/tracked-client.d.ts.map +1 -1
  45. package/dist/server/api/tracked-client.js +9 -0
  46. package/dist/server/change-source/change-source.d.ts +4 -2
  47. package/dist/server/change-source/change-source.d.ts.map +1 -1
  48. package/dist/server/change-source/outbox-change-source.d.ts +35 -0
  49. package/dist/server/change-source/outbox-change-source.d.ts.map +1 -1
  50. package/dist/server/change-source/outbox-change-source.js +23 -1
  51. package/dist/server/evaluate.d.ts +2 -0
  52. package/dist/server/evaluate.d.ts.map +1 -1
  53. package/dist/server/evaluate.js +1 -0
  54. package/dist/server/mutator.d.ts +2 -0
  55. package/dist/server/mutator.d.ts.map +1 -1
  56. package/dist/server/query.d.ts +6 -0
  57. package/dist/server/query.d.ts.map +1 -1
  58. package/dist/server/sync-engine.d.ts +27 -1
  59. package/dist/server/sync-engine.d.ts.map +1 -1
  60. package/dist/server/sync-engine.js +81 -3
  61. package/package.json +1 -1
@@ -0,0 +1,377 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DerivedMutators = void 0;
4
+ exports.newRecording = newRecording;
5
+ exports.shapeWrites = shapeWrites;
6
+ function newRecording() {
7
+ return { writes: [], raw: false };
8
+ }
9
+ /**
10
+ * How many agreeing calls a fingerprint needs before anyone is sent it.
11
+ *
12
+ * One call cannot tell a constant from a value it happened to compute — `turn:
13
+ * 2` looks the same either way — nor an argument from a computed value that
14
+ * happened to equal it once (`increment: 1` on a call whose argument was 1).
15
+ * A second call narrows both. It can still be a coincidence; every later call
16
+ * is checked again, and the first that disagrees demotes the mutation.
17
+ */
18
+ const MIN_OBSERVATIONS = 2;
19
+ class DerivedMutators {
20
+ constructor(tracker, options = {}) {
21
+ this.tracker = tracker;
22
+ this.learned = new Map();
23
+ this.listeners = new Set();
24
+ /** Bumped whenever what any query should get may have changed. */
25
+ this.version = 0;
26
+ this.enabled = options.enabled ?? false;
27
+ this.warn = options.onWarning ?? (() => undefined);
28
+ }
29
+ get isEnabled() {
30
+ return this.enabled;
31
+ }
32
+ /** Told after every successful call of a mutation. */
33
+ observe(name, args, recording, session) {
34
+ if (!this.enabled)
35
+ return;
36
+ const current = this.learned.get(name);
37
+ if (current?.status.state === "fallback")
38
+ return;
39
+ const shaped = recording.raw
40
+ ? { ok: false, reason: "raw SQL write" }
41
+ : shapeWrites(recording.writes, this.tracker);
42
+ if (!shaped.ok)
43
+ return this.demote(name, shaped.reason);
44
+ if (shaped.writes.length === 0)
45
+ return this.demote(name, "writes no model row");
46
+ const shape = JSON.stringify(shaped.writes.map((w) => [w.entity, w.op, Object.keys(w.set).sort(), Object.keys(w.inc).sort()]));
47
+ const observed = shaped.writes.map((w) => observeWrite(w, args, session.userId));
48
+ if (!current) {
49
+ this.store(name, { status: statusOf(observed, 1), shape, writes: observed });
50
+ return;
51
+ }
52
+ if (current.shape !== shape)
53
+ return this.demote(name, "writes differ between calls");
54
+ const merged = current.writes.map((w, i) => mergeWrite(w, observed[i]));
55
+ const observations = current.status.observations + 1;
56
+ const status = statusOf(merged, observations);
57
+ this.store(name, { status, shape, writes: merged });
58
+ }
59
+ status(name) {
60
+ return this.learned.get(name)?.status;
61
+ }
62
+ /** Every mutation seen so far and where it stands — for an operator, or a test. */
63
+ report() {
64
+ const out = {};
65
+ for (const [name, learned] of [...this.learned].sort(([a], [b]) => a.localeCompare(b))) {
66
+ out[name] = learned.status;
67
+ }
68
+ return out;
69
+ }
70
+ onChange(listener) {
71
+ this.listeners.add(listener);
72
+ return () => this.listeners.delete(listener);
73
+ }
74
+ /**
75
+ * The fingerprints one live query gets.
76
+ *
77
+ * Per subscription — per query *and* arguments — because two instances of
78
+ * one query can read different rows under different filters, and only the
79
+ * intersection with what this instance read says whether the write can be
80
+ * shown here without asking the server.
81
+ */
82
+ forSubscription(sub, session) {
83
+ if (!this.enabled || sub.tables === null || sub.keys === null || sub.filterColumns === null)
84
+ return [];
85
+ const out = [];
86
+ for (const [name, learned] of this.learned) {
87
+ if (learned.status.state !== "derived")
88
+ continue;
89
+ const writes = this.affecting(learned.writes, sub, session);
90
+ if (writes)
91
+ out.push({ name, writes });
92
+ }
93
+ return out.sort((a, b) => a.name.localeCompare(b.name));
94
+ }
95
+ /**
96
+ * The writes of one fingerprint as this subscription gets them, or null if
97
+ * the fingerprint is not for it.
98
+ *
99
+ * All writes or none: a mutator applied halfway would show a state the
100
+ * server never produces.
101
+ */
102
+ affecting(writes, sub, session) {
103
+ let touches = false;
104
+ const out = [];
105
+ for (const write of writes) {
106
+ const table = this.tracker.tableFor(write.entity);
107
+ if (sub.entities.includes(write.entity) && sub.tables.has(table))
108
+ touches = true;
109
+ // Checked for every table the query read, held or not: a write to
110
+ // `User.name` moves documents in and out of a list filtered by
111
+ // `author: { name }` without the list holding a single user.
112
+ if (sub.tables.has(table) && !this.membershipStable(write, table, sub))
113
+ return null;
114
+ const resolved = resolveWrite(write, session);
115
+ if (!resolved)
116
+ return null;
117
+ out.push(resolved);
118
+ }
119
+ return touches ? out : null;
120
+ }
121
+ /**
122
+ * Whether writing these fields can never move a row in, out of or along
123
+ * the query.
124
+ *
125
+ * Every column the query's `where` or `orderBy` mentioned, by any
126
+ * operator, through any relation: a write to one of them could make a row
127
+ * leave, arrive or move, and where to is the server's to say.
128
+ */
129
+ membershipStable(write, table, sub) {
130
+ const filtered = new Set(sub.filterColumns.get(table) ?? []);
131
+ for (const field of Object.keys(sub.filter ?? {}))
132
+ filtered.add(this.tracker.columnFor(write.entity, field));
133
+ for (const field of [...Object.keys(write.set), ...Object.keys(write.inc)]) {
134
+ if (filtered.has(this.tracker.columnFor(write.entity, field)))
135
+ return false;
136
+ }
137
+ return true;
138
+ }
139
+ demote(name, reason) {
140
+ const current = this.learned.get(name);
141
+ if (current?.status.state === "fallback")
142
+ return;
143
+ if (current?.status.state === "derived") {
144
+ this.warn(`Mutation "${name}" no longer has a derived client mutator: ${reason}.`);
145
+ }
146
+ this.store(name, { status: { state: "fallback", reason } });
147
+ }
148
+ store(name, next) {
149
+ const before = this.learned.get(name);
150
+ this.learned.set(name, next);
151
+ // What a browser would receive, not the candidates behind it: a call
152
+ // that only narrows them changes nothing anyone was sent.
153
+ const published = (learned) => learned?.status.state === "derived" ? JSON.stringify(learned.writes.map(signature)) : null;
154
+ if (published(before) === published(next))
155
+ return;
156
+ this.version += 1;
157
+ for (const listener of this.listeners)
158
+ listener();
159
+ }
160
+ }
161
+ exports.DerivedMutators = DerivedMutators;
162
+ // ---------------------------------------------------------------- shaping
163
+ const SET_OPERATORS = new Set(["set"]);
164
+ const INC_OPERATORS = { increment: 1, decrement: -1 };
165
+ /** Reduce what the tracking client saw to rows written by id — or say why not. */
166
+ function shapeWrites(writes, tracker) {
167
+ const out = [];
168
+ for (const write of writes) {
169
+ if (write.operation !== "update" && write.operation !== "delete") {
170
+ return { ok: false, reason: `${write.operation} on ${write.model}` };
171
+ }
172
+ const args = (write.args ?? {});
173
+ const id = idOfWhere(write.model, args.where, tracker);
174
+ if (id === undefined)
175
+ return { ok: false, reason: `${write.operation} on ${write.model} not by id` };
176
+ if (write.operation === "delete") {
177
+ out.push({ entity: write.model, op: "delete", id, set: {}, inc: {} });
178
+ continue;
179
+ }
180
+ const set = {};
181
+ const inc = {};
182
+ const data = args.data;
183
+ if (data === null || typeof data !== "object") {
184
+ return { ok: false, reason: `update on ${write.model} without data` };
185
+ }
186
+ for (const [field, raw] of Object.entries(data)) {
187
+ if (raw === undefined)
188
+ continue;
189
+ const type = tracker.fieldType(write.model, field);
190
+ if (type === undefined)
191
+ return { ok: false, reason: `nested write ${write.model}.${field}` };
192
+ if (type === "Json")
193
+ return { ok: false, reason: `JSON column ${write.model}.${field}` };
194
+ if (field === tracker.idField(write.model)) {
195
+ return { ok: false, reason: `changes the id of ${write.model}` };
196
+ }
197
+ const operation = operatorOf(raw);
198
+ if (operation === null) {
199
+ const value = scalar(raw);
200
+ if (value === undefined)
201
+ return { ok: false, reason: `value of ${write.model}.${field}` };
202
+ set[field] = value;
203
+ }
204
+ else if (SET_OPERATORS.has(operation.op)) {
205
+ const value = scalar(operation.value);
206
+ if (value === undefined)
207
+ return { ok: false, reason: `value of ${write.model}.${field}` };
208
+ set[field] = value;
209
+ }
210
+ else if (operation.op in INC_OPERATORS && typeof operation.value === "number") {
211
+ inc[field] = operation.value * INC_OPERATORS[operation.op];
212
+ }
213
+ else {
214
+ return { ok: false, reason: `${operation.op} on ${write.model}.${field}` };
215
+ }
216
+ }
217
+ out.push({ entity: write.model, op: "update", id, set, inc });
218
+ }
219
+ return { ok: true, writes: out };
220
+ }
221
+ /** `{ id: v }` — and nothing else — names one row. */
222
+ function idOfWhere(model, where, tracker) {
223
+ if (where === null || typeof where !== "object")
224
+ return undefined;
225
+ const entries = Object.entries(where).filter(([, v]) => v !== undefined);
226
+ if (entries.length !== 1)
227
+ return undefined;
228
+ const [field, value] = entries[0];
229
+ if (field !== tracker.idField(model))
230
+ return undefined;
231
+ const id = scalar(value);
232
+ return id === null ? undefined : id;
233
+ }
234
+ function operatorOf(raw) {
235
+ if (raw === null || typeof raw !== "object" || raw instanceof Date || Array.isArray(raw))
236
+ return null;
237
+ const entries = Object.entries(raw);
238
+ if (entries.length !== 1)
239
+ return { op: "?", value: undefined };
240
+ return { op: entries[0][0], value: entries[0][1] };
241
+ }
242
+ /** A value a browser row can hold as is, or undefined. */
243
+ function scalar(value) {
244
+ if (value === null)
245
+ return null;
246
+ if (value instanceof Date)
247
+ return value.toISOString();
248
+ if (typeof value === "string" || typeof value === "boolean")
249
+ return value;
250
+ if (typeof value === "number" && Number.isFinite(value))
251
+ return value;
252
+ return undefined;
253
+ }
254
+ // ---------------------------------------------------------------- learning
255
+ function observeWrite(write, args, userId) {
256
+ const value = (v) => observeValue(v, args, userId);
257
+ return {
258
+ entity: write.entity,
259
+ op: write.op,
260
+ id: value(write.id),
261
+ set: Object.fromEntries(Object.entries(write.set).map(([f, v]) => [f, value(v)])),
262
+ inc: Object.fromEntries(Object.entries(write.inc).map(([f, v]) => [f, value(v)])),
263
+ };
264
+ }
265
+ function observeValue(value, args, userId) {
266
+ const v = value;
267
+ return {
268
+ paths: pathsOf(args, v),
269
+ session: userId !== null && v === userId,
270
+ constant: v,
271
+ constantHolds: true,
272
+ };
273
+ }
274
+ /** Every path in the arguments whose leaf is this value. */
275
+ function pathsOf(args, value) {
276
+ const out = [];
277
+ const walk = (node, path, depth) => {
278
+ if (depth > 8)
279
+ return;
280
+ if (node instanceof Date) {
281
+ if (node.toISOString() === value)
282
+ out.push(path);
283
+ return;
284
+ }
285
+ if (node !== null && typeof node === "object") {
286
+ if (Array.isArray(node))
287
+ node.forEach((child, i) => walk(child, [...path, i], depth + 1));
288
+ else
289
+ for (const [key, child] of Object.entries(node))
290
+ walk(child, [...path, key], depth + 1);
291
+ return;
292
+ }
293
+ if (node === value && node !== undefined)
294
+ out.push(path);
295
+ };
296
+ walk(args, [], 0);
297
+ return out;
298
+ }
299
+ function mergeWrite(a, b) {
300
+ const fields = (x, y) => Object.fromEntries(Object.keys(x).map((f) => [f, mergeValue(x[f], y[f])]));
301
+ return { ...a, id: mergeValue(a.id, b.id), set: fields(a.set, b.set), inc: fields(a.inc, b.inc) };
302
+ }
303
+ function mergeValue(a, b) {
304
+ const keys = new Set(b.paths.map((p) => JSON.stringify(p)));
305
+ return {
306
+ paths: a.paths.filter((p) => keys.has(JSON.stringify(p))),
307
+ session: a.session && b.session,
308
+ constant: a.constant,
309
+ constantHolds: a.constantHolds && a.constant === b.constant,
310
+ };
311
+ }
312
+ function kindOf(value) {
313
+ if (value.paths.length)
314
+ return "arg";
315
+ if (value.session)
316
+ return "session";
317
+ if (value.constantHolds)
318
+ return "constant";
319
+ return "broken";
320
+ }
321
+ function valuesOf(write) {
322
+ return [write.id, ...Object.values(write.set), ...Object.values(write.inc)];
323
+ }
324
+ function statusOf(writes, observations) {
325
+ const kinds = writes.flatMap(valuesOf).map(kindOf);
326
+ if (kinds.includes("broken")) {
327
+ return { state: "fallback", reason: "a written value is not an argument, the caller or a constant" };
328
+ }
329
+ if (observations < MIN_OBSERVATIONS)
330
+ return { state: "learning", observations };
331
+ return { state: "derived", observations };
332
+ }
333
+ function signature(write) {
334
+ const sig = (state) => {
335
+ const kind = kindOf(state);
336
+ return kind === "arg" ? [kind, state.paths[0]] : kind === "constant" ? [kind, state.constant] : [kind];
337
+ };
338
+ const fields = (states) => Object.entries(states).map(([field, state]) => [field, sig(state)]);
339
+ return [write.entity, write.op, sig(write.id), fields(write.set), fields(write.inc)];
340
+ }
341
+ /** The template as one session gets it: the caller's id filled in. */
342
+ function resolveWrite(write, session) {
343
+ const value = (state) => {
344
+ switch (kindOf(state)) {
345
+ case "arg":
346
+ return { arg: state.paths[0] };
347
+ case "session":
348
+ return session.userId === null ? null : { const: session.userId };
349
+ case "constant":
350
+ return { const: state.constant };
351
+ default:
352
+ return null;
353
+ }
354
+ };
355
+ const fields = (states) => {
356
+ const out = {};
357
+ for (const [field, state] of Object.entries(states)) {
358
+ const resolved = value(state);
359
+ if (!resolved)
360
+ return null;
361
+ out[field] = resolved;
362
+ }
363
+ return out;
364
+ };
365
+ const id = value(write.id);
366
+ const set = fields(write.set);
367
+ const inc = fields(write.inc);
368
+ if (!id || !set || !inc)
369
+ return null;
370
+ return {
371
+ entity: write.entity,
372
+ op: write.op,
373
+ id,
374
+ ...(Object.keys(set).length ? { set } : {}),
375
+ ...(Object.keys(inc).length ? { inc } : {}),
376
+ };
377
+ }
@@ -1,4 +1,5 @@
1
1
  import type { SessionInfo } from "../query";
2
+ import type { WriteRecording } from "./derived";
2
3
  import type { ListExecution } from "./list";
3
4
  import type { KeyCollector } from "./read-keys";
4
5
  export interface Execution {
@@ -41,6 +42,11 @@ export interface Execution {
41
42
  lastModel?: string;
42
43
  /** Present for mutations only. */
43
44
  mutationId?: number;
45
+ /**
46
+ * What a mutation wrote, model call by model call — the material its
47
+ * derived client mutator is learned from. Present for mutations only.
48
+ */
49
+ writes?: WriteRecording;
44
50
  /**
45
51
  * Present while a `@list` endpoint runs for one window.
46
52
  *
@@ -1 +1 @@
1
- {"version":3,"file":"execution.d.ts","sourceRoot":"","sources":["../../../src/server/api/execution.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,MAAM,WAAW,SAAS;IACtB,IAAI,EAAE,OAAO,GAAG,UAAU,CAAC;IAC3B,OAAO,EAAE,WAAW,CAAC;IACrB,6EAA6E;IAC7E,MAAM,EAAE,GAAG,CAAC;IACZ,iEAAiE;IACjE,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACnB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,YAAY,CAAC;IACpB;;;;;;;;OAQG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;OAGG;IACH,SAAS,EAAE,OAAO,CAAC;IACnB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,kCAAkC;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,aAAa,CAAC;CACxB;AAID,wBAAgB,cAAc,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAOxF;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAStF;AAED,wBAAgB,gBAAgB,IAAI,SAAS,CAS5C;AAED,wBAAgB,aAAa,IAAI,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CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAOxF;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAUtF;AAED,wBAAgB,gBAAgB,IAAI,SAAS,CAS5C;AAED,wBAAgB,aAAa,IAAI,SAAS,GAAG,SAAS,CAErD;AAED;;;;;GAKG;AACH,qBACa,OAAO;IAChB,IAAI,QAAQ,IAAI,MAAM,CAIrB;IAED,IAAI,MAAM,IAAI,MAAM,GAAG,IAAI,CAE1B;IAED,qDAAqD;IACrD,IAAI,IAAI,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAElC;IAED,uEAAuE;IACvE,aAAa,IAAI,MAAM;CAK1B;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,KAAK,CAAC,CAcnD"}
@@ -51,6 +51,7 @@ function runUntracked(execution, fn) {
51
51
  keys: undefined,
52
52
  untracked: false,
53
53
  list: undefined,
54
+ writes: undefined,
54
55
  };
55
56
  return storage.run(shadow, async () => await fn());
56
57
  }
@@ -74,6 +74,13 @@ export interface KeyCollectorDeps {
74
74
  export declare class KeyCollector {
75
75
  /** table → the model that names it, and every predicate placed on it. */
76
76
  private readonly perTable;
77
+ /**
78
+ * table → every column a `where` or an `orderBy` mentioned, whatever the
79
+ * operator. Keys keep only the narrowest conjunct, which is all
80
+ * invalidation needs; deciding whether a write can move a row in or out of
81
+ * the result (or along it) needs all of them.
82
+ */
83
+ private readonly predicateColumns;
77
84
  private readonly tracker;
78
85
  constructor(deps: KeyCollectorDeps);
79
86
  /**
@@ -86,6 +93,19 @@ export declare class KeyCollector {
86
93
  record(model: string | undefined, args: unknown): void;
87
94
  /** Everything read so far, collapsed to keys per table. */
88
95
  result(): ReadKeySet;
96
+ /** Columns the reads filtered or ordered by, per table. See {@link predicateColumns}. */
97
+ columns(): Map<string, Set<string>>;
98
+ private addColumn;
99
+ /** Every scalar a `where` names, on its own model and through relations. */
100
+ private columnsOf;
101
+ /**
102
+ * The columns one call's arguments filter, order, page or group by — at
103
+ * the top and inside every `include`/`select` of a relation, because a
104
+ * nested `where` decides which related rows are in the result too.
105
+ */
106
+ private columnsOfArgs;
107
+ /** `orderBy` keys, including an order by a related row's field. */
108
+ private orderColumns;
89
109
  /**
90
110
  * Record a read of a table that is not a Prisma model — one of Nowity's
91
111
  * own — whose rows were selected by `column IN values`. `null` for values
@@ -1 +1 @@
1
- {"version":3,"file":"read-keys.d.ts","sourceRoot":"","sources":["../../../src/server/api/read-keys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,OAAO,EAAY,KAAK,QAAQ,EAAE,KAAK,OAAO,EAAE,KAAK,UAAU,EAAY,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,YAAY,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC;AAEzD,mDAAmD;AACnD,eAAO,MAAM,GAAG,OAAW,CAAC;AAE5B;;;;;;;GAOG;AACH,MAAM,MAAM,OAAO,GACb;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,GACf;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,QAAQ,CAAA;CAAE,GAC/C;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,OAAO,EAAE,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,EAAE,CAAA;CAAE,CAAC;AAmDzC,MAAM,WA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,mEAAmE;IACnE,OAAO,CAAC,YAAY;IAepB;;;;OAIG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,QAAQ,EAAE,GAAG,IAAI,GAAG,IAAI;IAQnF,8EAA8E;IAC9E,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAI1B,OAAO,CAAC,IAAI;IAOZ;;;;;;;;OAQG;IACH,OAAO,CAAC,OAAO;IAgEf,2EAA2E;IAC3E,OAAO,CAAC,YAAY;IAkDpB,gFAAgF;IAChF,OAAO,CAAC,eAAe;IAMvB,OAAO,CAAC,cAAc;IAqCtB;;;;;;;;OAQG;IACH,OAAO,CAAC,QAAQ;CA4BnB;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,cAAc,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAmB3F;AAsFD,iEAAiE;AACjE,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI,CAarE"}
@@ -97,6 +97,13 @@ class KeyCollector {
97
97
  constructor(deps) {
98
98
  /** table → the model that names it, and every predicate placed on it. */
99
99
  this.perTable = new Map();
100
+ /**
101
+ * table → every column a `where` or an `orderBy` mentioned, whatever the
102
+ * operator. Keys keep only the narrowest conjunct, which is all
103
+ * invalidation needs; deciding whether a write can move a row in or out of
104
+ * the result (or along it) needs all of them.
105
+ */
106
+ this.predicateColumns = new Map();
100
107
  this.tracker = deps.tracker;
101
108
  }
102
109
  /**
@@ -113,6 +120,7 @@ class KeyCollector {
113
120
  this.push(model, ANY_EXPR);
114
121
  return;
115
122
  }
123
+ this.columnsOfArgs(model, args, 0);
116
124
  let constrained = false;
117
125
  for (const key of PREDICATE_ARGS) {
118
126
  if (!(key in args))
@@ -138,6 +146,101 @@ class KeyCollector {
138
146
  }
139
147
  return out;
140
148
  }
149
+ /** Columns the reads filtered or ordered by, per table. See {@link predicateColumns}. */
150
+ columns() {
151
+ return new Map([...this.predicateColumns].map(([table, columns]) => [table, new Set(columns)]));
152
+ }
153
+ addColumn(model, field) {
154
+ const table = this.tracker.tableFor(model);
155
+ let columns = this.predicateColumns.get(table);
156
+ if (!columns)
157
+ this.predicateColumns.set(table, (columns = new Set()));
158
+ columns.add(this.tracker.columnFor(model, field));
159
+ }
160
+ /** Every scalar a `where` names, on its own model and through relations. */
161
+ columnsOf(model, where, depth) {
162
+ if (depth > 24 || !isPlainObject(where))
163
+ return;
164
+ for (const [key, value] of Object.entries(where)) {
165
+ if (value === undefined)
166
+ continue;
167
+ if (key === "AND" || key === "OR" || key === "NOT") {
168
+ for (const item of asArray(value))
169
+ this.columnsOf(model, item, depth + 1);
170
+ continue;
171
+ }
172
+ const relation = this.tracker.relation(model, key);
173
+ if (relation) {
174
+ if (!isPlainObject(value))
175
+ continue;
176
+ const wrapped = Object.keys(value).some((k) => POSITIVE_RELATION_FILTERS.has(k) || NEGATIVE_RELATION_FILTERS.has(k));
177
+ if (!wrapped)
178
+ this.columnsOf(relation.target, value, depth + 1);
179
+ else
180
+ for (const inner of Object.values(value))
181
+ this.columnsOf(relation.target, inner, depth + 1);
182
+ continue;
183
+ }
184
+ if (this.tracker.fieldType(model, key) !== undefined) {
185
+ this.addColumn(model, key);
186
+ continue;
187
+ }
188
+ // A compound unique: equalities on this very model.
189
+ this.columnsOf(model, value, depth + 1);
190
+ }
191
+ }
192
+ /**
193
+ * The columns one call's arguments filter, order, page or group by — at
194
+ * the top and inside every `include`/`select` of a relation, because a
195
+ * nested `where` decides which related rows are in the result too.
196
+ */
197
+ columnsOfArgs(model, args, depth) {
198
+ if (depth > 24 || !isPlainObject(args))
199
+ return;
200
+ this.columnsOf(model, args.where, depth + 1);
201
+ this.columnsOf(model, args.having, depth + 1);
202
+ this.columnsOf(model, args.cursor, depth + 1);
203
+ this.orderColumns(model, args.orderBy, depth + 1);
204
+ for (const key of ["distinct", "by"]) {
205
+ for (const field of asArray(args[key])) {
206
+ if (typeof field === "string" && this.tracker.fieldType(model, field) !== undefined) {
207
+ this.addColumn(model, field);
208
+ }
209
+ }
210
+ }
211
+ for (const key of ["select", "include"]) {
212
+ const nested = args[key];
213
+ if (!isPlainObject(nested))
214
+ continue;
215
+ for (const [field, child] of Object.entries(nested)) {
216
+ if (field === "_count") {
217
+ this.columnsOfArgs(model, child, depth + 1);
218
+ continue;
219
+ }
220
+ const relation = this.tracker.relation(model, field);
221
+ if (relation)
222
+ this.columnsOfArgs(relation.target, child, depth + 1);
223
+ }
224
+ }
225
+ }
226
+ /** `orderBy` keys, including an order by a related row's field. */
227
+ orderColumns(model, orderBy, depth) {
228
+ if (depth > 24)
229
+ return;
230
+ for (const item of asArray(orderBy)) {
231
+ if (!isPlainObject(item))
232
+ continue;
233
+ for (const [field, value] of Object.entries(item)) {
234
+ if (this.tracker.fieldType(model, field) !== undefined) {
235
+ this.addColumn(model, field);
236
+ continue;
237
+ }
238
+ const relation = this.tracker.relation(model, field);
239
+ if (relation)
240
+ this.orderColumns(relation.target, value, depth + 1);
241
+ }
242
+ }
243
+ }
141
244
  /**
142
245
  * Record a read of a table that is not a Prisma model — one of Nowity's
143
246
  * own — whose rows were selected by `column IN values`. `null` for values
@@ -31,6 +31,32 @@ export interface NowityRuntimeConfig {
31
31
  * Next asks the same question the socket does and has to get the same answer.
32
32
  */
33
33
  auth?: Function;
34
+ /**
35
+ * Derive client mutators from what mutations write, and send each live
36
+ * query the ones that can change it, so a browser shows a simple write
37
+ * before the server confirms it without anyone writing a `local()` for it.
38
+ *
39
+ * On unless `false`. The browser has its own switch (`optimistic` on the
40
+ * client), which turns off every optimistic effect — derived and
41
+ * hand-written — for comparing the two.
42
+ */
43
+ derivedMutators?: boolean;
44
+ /**
45
+ * A second client of the same schema, used for nothing but read snapshots:
46
+ * the change stream's fan-out, the first answer of a subscription, and
47
+ * server-side rendering.
48
+ *
49
+ * Every snapshot is an interactive transaction, and on a shared client it
50
+ * waits in the same pool as the application's writes. Under load the two
51
+ * starve each other — a write cannot get a connection because the stream
52
+ * holds them, the stream falls behind because writes do. Give it a client
53
+ * with a pool of its own (`datasourceUrl` with `connection_limit`) and the
54
+ * two stop competing. The client is extended for read tracking here, like
55
+ * {@link prisma}; pass the plain one.
56
+ *
57
+ * Absent, snapshots run on {@link prisma}.
58
+ */
59
+ snapshotPrisma?: any;
34
60
  onWarning?: (message: string) => void;
35
61
  }
36
62
  export interface NowityRuntime {
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../../src/server/api/runtime.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,oBAAoB,EAAkB,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAInD,OAAO,EAAE,cAAc,EAAe,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,WAAW,mBAAmB;IAChC,uCAAuC;IACvC,MAAM,EAAE,GAAG,CAAC;IACZ,gDAAgD;IAChD,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB;;;;;OAKG;IACH,EAAE,CAAC,EAAE,QAAQ,CAAC;IACd;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;IACtB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC;AAED,MAAM,WAAW,aAAa;IAC1B,GAAG,EAAE,QAAQ,CAAC;IACd,SAAS,EAAE,oBAAoB,CAAC;IAChC,2EAA2E;IAC3E,MAAM,EAAE,GAAG,CAAC;IACZ,OAAO,EAAE,cAAc,CAAC;CAC3B;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,CA0C9E"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../../src/server/api/runtime.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,oBAAoB,EAAkB,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAInD,OAAO,EAAE,cAAc,EAAe,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,WAAW,mBAAmB;IAChC,uCAAuC;IACvC,MAAM,EAAE,GAAG,CAAC;IACZ,gDAAgD;IAChD,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB;;;;;OAKG;IACH,EAAE,CAAC,EAAE,QAAQ,CAAC;IACd;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;IACtB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,EAAE,GAAG,CAAC;IACrB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC;AAED,MAAM,WAAW,aAAa;IAC1B,GAAG,EAAE,QAAQ,CAAC;IACd,SAAS,EAAE,oBAAoB,CAAC;IAChC,2EAA2E;IAC3E,MAAM,EAAE,GAAG,CAAC;IACZ,OAAO,EAAE,cAAc,CAAC;CAC3B;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,CA4C9E"}
@@ -57,7 +57,9 @@ function createNowityRuntime(config) {
57
57
  contracts,
58
58
  tracker,
59
59
  resolve: (contract) => context.getBean(contract),
60
+ derivedMutators: config.derivedMutators,
60
61
  onWarning: config.onWarning,
61
62
  });
62
- return { api, snapshots: new db_1.PrismaSnapshotRunner(prisma), prisma, tracker };
63
+ const snapshotClient = config.snapshotPrisma ? (0, tracked_client_1.trackReads)(config.snapshotPrisma, tracker) : prisma;
64
+ return { api, snapshots: new db_1.PrismaSnapshotRunner(snapshotClient), prisma, tracker };
63
65
  }
@@ -1 +1 @@
1
- {"version":3,"file":"tracked-client.d.ts","sourceRoot":"","sources":["../../../src/server/api/tracked-client.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,CAAC,SAAS,EAAE,OAAO,GAAG,GAAG,CAAC;CACrC;AAED,wBAAgB,UAAU,CAAC,CAAC,SAAS,gBAAgB,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,cAAc,GAAG,CAAC,CAoD5F"}
1
+ {"version":3,"file":"tracked-client.d.ts","sourceRoot":"","sources":["../../../src/server/api/tracked-client.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,CAAC,SAAS,EAAE,OAAO,GAAG,GAAG,CAAC;CACrC;AAED,wBAAgB,UAAU,CAAC,CAAC,SAAS,gBAAgB,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,cAAc,GAAG,CAAC,CA4D5F"}
@@ -28,6 +28,15 @@ function trackReads(prisma, tracker) {
28
28
  `"${model ?? "raw"}" was called inside a live query, which is ` +
29
29
  `re-executed on every change to the tables it reads.`);
30
30
  }
31
+ // What a mutation writes is what its derived client mutator is
32
+ // learned from. Raw SQL that writes makes the list incomplete,
33
+ // and says so.
34
+ if (execution.writes && tracker.isWrite(operation)) {
35
+ if (tracker.isRaw(operation) || !model)
36
+ execution.writes.raw = true;
37
+ else
38
+ execution.writes.writes.push({ model, operation, args });
39
+ }
31
40
  if (tracker.isRaw(operation) || !model) {
32
41
  // Nothing to derive from raw SQL. Say so rather than guess:
33
42
  // the subscription falls back to being revalidated always.
@@ -9,8 +9,10 @@ export type BatchHandler = (batch: TransactionBatch) => Promise<void>;
9
9
  * WAL can be swapped in later without touching the sync engine.
10
10
  *
11
11
  * Contract every implementation must honour:
12
- * 1. `handler` is called once per database transaction, with commit boundaries
13
- * preserved and in commit order.
12
+ * 1. `handler` is called with whole database transactions in commit order —
13
+ * one per call, or several committed ones together (the outbox does that
14
+ * per read, so the engine takes one snapshot for all of them). A
15
+ * transaction is never split across calls.
14
16
  * 2. `handler` is awaited; no further batch is delivered until it resolves.
15
17
  * 3. Delivery is at-least-once. Duplicates are allowed — the engine recomputes
16
18
  * queries from the database, so replaying a batch produces an empty diff.