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.
- package/README.md +751 -0
- package/dist/api/contract.d.ts +186 -0
- package/dist/api/contract.d.ts.map +1 -0
- package/dist/api/contract.js +251 -0
- package/dist/api/errors.d.ts +50 -0
- package/dist/api/errors.d.ts.map +1 -0
- package/dist/api/errors.js +50 -0
- package/dist/api/index.d.ts +13 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +32 -0
- package/dist/client/confirmed-store.d.ts +33 -0
- package/dist/client/confirmed-store.d.ts.map +1 -0
- package/dist/client/confirmed-store.js +123 -0
- package/dist/client/connection.d.ts +102 -0
- package/dist/client/connection.d.ts.map +1 -0
- package/dist/client/connection.js +285 -0
- package/dist/client/list.d.ts +102 -0
- package/dist/client/list.d.ts.map +1 -0
- package/dist/client/list.js +257 -0
- package/dist/client/live-query.d.ts +153 -0
- package/dist/client/live-query.d.ts.map +1 -0
- package/dist/client/live-query.js +244 -0
- package/dist/client/nowity-client.d.ts +188 -0
- package/dist/client/nowity-client.d.ts.map +1 -0
- package/dist/client/nowity-client.js +580 -0
- package/dist/client/null-transport.d.ts +16 -0
- package/dist/client/null-transport.d.ts.map +1 -0
- package/dist/client/null-transport.js +17 -0
- package/dist/client/overlay.d.ts +44 -0
- package/dist/client/overlay.d.ts.map +1 -0
- package/dist/client/overlay.js +90 -0
- package/dist/client/query-source.d.ts +78 -0
- package/dist/client/query-source.d.ts.map +1 -0
- package/dist/client/query-source.js +57 -0
- package/dist/client/ssr-source.d.ts +70 -0
- package/dist/client/ssr-source.d.ts.map +1 -0
- package/dist/client/ssr-source.js +134 -0
- package/dist/client/transport.d.ts +19 -0
- package/dist/client/transport.d.ts.map +1 -0
- package/dist/client/transport.js +2 -0
- package/dist/client/ws-transport.d.ts +30 -0
- package/dist/client/ws-transport.d.ts.map +1 -0
- package/dist/client/ws-transport.js +89 -0
- package/dist/client.d.ts +47 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +80 -0
- package/dist/core/diff.d.ts +14 -0
- package/dist/core/diff.d.ts.map +1 -0
- package/dist/core/diff.js +56 -0
- package/dist/core/normalize.d.ts +16 -0
- package/dist/core/normalize.d.ts.map +1 -0
- package/dist/core/normalize.js +67 -0
- package/dist/core/protocol.d.ts +215 -0
- package/dist/core/protocol.d.ts.map +1 -0
- package/dist/core/protocol.js +14 -0
- package/dist/core/ssr-bridge.d.ts +69 -0
- package/dist/core/ssr-bridge.d.ts.map +1 -0
- package/dist/core/ssr-bridge.js +40 -0
- package/dist/core/types.d.ts +96 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +17 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +98 -0
- package/dist/ironbean/client.d.ts +7 -0
- package/dist/ironbean/client.d.ts.map +1 -0
- package/dist/ironbean/client.js +38 -0
- package/dist/ironbean/server.d.ts +42 -0
- package/dist/ironbean/server.d.ts.map +1 -0
- package/dist/ironbean/server.js +62 -0
- package/dist/ironbean/tokens.d.ts +26 -0
- package/dist/ironbean/tokens.d.ts.map +1 -0
- package/dist/ironbean/tokens.js +23 -0
- package/dist/next/config.d.ts +7 -0
- package/dist/next/config.d.ts.map +1 -0
- package/dist/next/config.js +77 -0
- package/dist/next/live.d.ts +69 -0
- package/dist/next/live.d.ts.map +1 -0
- package/dist/next/live.js +70 -0
- package/dist/next/serve.d.ts +44 -0
- package/dist/next/serve.d.ts.map +1 -0
- package/dist/next/serve.js +107 -0
- package/dist/next/server.d.ts +62 -0
- package/dist/next/server.d.ts.map +1 -0
- package/dist/next/server.js +127 -0
- package/dist/react/connection-context.d.ts +4 -0
- package/dist/react/connection-context.d.ts.map +1 -0
- package/dist/react/connection-context.js +28 -0
- package/dist/react/grid-list.d.ts +34 -0
- package/dist/react/grid-list.d.ts.map +1 -0
- package/dist/react/grid-list.js +62 -0
- package/dist/react/hydrate.d.ts +22 -0
- package/dist/react/hydrate.d.ts.map +1 -0
- package/dist/react/hydrate.js +18 -0
- package/dist/react/index.d.ts +35 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +51 -0
- package/dist/react/list.d.ts +31 -0
- package/dist/react/list.d.ts.map +1 -0
- package/dist/react/list.js +35 -0
- package/dist/react/prerendered.d.ts +33 -0
- package/dist/react/prerendered.d.ts.map +1 -0
- package/dist/react/prerendered.js +165 -0
- package/dist/react/provider.d.ts +64 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +130 -0
- package/dist/react/refusals.d.ts +31 -0
- package/dist/react/refusals.d.ts.map +1 -0
- package/dist/react/refusals.js +51 -0
- package/dist/react/restricted.d.ts +76 -0
- package/dist/react/restricted.d.ts.map +1 -0
- package/dist/react/restricted.js +126 -0
- package/dist/react/use-list.d.ts +42 -0
- package/dist/react/use-list.d.ts.map +1 -0
- package/dist/react/use-list.js +56 -0
- package/dist/react/use-query.d.ts +66 -0
- package/dist/react/use-query.d.ts.map +1 -0
- package/dist/react/use-query.js +126 -0
- package/dist/react/virtual.d.ts +60 -0
- package/dist/react/virtual.d.ts.map +1 -0
- package/dist/react/virtual.js +131 -0
- package/dist/server/api/auth.d.ts +37 -0
- package/dist/server/api/auth.d.ts.map +1 -0
- package/dist/server/api/auth.js +54 -0
- package/dist/server/api/bridge.d.ts +28 -0
- package/dist/server/api/bridge.d.ts.map +1 -0
- package/dist/server/api/bridge.js +330 -0
- package/dist/server/api/collector.d.ts +25 -0
- package/dist/server/api/collector.d.ts.map +1 -0
- package/dist/server/api/collector.js +184 -0
- package/dist/server/api/configure.d.ts +31 -0
- package/dist/server/api/configure.d.ts.map +1 -0
- package/dist/server/api/configure.js +67 -0
- package/dist/server/api/execution.d.ts +77 -0
- package/dist/server/api/execution.d.ts.map +1 -0
- package/dist/server/api/execution.js +97 -0
- package/dist/server/api/list.d.ts +141 -0
- package/dist/server/api/list.d.ts.map +1 -0
- package/dist/server/api/list.js +281 -0
- package/dist/server/api/prerender.d.ts +32 -0
- package/dist/server/api/prerender.d.ts.map +1 -0
- package/dist/server/api/prerender.js +69 -0
- package/dist/server/api/read-keys.d.ts +128 -0
- package/dist/server/api/read-keys.d.ts.map +1 -0
- package/dist/server/api/read-keys.js +464 -0
- package/dist/server/api/read-set.d.ts +103 -0
- package/dist/server/api/read-set.d.ts.map +1 -0
- package/dist/server/api/read-set.js +210 -0
- package/dist/server/api/runtime.d.ts +36 -0
- package/dist/server/api/runtime.d.ts.map +1 -0
- package/dist/server/api/runtime.js +49 -0
- package/dist/server/api/tracked-client.d.ts +6 -0
- package/dist/server/api/tracked-client.d.ts.map +1 -0
- package/dist/server/api/tracked-client.js +56 -0
- package/dist/server/change-source/change-source.d.ts +41 -0
- package/dist/server/change-source/change-source.d.ts.map +1 -0
- package/dist/server/change-source/change-source.js +2 -0
- package/dist/server/change-source/outbox-change-source.d.ts +106 -0
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -0
- package/dist/server/change-source/outbox-change-source.js +237 -0
- package/dist/server/change-source/wal-change-source.d.ts +85 -0
- package/dist/server/change-source/wal-change-source.d.ts.map +1 -0
- package/dist/server/change-source/wal-change-source.js +193 -0
- package/dist/server/db.d.ts +51 -0
- package/dist/server/db.d.ts.map +1 -0
- package/dist/server/db.js +53 -0
- package/dist/server/evaluate.d.ts +39 -0
- package/dist/server/evaluate.d.ts.map +1 -0
- package/dist/server/evaluate.js +61 -0
- package/dist/server/invalidation-index.d.ts +64 -0
- package/dist/server/invalidation-index.d.ts.map +1 -0
- package/dist/server/invalidation-index.js +0 -0
- package/dist/server/mutator.d.ts +59 -0
- package/dist/server/mutator.d.ts.map +1 -0
- package/dist/server/mutator.js +77 -0
- package/dist/server/query.d.ts +113 -0
- package/dist/server/query.d.ts.map +1 -0
- package/dist/server/query.js +35 -0
- package/dist/server/setup.d.ts +30 -0
- package/dist/server/setup.d.ts.map +1 -0
- package/dist/server/setup.js +107 -0
- package/dist/server/sql/setup.sql +215 -0
- package/dist/server/ssr.d.ts +29 -0
- package/dist/server/ssr.d.ts.map +1 -0
- package/dist/server/ssr.js +43 -0
- package/dist/server/sync-engine.d.ts +256 -0
- package/dist/server/sync-engine.d.ts.map +1 -0
- package/dist/server/sync-engine.js +805 -0
- package/dist/server/ws-server.d.ts +56 -0
- package/dist/server/ws-server.d.ts.map +1 -0
- package/dist/server/ws-server.js +241 -0
- package/package.json +81 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ListPlans = exports.LIST_COUNT_ENTITY = void 0;
|
|
4
|
+
exports.beginList = beginList;
|
|
5
|
+
exports.interceptFindMany = interceptFindMany;
|
|
6
|
+
exports.evaluateList = evaluateList;
|
|
7
|
+
exports.countRowId = countRowId;
|
|
8
|
+
/** Entity the count rows are stored under. Not a table, and cannot collide with one. */
|
|
9
|
+
exports.LIST_COUNT_ENTITY = "nowity:list";
|
|
10
|
+
/**
|
|
11
|
+
* Where each endpoint's collection was last found.
|
|
12
|
+
*
|
|
13
|
+
* Keyed by endpoint rather than by arguments: it is a fact about the code, not
|
|
14
|
+
* about the call. A method that reaches its collection by different routes for
|
|
15
|
+
* different arguments simply gets its plan re-learned, which costs one extra
|
|
16
|
+
* execution and is still correct.
|
|
17
|
+
*
|
|
18
|
+
* "Probed and there was nothing to push into" is an answer too, and a different
|
|
19
|
+
* one from "not probed yet" — otherwise an endpoint that cannot be paged would
|
|
20
|
+
* be probed again before every window it ever serves.
|
|
21
|
+
*/
|
|
22
|
+
class ListPlans {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.plans = new Map();
|
|
25
|
+
}
|
|
26
|
+
known(endpoint) {
|
|
27
|
+
return this.plans.has(endpoint);
|
|
28
|
+
}
|
|
29
|
+
get(endpoint) {
|
|
30
|
+
return this.plans.get(endpoint) ?? null;
|
|
31
|
+
}
|
|
32
|
+
learn(endpoint, plan) {
|
|
33
|
+
this.plans.set(endpoint, plan);
|
|
34
|
+
}
|
|
35
|
+
forget(endpoint) {
|
|
36
|
+
this.plans.delete(endpoint);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.ListPlans = ListPlans;
|
|
40
|
+
function beginList(want, plan) {
|
|
41
|
+
return { want, plan, probe: false, calls: [], windowed: null };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A `findMany` about to run inside a list execution.
|
|
45
|
+
*
|
|
46
|
+
* Returns the arguments it should run with — the method's own, plus the window
|
|
47
|
+
* when this is the call the plan points at. A collection that already limits
|
|
48
|
+
* itself is left alone: composing someone else's `take` with ours would answer
|
|
49
|
+
* with rows neither of us asked for.
|
|
50
|
+
*/
|
|
51
|
+
function interceptFindMany(list, model, args) {
|
|
52
|
+
const call = { model, ordinal: list.calls.length + 1, args, windowed: false };
|
|
53
|
+
list.calls.push(call);
|
|
54
|
+
// A probe answers nobody, so every query it makes is cut down to one row.
|
|
55
|
+
if (list.probe) {
|
|
56
|
+
return { args: isPushable(args) ? { ...args, skip: 0, take: 1 } : args, call };
|
|
57
|
+
}
|
|
58
|
+
const planned = list.plan?.ordinal === call.ordinal && list.plan.model === model;
|
|
59
|
+
if (!planned || !isPushable(args))
|
|
60
|
+
return { args, call };
|
|
61
|
+
call.windowed = true;
|
|
62
|
+
list.windowed = call;
|
|
63
|
+
return { args: { ...args, ...windowArgs(list.want) }, call };
|
|
64
|
+
}
|
|
65
|
+
function isPushable(args) {
|
|
66
|
+
return (args?.take === undefined &&
|
|
67
|
+
args?.skip === undefined &&
|
|
68
|
+
args?.cursor === undefined &&
|
|
69
|
+
args?.distinct === undefined);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The window as Prisma arguments.
|
|
73
|
+
*
|
|
74
|
+
* A count run asks for one row rather than none, so that a method guarding on
|
|
75
|
+
* `rows.length` reaches the same verdict it would have reached unwindowed: an
|
|
76
|
+
* empty collection is still empty, a non-empty one is still not.
|
|
77
|
+
*/
|
|
78
|
+
function windowArgs(want) {
|
|
79
|
+
return want.kind === "page"
|
|
80
|
+
? { skip: want.offset, take: want.limit }
|
|
81
|
+
: { skip: 0, take: 1 };
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The collection's `where`, when it is simple enough to be answered elsewhere.
|
|
85
|
+
*
|
|
86
|
+
* Only a flat map of field to scalar — the shape `{ projectId: "archive" }` has,
|
|
87
|
+
* and the shape a browser can check a row against without reimplementing a query
|
|
88
|
+
* planner. An absent `where` is `{}`: every row of the entity belongs, which is
|
|
89
|
+
* a real answer and a common one.
|
|
90
|
+
*
|
|
91
|
+
* Anything richer is refused outright. `OR`, `gt`, a nested relation, a `mode`
|
|
92
|
+
* — each of them is a small language, and a browser that got one of them subtly
|
|
93
|
+
* wrong would show a row in a collection it does not belong to. Refusing costs a
|
|
94
|
+
* round trip on a locally created row, which is what it cost before any of this.
|
|
95
|
+
*/
|
|
96
|
+
function filterOf(args) {
|
|
97
|
+
const where = args?.where;
|
|
98
|
+
if (where === undefined || where === null)
|
|
99
|
+
return {};
|
|
100
|
+
if (typeof where !== "object" || Array.isArray(where))
|
|
101
|
+
return undefined;
|
|
102
|
+
// The same rule the ordering follows: a condition on a column the rows do not
|
|
103
|
+
// carry cannot be checked against them. A query that filters on something it
|
|
104
|
+
// does not select therefore sends no filter, and a row created here waits for
|
|
105
|
+
// the server — which is what it did before any of this existed.
|
|
106
|
+
const selected = args.select && typeof args.select === "object" ? new Set(Object.keys(args.select)) : null;
|
|
107
|
+
const filter = {};
|
|
108
|
+
for (const [field, value] of Object.entries(where)) {
|
|
109
|
+
if (selected && !selected.has(field))
|
|
110
|
+
return undefined;
|
|
111
|
+
if (value === null) {
|
|
112
|
+
filter[field] = null;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const kind = typeof value;
|
|
116
|
+
if (kind !== "string" && kind !== "number" && kind !== "boolean")
|
|
117
|
+
return undefined;
|
|
118
|
+
filter[field] = value;
|
|
119
|
+
}
|
|
120
|
+
return filter;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The collection's `orderBy`, as keys a browser can compare by.
|
|
124
|
+
*
|
|
125
|
+
* Read off the query the endpoint already wrote, which is the only place an
|
|
126
|
+
* ordering is allowed to be stated. It travels to the browser so that a row
|
|
127
|
+
* changed locally can be put in its place before the server has been asked —
|
|
128
|
+
* and it is sent as *data*, so nothing on the client duplicates the decision.
|
|
129
|
+
*
|
|
130
|
+
* Anything that is not a plain scalar direction — ordering through a relation,
|
|
131
|
+
* by an aggregate, by a raw expression — yields nothing at all. A browser that
|
|
132
|
+
* cannot evaluate the ordering must not guess at it: it waits for the server,
|
|
133
|
+
* which is what it does today.
|
|
134
|
+
*/
|
|
135
|
+
function orderOf(args, model, kindOf) {
|
|
136
|
+
const clauses = args?.orderBy;
|
|
137
|
+
if (!clauses)
|
|
138
|
+
return undefined;
|
|
139
|
+
// A key the rows do not carry cannot be compared, and comparing what is
|
|
140
|
+
// missing is worse than not comparing: every row's value would be
|
|
141
|
+
// `undefined`, so the ordering would come out of whatever the remaining keys
|
|
142
|
+
// said — quietly, and wrongly. A query that orders by a column it does not
|
|
143
|
+
// select therefore sends no ordering at all.
|
|
144
|
+
const selected = args.select && typeof args.select === "object" ? new Set(Object.keys(args.select)) : null;
|
|
145
|
+
const keys = [];
|
|
146
|
+
for (const clause of Array.isArray(clauses) ? clauses : [clauses]) {
|
|
147
|
+
if (!clause || typeof clause !== "object")
|
|
148
|
+
return undefined;
|
|
149
|
+
for (const [field, value] of Object.entries(clause)) {
|
|
150
|
+
// `{ points: { sort: "desc", nulls: "last" } }` is the long form of
|
|
151
|
+
// the same thing; anything else is a relation or an aggregate.
|
|
152
|
+
const dir = typeof value === "string" ? value : value?.sort;
|
|
153
|
+
if (dir !== "asc" && dir !== "desc")
|
|
154
|
+
return undefined;
|
|
155
|
+
// The default placement of nulls — last ascending, first descending —
|
|
156
|
+
// is the one the browser reproduces. An explicit `nulls` is a second
|
|
157
|
+
// rule, and one this cannot express, so it sends nothing.
|
|
158
|
+
if (typeof value === "object" && value?.nulls !== undefined)
|
|
159
|
+
return undefined;
|
|
160
|
+
if (selected && !selected.has(field))
|
|
161
|
+
return undefined;
|
|
162
|
+
// A field the schema does not know is not a column — an ordering
|
|
163
|
+
// through a relation, most likely. Nothing to compare by.
|
|
164
|
+
const type = kindOf(model, field);
|
|
165
|
+
if (!type)
|
|
166
|
+
return undefined;
|
|
167
|
+
keys.push({ field, dir, type });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return keys.length ? keys : undefined;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Run a list endpoint for one window, pushing the window in where it can.
|
|
174
|
+
*
|
|
175
|
+
* `run` starts the method inside a fresh execution; it may be called twice, and
|
|
176
|
+
* the second run is the honest one — whole method, no window, sliced in memory.
|
|
177
|
+
*/
|
|
178
|
+
async function evaluateList(plans, endpoint, want, run,
|
|
179
|
+
/** How a field may be compared in a browser, from the schema. */
|
|
180
|
+
kindOf = () => undefined) {
|
|
181
|
+
if (!plans.known(endpoint))
|
|
182
|
+
await probeFor(plans, endpoint, run);
|
|
183
|
+
let state = beginList(want, plans.get(endpoint));
|
|
184
|
+
let attempt = await run(state);
|
|
185
|
+
// The window went somewhere that turned out not to be the collection: the
|
|
186
|
+
// method took a different route this time. What came back is a window of the
|
|
187
|
+
// wrong query, so it is discarded rather than served.
|
|
188
|
+
if (state.windowed && state.windowed !== last(state.calls)) {
|
|
189
|
+
plans.forget(endpoint);
|
|
190
|
+
state = beginList(want, null);
|
|
191
|
+
attempt = await run(state);
|
|
192
|
+
}
|
|
193
|
+
const collection = last(state.calls);
|
|
194
|
+
if (collection) {
|
|
195
|
+
plans.learn(endpoint, { ordinal: collection.ordinal, model: collection.model });
|
|
196
|
+
}
|
|
197
|
+
const order = collection ? orderOf(collection.args, collection.model, kindOf) : undefined;
|
|
198
|
+
// Membership is only worth sending alongside an ordering: a browser that
|
|
199
|
+
// knows a row belongs but not where it goes cannot place it anyway.
|
|
200
|
+
const filter = order && collection ? filterOf(collection.args) : undefined;
|
|
201
|
+
if (state.windowed) {
|
|
202
|
+
return {
|
|
203
|
+
execution: attempt.execution,
|
|
204
|
+
rows: asRows(attempt.returned),
|
|
205
|
+
total: want.kind === "count" ? await countOf(attempt.execution, state.windowed) : 0,
|
|
206
|
+
...(order ? { order } : {}),
|
|
207
|
+
...(filter ? { filter } : {}),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
// Nothing to push a window into — no `findMany` at all, or one that limits
|
|
211
|
+
// itself. The method ran whole, so the window is a slice of what it returned
|
|
212
|
+
// and the total is simply how much that was.
|
|
213
|
+
const all = asRows(attempt.returned);
|
|
214
|
+
return {
|
|
215
|
+
execution: attempt.execution,
|
|
216
|
+
rows: want.kind === "page" ? all.slice(want.offset, want.offset + want.limit) : [],
|
|
217
|
+
total: all.length,
|
|
218
|
+
...(order ? { order } : {}),
|
|
219
|
+
...(filter ? { filter } : {}),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Find the collection without reading it.
|
|
224
|
+
*
|
|
225
|
+
* The method runs for real — the permission check included, so a refusal is
|
|
226
|
+
* still a refusal and comes out of here rather than out of the run that follows
|
|
227
|
+
* — but every query it makes returns at most one row. What survives is the
|
|
228
|
+
* position of its last `findMany`, which is the only thing the next run needs.
|
|
229
|
+
*/
|
|
230
|
+
async function probeFor(plans, endpoint, run) {
|
|
231
|
+
const state = {
|
|
232
|
+
want: { kind: "count" },
|
|
233
|
+
plan: null,
|
|
234
|
+
probe: true,
|
|
235
|
+
calls: [],
|
|
236
|
+
windowed: null,
|
|
237
|
+
};
|
|
238
|
+
await run(state);
|
|
239
|
+
const collection = last(state.calls);
|
|
240
|
+
plans.learn(endpoint, collection ? { ordinal: collection.ordinal, model: collection.model } : null);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* How many rows the collection has, asked of the database rather than counted.
|
|
244
|
+
*
|
|
245
|
+
* The `where` is the collection's own, so the count and the windows are answers
|
|
246
|
+
* about the same set of rows — and both are read inside the engine's snapshot,
|
|
247
|
+
* so a scrollbar cannot be sized from a different moment than the rows under it.
|
|
248
|
+
*/
|
|
249
|
+
async function countOf(execution, call) {
|
|
250
|
+
const delegate = execution.prisma[delegateOf(call.model)];
|
|
251
|
+
if (!delegate?.count) {
|
|
252
|
+
throw new Error(`Cannot count "${call.model}": the Prisma client has no such model. ` +
|
|
253
|
+
`This is a Nowity bug, not an application one.`);
|
|
254
|
+
}
|
|
255
|
+
const where = call.args?.where;
|
|
256
|
+
return delegate.count(where === undefined ? {} : { where });
|
|
257
|
+
}
|
|
258
|
+
/** `Document` is `prisma.document` — Prisma lowercases the first letter and nothing else. */
|
|
259
|
+
function delegateOf(model) {
|
|
260
|
+
return model.charAt(0).toLowerCase() + model.slice(1);
|
|
261
|
+
}
|
|
262
|
+
function last(items) {
|
|
263
|
+
return items.length ? items[items.length - 1] : null;
|
|
264
|
+
}
|
|
265
|
+
function asRows(returned) {
|
|
266
|
+
if (returned === null || returned === undefined)
|
|
267
|
+
return [];
|
|
268
|
+
if (Array.isArray(returned))
|
|
269
|
+
return returned;
|
|
270
|
+
throw new Error(`A @list method must return an array of rows; this one returned ${typeof returned}.`);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Identity of a collection's count row.
|
|
274
|
+
*
|
|
275
|
+
* It has to be stable across recomputations — the client's identity map is what
|
|
276
|
+
* makes a changing total a field update rather than a row being replaced — and
|
|
277
|
+
* distinct per collection, which the endpoint and its arguments already are.
|
|
278
|
+
*/
|
|
279
|
+
function countRowId(endpoint, args) {
|
|
280
|
+
return `${endpoint}|${JSON.stringify(args ?? null)}`;
|
|
281
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first paint: `Database → Server Component → initialProps → MobX store`.
|
|
3
|
+
*
|
|
4
|
+
* Endpoints are called the same way here as anywhere else — the same methods,
|
|
5
|
+
* the same arguments, the same permission checks — but every call is also
|
|
6
|
+
* recorded as a hydration payload. The browser then claims those payloads on its
|
|
7
|
+
* first subscribe, so the first render has real rows instead of a spinner that
|
|
8
|
+
* resolves into either data or an error.
|
|
9
|
+
*
|
|
10
|
+
* Everything a page prerenders is read inside **one** snapshot. A page that
|
|
11
|
+
* renders two queries cannot show them from two different points in time.
|
|
12
|
+
*/
|
|
13
|
+
import type { HydrationPayload } from "../../core/protocol";
|
|
14
|
+
import type { SnapshotRunner } from "../db";
|
|
15
|
+
import type { QueryRegistry, SessionInfo } from "../query";
|
|
16
|
+
/** Resolve a contract to a recording view of its implementation. */
|
|
17
|
+
export type PrerenderGet = <T extends object>(service: string | (abstract new (...args: any[]) => T)) => T;
|
|
18
|
+
/** A refusal during prerendering, carrying everything that was read before it. */
|
|
19
|
+
export interface PrerenderError extends Error {
|
|
20
|
+
hydration?: HydrationPayload[];
|
|
21
|
+
}
|
|
22
|
+
export interface PrerenderResult<T> {
|
|
23
|
+
data: T;
|
|
24
|
+
/** Feed this to the browser client; it is consumed by the matching subscribe. */
|
|
25
|
+
hydration: HydrationPayload[];
|
|
26
|
+
}
|
|
27
|
+
export interface PrerenderDeps {
|
|
28
|
+
snapshots: SnapshotRunner;
|
|
29
|
+
queries: QueryRegistry;
|
|
30
|
+
}
|
|
31
|
+
export declare function prerender<T>(deps: PrerenderDeps, session: SessionInfo, render: (get: PrerenderGet) => Promise<T>): Promise<PrerenderResult<T>>;
|
|
32
|
+
//# sourceMappingURL=prerender.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prerender.d.ts","sourceRoot":"","sources":["../../../src/server/api/prerender.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAI5D,OAAO,KAAK,EAAmB,cAAc,EAAE,MAAM,OAAO,CAAC;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE3D,oEAAoE;AACpE,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,SAAS,MAAM,EACxC,OAAO,EAAE,MAAM,GAAG,CAAC,QAAQ,MAAM,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,KACrD,CAAC,CAAC;AAEP,kFAAkF;AAClF,MAAM,WAAW,cAAe,SAAQ,KAAK;IACzC,SAAS,CAAC,EAAE,gBAAgB,EAAE,CAAC;CAClC;AAED,MAAM,WAAW,eAAe,CAAC,CAAC;IAC9B,IAAI,EAAE,CAAC,CAAC;IACR,iFAAiF;IACjF,SAAS,EAAE,gBAAgB,EAAE,CAAC;CACjC;AAED,MAAM,WAAW,aAAa;IAC1B,SAAS,EAAE,cAAc,CAAC;IAC1B,OAAO,EAAE,aAAa,CAAC;CAC1B;AAED,wBAAsB,SAAS,CAAC,CAAC,EAC7B,IAAI,EAAE,aAAa,EACnB,OAAO,EAAE,WAAW,EACpB,MAAM,EAAE,CAAC,GAAG,EAAE,YAAY,KAAK,OAAO,CAAC,CAAC,CAAC,GAC1C,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CA2B7B"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prerender = prerender;
|
|
4
|
+
const contract_1 = require("../../api/contract");
|
|
5
|
+
const errors_1 = require("../../api/errors");
|
|
6
|
+
const evaluate_1 = require("../evaluate");
|
|
7
|
+
async function prerender(deps, session, render) {
|
|
8
|
+
const hydration = [];
|
|
9
|
+
// Whatever was read before a refusal is still worth keeping: a page whose
|
|
10
|
+
// second query is forbidden should still hydrate its first, and the refusal
|
|
11
|
+
// itself is recorded as a denied payload so the browser renders "no access"
|
|
12
|
+
// from the first paint instead of after a round trip.
|
|
13
|
+
const data = await deps.snapshots.withSnapshot(async (snap) => {
|
|
14
|
+
const get = (service) => {
|
|
15
|
+
const name = (0, contract_1.contractNameOf)(service);
|
|
16
|
+
return new Proxy({}, {
|
|
17
|
+
get: (_target, method) => typeof method === "string"
|
|
18
|
+
? (...args) => runOne(deps, snap, session, name, method, args, hydration)
|
|
19
|
+
: undefined,
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
try {
|
|
23
|
+
return await render(get);
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
err.hydration = hydration;
|
|
27
|
+
throw err;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return { data, hydration };
|
|
31
|
+
}
|
|
32
|
+
async function runOne(deps, snap, session, contract, method, args, hydration) {
|
|
33
|
+
const name = (0, contract_1.endpointName)(contract, method);
|
|
34
|
+
const def = deps.queries.get(name);
|
|
35
|
+
try {
|
|
36
|
+
const output = await (0, evaluate_1.evaluateQuery)(def, args, session, snap);
|
|
37
|
+
const rows = (0, evaluate_1.outputRows)(output);
|
|
38
|
+
hydration.push({
|
|
39
|
+
query: name,
|
|
40
|
+
args,
|
|
41
|
+
entity: output.entity ?? def.entity,
|
|
42
|
+
...(output.single ? { single: true } : {}),
|
|
43
|
+
ids: output.ids,
|
|
44
|
+
rows: rows.rows,
|
|
45
|
+
...(rows.extra ? { extra: rows.extra } : {}),
|
|
46
|
+
scope: null,
|
|
47
|
+
epoch: 0,
|
|
48
|
+
});
|
|
49
|
+
return output.single ? (rows.rows[0] ?? null) : rows.rows;
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
if (!(0, errors_1.isForbidden)(err))
|
|
53
|
+
throw err;
|
|
54
|
+
// Hydrating a refused query as "denied" rather than as an empty list is
|
|
55
|
+
// what lets the first paint show the real state: an error boundary that
|
|
56
|
+
// fires immediately, not a list that renders empty and then disappears.
|
|
57
|
+
hydration.push({
|
|
58
|
+
query: name,
|
|
59
|
+
args,
|
|
60
|
+
entity: def.entity,
|
|
61
|
+
ids: [],
|
|
62
|
+
rows: [],
|
|
63
|
+
scope: null,
|
|
64
|
+
epoch: 0,
|
|
65
|
+
denied: true,
|
|
66
|
+
});
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which *rows* a query read, not just which tables.
|
|
3
|
+
*
|
|
4
|
+
* {@link ReadSetTracker} answers "did this query read `Document`?", and that is
|
|
5
|
+
* enough to be correct: a change to `Document` recomputes the query, the diff
|
|
6
|
+
* comes out empty, nobody notices. It is not enough to be fast. Every open game
|
|
7
|
+
* reads `Game`, so every move recomputed every game — the cost of one change
|
|
8
|
+
* grew with the number of subscriptions watching the same table, and the total
|
|
9
|
+
* cost with their product.
|
|
10
|
+
*
|
|
11
|
+
* A read key is the missing half: the equalities the query's own `where` already
|
|
12
|
+
* states. `findUnique({ where: { id: "42" } })` reads `Game`, and it reads the
|
|
13
|
+
* row whose `id` is `"42"`. A change to any other row cannot alter that result,
|
|
14
|
+
* so it does not need to touch the database to find that out.
|
|
15
|
+
*
|
|
16
|
+
* ## The rule everything here obeys
|
|
17
|
+
*
|
|
18
|
+
* A key set is sound when every change that *could* alter the result matches at
|
|
19
|
+
* least one key. Over-approximation costs a recomputation; under-approximation
|
|
20
|
+
* costs a subscription that silently stops updating, which is the failure mode
|
|
21
|
+
* this codebase spends work to avoid everywhere else. So every construct that is
|
|
22
|
+
* not understood — an operator added in a future Prisma release, a filter whose
|
|
23
|
+
* semantics are not plain value equality, a raw query — widens to {@link ANY},
|
|
24
|
+
* which means "any change to this table". That is exactly the behaviour this
|
|
25
|
+
* module replaced, so the worst case of not understanding something is the old
|
|
26
|
+
* cost, never a wrong answer.
|
|
27
|
+
*
|
|
28
|
+
* ## Why negation is not narrowed
|
|
29
|
+
*
|
|
30
|
+
* `NOT` inverts which rows matter, and the inversion is not expressible as a set
|
|
31
|
+
* of equalities: under `NOT: { status: "open" }` it is the rows that *stop*
|
|
32
|
+
* being open that change the result. Both images of a change are checked, so
|
|
33
|
+
* some negations would in fact be safe — but "some" is not a rule anyone can
|
|
34
|
+
* apply while reading a where-clause, and a nearly-right invalidation rule is
|
|
35
|
+
* worse than an honest one. Everything under a negation widens.
|
|
36
|
+
*/
|
|
37
|
+
import { type KeyValue, type ReadKey, type ReadKeySet, type TableKeys } from "../../core/types";
|
|
38
|
+
import type { ReadSetTracker } from "./read-set";
|
|
39
|
+
export type { KeyValue, ReadKey, ReadKeySet, TableKeys };
|
|
40
|
+
/** Everything in the table is potentially read. */
|
|
41
|
+
export declare const ANY: "any";
|
|
42
|
+
/**
|
|
43
|
+
* A predicate over one model's own columns, as far as it could be understood.
|
|
44
|
+
*
|
|
45
|
+
* `all`/`some` mirror `AND`/`OR`. They are kept as a tree rather than flattened
|
|
46
|
+
* into a list of keys because the two combine differently: for `AND` a *single*
|
|
47
|
+
* conjunct is enough to be sound (a row failing it cannot match the whole), so
|
|
48
|
+
* the narrowest one wins, while for `OR` every branch has to be represented.
|
|
49
|
+
*/
|
|
50
|
+
export type KeyExpr = {
|
|
51
|
+
kind: "any";
|
|
52
|
+
} | {
|
|
53
|
+
kind: "eq";
|
|
54
|
+
column: string;
|
|
55
|
+
value: KeyValue;
|
|
56
|
+
} | {
|
|
57
|
+
kind: "all";
|
|
58
|
+
parts: KeyExpr[];
|
|
59
|
+
} | {
|
|
60
|
+
kind: "some";
|
|
61
|
+
parts: KeyExpr[];
|
|
62
|
+
};
|
|
63
|
+
export interface KeyCollectorDeps {
|
|
64
|
+
tracker: ReadSetTracker;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Records what each Prisma call read, at row granularity where it can tell.
|
|
68
|
+
*
|
|
69
|
+
* One instance per execution. Calls accumulate: an endpoint that reads `Game`
|
|
70
|
+
* twice with different filters has read the union of both, so the table's keys
|
|
71
|
+
* are the union — and if either call could not be narrowed, the table is
|
|
72
|
+
* {@link ANY} regardless of how precise the other one was.
|
|
73
|
+
*/
|
|
74
|
+
export declare class KeyCollector {
|
|
75
|
+
/** table → the model that names it, and every predicate placed on it. */
|
|
76
|
+
private readonly perTable;
|
|
77
|
+
private readonly tracker;
|
|
78
|
+
constructor(deps: KeyCollectorDeps);
|
|
79
|
+
/**
|
|
80
|
+
* Record one Prisma operation.
|
|
81
|
+
*
|
|
82
|
+
* `model` is the model the operation was called on; `args` its arguments as
|
|
83
|
+
* Prisma received them. An operation without a `where` reads the model
|
|
84
|
+
* unconstrained, which is the honest answer for `findMany({})`.
|
|
85
|
+
*/
|
|
86
|
+
record(model: string | undefined, args: unknown): void;
|
|
87
|
+
/** Everything read so far, collapsed to keys per table. */
|
|
88
|
+
result(): ReadKeySet;
|
|
89
|
+
/** Mark a model's table as read without knowing anything about which rows. */
|
|
90
|
+
widen(model: string): void;
|
|
91
|
+
private push;
|
|
92
|
+
/**
|
|
93
|
+
* The predicate one `where` places on `model`'s own columns.
|
|
94
|
+
*
|
|
95
|
+
* Nested models it mentions are pushed into the collector as it goes, since
|
|
96
|
+
* a filter on a relation is a read of that relation's table.
|
|
97
|
+
*
|
|
98
|
+
* `pessimistic` is set once anything above has inverted the meaning of the
|
|
99
|
+
* subtree; from then on nothing below may narrow anything.
|
|
100
|
+
*/
|
|
101
|
+
private exprFor;
|
|
102
|
+
/** A filter on one scalar column, as far as it is plain value equality. */
|
|
103
|
+
private scalarFilter;
|
|
104
|
+
/** Whether a column's values survive the trip through a change event intact. */
|
|
105
|
+
private isIndexableType;
|
|
106
|
+
private relationFilter;
|
|
107
|
+
/**
|
|
108
|
+
* Walk a subtree for the tables it mentions, narrowing nothing.
|
|
109
|
+
*
|
|
110
|
+
* This is what `select`, `include`, `orderBy` and everything under a
|
|
111
|
+
* negation get: the tables they reach are read, and which of their rows is
|
|
112
|
+
* not something this walk claims to know. A nested `where` inside an
|
|
113
|
+
* `include` is still picked up — the rows it selects are the only ones that
|
|
114
|
+
* relation can contribute.
|
|
115
|
+
*/
|
|
116
|
+
private traverse;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The index entries one table's expressions collapse to.
|
|
120
|
+
*
|
|
121
|
+
* `AND` keeps only its narrowest conjunct, because a row that fails one of them
|
|
122
|
+
* fails the conjunction; `OR` has to keep all of its branches, because any of
|
|
123
|
+
* them alone can put a row in the result.
|
|
124
|
+
*/
|
|
125
|
+
export declare function keysOf(exprs: KeyExpr[], tracker: ReadSetTracker, model?: string): TableKeys;
|
|
126
|
+
/** Merge one read key set into another, widening on conflict. */
|
|
127
|
+
export declare function mergeKeySets(into: ReadKeySet, from: ReadKeySet): void;
|
|
128
|
+
//# sourceMappingURL=read-keys.d.ts.map
|
|
@@ -0,0 +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,WAAW,gBAAgB;IAC7B,OAAO,EAAE,cAAc,CAAC;CAC3B;AAED;;;;;;;GAOG;AACH,qBAAa,YAAY;IACrB,yEAAyE;IACzE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA0D;IACnF,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;gBAE7B,IAAI,EAAE,gBAAgB;IAIlC;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI;IAwBtD,2DAA2D;IAC3D,MAAM,IAAI,UAAU;IAQpB,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"}
|