@pothos/selection-mapper 0.1.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/.swcrc +10 -0
- package/LICENSE +6 -0
- package/README.md +92 -0
- package/dts/adapter.d.ts +74 -0
- package/dts/adapter.d.ts.map +1 -0
- package/dts/deep-equal.d.ts +7 -0
- package/dts/deep-equal.d.ts.map +1 -0
- package/dts/index.d.ts +13 -0
- package/dts/index.d.ts.map +1 -0
- package/dts/loader-map.d.ts +63 -0
- package/dts/loader-map.d.ts.map +1 -0
- package/dts/matches.d.ts +113 -0
- package/dts/matches.d.ts.map +1 -0
- package/dts/node.d.ts +85 -0
- package/dts/node.d.ts.map +1 -0
- package/dts/plan.d.ts +176 -0
- package/dts/plan.d.ts.map +1 -0
- package/dts/types.d.ts +97 -0
- package/dts/types.d.ts.map +1 -0
- package/dts/walk.d.ts +24 -0
- package/dts/walk.d.ts.map +1 -0
- package/esm/adapter.d.ts +74 -0
- package/esm/adapter.d.ts.map +1 -0
- package/esm/adapter.js +47 -0
- package/esm/adapter.js.map +1 -0
- package/esm/deep-equal.d.ts +7 -0
- package/esm/deep-equal.d.ts.map +1 -0
- package/esm/deep-equal.js +50 -0
- package/esm/deep-equal.js.map +1 -0
- package/esm/index.d.ts +13 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +12 -0
- package/esm/index.js.map +1 -0
- package/esm/loader-map.d.ts +63 -0
- package/esm/loader-map.d.ts.map +1 -0
- package/esm/loader-map.js +197 -0
- package/esm/loader-map.js.map +1 -0
- package/esm/matches.d.ts +113 -0
- package/esm/matches.d.ts.map +1 -0
- package/esm/matches.js +419 -0
- package/esm/matches.js.map +1 -0
- package/esm/node.d.ts +85 -0
- package/esm/node.d.ts.map +1 -0
- package/esm/node.js +232 -0
- package/esm/node.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/plan.d.ts +176 -0
- package/esm/plan.d.ts.map +1 -0
- package/esm/plan.js +339 -0
- package/esm/plan.js.map +1 -0
- package/esm/types.d.ts +97 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +10 -0
- package/esm/types.js.map +1 -0
- package/esm/walk.d.ts +24 -0
- package/esm/walk.d.ts.map +1 -0
- package/esm/walk.js +442 -0
- package/esm/walk.js.map +1 -0
- package/lib/adapter.js +43 -0
- package/lib/adapter.js.map +1 -0
- package/lib/deep-equal.js +60 -0
- package/lib/deep-equal.js.map +1 -0
- package/lib/index.js +57 -0
- package/lib/index.js.map +1 -0
- package/lib/loader-map.js +209 -0
- package/lib/loader-map.js.map +1 -0
- package/lib/matches.js +427 -0
- package/lib/matches.js.map +1 -0
- package/lib/node.js +245 -0
- package/lib/node.js.map +1 -0
- package/lib/plan.js +349 -0
- package/lib/plan.js.map +1 -0
- package/lib/types.js +9 -0
- package/lib/types.js.map +1 -0
- package/lib/walk.js +455 -0
- package/lib/walk.js.map +1 -0
- package/package.json +55 -0
- package/src/adapter.ts +96 -0
- package/src/deep-equal.ts +67 -0
- package/src/index.ts +32 -0
- package/src/loader-map.ts +287 -0
- package/src/matches.ts +681 -0
- package/src/node.ts +327 -0
- package/src/plan.ts +454 -0
- package/src/types.ts +123 -0
- package/src/walk.ts +696 -0
package/src/node.ts
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The node one root accumulates into — a model, its arguments, its columns, its relations and its
|
|
3
|
+
* computed values — and the merge, compare and conflict rules over a tree of them, shared by every
|
|
4
|
+
* adapter whose query is a tree of that shape.
|
|
5
|
+
*
|
|
6
|
+
* A subclass supplies the only format-specific parts: `visitQuery`, the key loop of the ORM's own
|
|
7
|
+
* query, and `toQuery`, the node written back. The traversal never reaches in here — it drives the
|
|
8
|
+
* `Adapter` contract, of which this implements everything but those two.
|
|
9
|
+
*/
|
|
10
|
+
import { isThenable, PothosValidationError } from '@pothos/core';
|
|
11
|
+
import { Adapter, type NodeBase } from './adapter.js';
|
|
12
|
+
import { deepEqual } from './deep-equal.js';
|
|
13
|
+
import type { MergeOptions, TypeLevelConflict } from './types.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One level of the query being built, used by the prisma and drizzle adapters: the model, its
|
|
17
|
+
* arguments, the columns it selects (`null` = every column, which is final: a node never goes
|
|
18
|
+
* back to named columns), its relations and its computed values (prisma `_count` keys, drizzle
|
|
19
|
+
* `extras`). An adapter whose query is not a tree of this shape extends `Adapter` directly.
|
|
20
|
+
*/
|
|
21
|
+
export interface Node<Model> extends NodeBase<Model> {
|
|
22
|
+
args: object;
|
|
23
|
+
columns: Set<string> | null;
|
|
24
|
+
relations: Map<string, Node<Model>>;
|
|
25
|
+
computed: Map<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createNode<Model>(model: Model): Node<Model> {
|
|
29
|
+
return { model, args: {}, columns: new Set(), relations: new Map(), computed: new Map() };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The only way to obtain a child node: get the relation's node or create it. `value` is what the
|
|
34
|
+
* map holds for the relation, checked here because a thenable is an un-awaited
|
|
35
|
+
* `nestedSelection()` that would otherwise be spread as an empty object.
|
|
36
|
+
*/
|
|
37
|
+
export function relation<Model>(
|
|
38
|
+
node: Node<Model>,
|
|
39
|
+
name: string,
|
|
40
|
+
model: Model,
|
|
41
|
+
value: unknown,
|
|
42
|
+
): Node<Model> {
|
|
43
|
+
if (isThenable(value)) {
|
|
44
|
+
throw new PothosValidationError(
|
|
45
|
+
`Relation "${name}" was given a promise. Await nestedSelection() (or a helper built on it, such as getQuery) inside an async selection function.`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let child = node.relations.get(name);
|
|
50
|
+
|
|
51
|
+
if (!child) {
|
|
52
|
+
child = createNode(model);
|
|
53
|
+
node.relations.set(name, child);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return child;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* What an adapter reports for one key of its query. Called by `NodeAdapter.visitQuery`; the
|
|
61
|
+
* visitor decides what to do with the key, so merging, checking and conflict-finding share one
|
|
62
|
+
* key loop per adapter instead of one each.
|
|
63
|
+
*
|
|
64
|
+
* A visitor is re-entrant: `relation` reads the nested query with the same visitor, so a
|
|
65
|
+
* `visitQuery` must hold no state of its own across a callback.
|
|
66
|
+
*/
|
|
67
|
+
export interface QueryVisitor<Model, Query> {
|
|
68
|
+
/** A named column of the model. */
|
|
69
|
+
column(name: string): void;
|
|
70
|
+
/** Every column. Final — a node never goes back to named columns. */
|
|
71
|
+
allColumns(): void;
|
|
72
|
+
/** A relation, with the model it targets and the query selected beneath it. */
|
|
73
|
+
relation(name: string, model: Model, query: Query): void;
|
|
74
|
+
/**
|
|
75
|
+
* A value the ORM computes per row, neither a column nor a relation (prisma `_count` keys,
|
|
76
|
+
* drizzle `extras`). `kind` is what a conflict on it is called to a user of that ORM, whose own
|
|
77
|
+
* word for these is not the shared one (prisma's counts read as a relation).
|
|
78
|
+
*/
|
|
79
|
+
computed(name: string, value: unknown, kind?: TypeLevelConflict['kind']): void;
|
|
80
|
+
/**
|
|
81
|
+
* The query arguments, normalized. A non-empty set replaces whatever the node holds. Reported
|
|
82
|
+
* on every pass, empty or not, since a merge check compares a query's arguments to the node's
|
|
83
|
+
* whether or not the query has any.
|
|
84
|
+
*/
|
|
85
|
+
args(args: object): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The adapter the prisma and drizzle adapters extend: the node tree above with every merge,
|
|
90
|
+
* compare and conflict rule this package owns. A subclass answers `visitQuery` and `toQuery`, plus
|
|
91
|
+
* the three translation members of `Adapter`.
|
|
92
|
+
*/
|
|
93
|
+
export abstract class NodeAdapter<Model, Query> extends Adapter<Model, Query, Node<Model>> {
|
|
94
|
+
/**
|
|
95
|
+
* One merger and one merge check per adapter, re-used down the tree: each saves and restores
|
|
96
|
+
* the node it is at rather than allocating a visitor per level, so a merge allocates only what
|
|
97
|
+
* the subclass's own `visitQuery` does. They cannot be this object — a lenient merge runs a
|
|
98
|
+
* check inside itself, so one `this` could not hold both cursors.
|
|
99
|
+
*/
|
|
100
|
+
private readonly merger: Merger<Model, Query> = new Merger(this);
|
|
101
|
+
private readonly mergeCheck: MergeCheck<Model, Query> = new MergeCheck(this);
|
|
102
|
+
|
|
103
|
+
/** Every key of `query`, read against `model` and reported to `visit`. The ORM's key loop. */
|
|
104
|
+
abstract visitQuery(query: Query, model: Model, visit: QueryVisitor<Model, Query>): void;
|
|
105
|
+
|
|
106
|
+
createNode(model: Model): Node<Model> {
|
|
107
|
+
return createNode(model);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
mergeQuery(node: Node<Model>, query: Query, options?: MergeOptions): void {
|
|
111
|
+
this.merger.run(node, query, options);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
override canMergeQuery(node: Node<Model>, query: Query, options?: MergeOptions): boolean {
|
|
115
|
+
return this.mergeCheck.run(node, query, options);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Whether `value` cannot join `computed` under `name`. A `deepEqual` against the value already
|
|
120
|
+
* there, which is what an adapter whose computed values are plain values wants; drizzle
|
|
121
|
+
* compares its `extras` by identity, and prisma overrides it for the `_count: true` wildcard.
|
|
122
|
+
*/
|
|
123
|
+
computedConflicts(computed: ReadonlyMap<string, unknown>, name: string, value: unknown): boolean {
|
|
124
|
+
return computed.has(name) && !deepEqual(computed.get(name), value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The first top-level key of `query` that conflicts with `node`. Top-level only, so the error
|
|
129
|
+
* names the key a user wrote rather than something nested beneath it.
|
|
130
|
+
*/
|
|
131
|
+
override firstConflict(node: Node<Model>, query: Query): TypeLevelConflict | undefined {
|
|
132
|
+
let found: TypeLevelConflict | undefined;
|
|
133
|
+
|
|
134
|
+
this.visitQuery(query, node.model, {
|
|
135
|
+
column() {},
|
|
136
|
+
allColumns() {},
|
|
137
|
+
args() {},
|
|
138
|
+
relation: (name, _model, nested) => {
|
|
139
|
+
const child = node.relations.get(name);
|
|
140
|
+
|
|
141
|
+
if (!found && child && !new MergeCheck(this).run(child, nested)) {
|
|
142
|
+
found = { kind: 'relation', name };
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
computed: (name, value, kind = 'extra') => {
|
|
146
|
+
if (!found && this.computedConflicts(node.computed, name, value)) {
|
|
147
|
+
found = { kind, name };
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
return found;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Node to node, without the serialize/merge round trip. */
|
|
156
|
+
override mergeNode(into: Node<Model>, from: Node<Model>): void {
|
|
157
|
+
if (from.columns === null) {
|
|
158
|
+
into.columns = null;
|
|
159
|
+
} else if (into.columns) {
|
|
160
|
+
for (const column of from.columns) {
|
|
161
|
+
into.columns.add(column);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (hasKeys(from.args)) {
|
|
166
|
+
into.args = from.args;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
for (const [name, value] of from.computed) {
|
|
170
|
+
into.computed.set(name, value);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
for (const [name, child] of from.relations) {
|
|
174
|
+
this.mergeNode(relation(into, name, child.model, undefined), child);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** `canMergeQuery` node to node, likewise. */
|
|
179
|
+
override canMergeNode(into: Node<Model>, from: Node<Model>): boolean {
|
|
180
|
+
if (!deepEqual(into.args, from.args)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
for (const [name, value] of from.computed) {
|
|
185
|
+
if (this.computedConflicts(into.computed, name, value)) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (const [name, child] of from.relations) {
|
|
191
|
+
const existing = into.relations.get(name);
|
|
192
|
+
|
|
193
|
+
if (existing && !this.canMergeNode(existing, child)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** One query folded into a node, in place. */
|
|
203
|
+
class Merger<Model, Query> implements QueryVisitor<Model, Query> {
|
|
204
|
+
private node!: Node<Model>;
|
|
205
|
+
/** A relation query must add no columns; the plan beneath it adds the ones it needs. */
|
|
206
|
+
private asQuery = false;
|
|
207
|
+
/** A conflicting relation or computed value is left out, not merged. Top level only. */
|
|
208
|
+
private lenient = false;
|
|
209
|
+
|
|
210
|
+
constructor(private readonly adapter: NodeAdapter<Model, Query>) {}
|
|
211
|
+
|
|
212
|
+
run(node: Node<Model>, query: Query, options?: MergeOptions): void {
|
|
213
|
+
this.node = node;
|
|
214
|
+
this.asQuery = options?.asQuery ?? false;
|
|
215
|
+
this.lenient = options?.lenient ?? false;
|
|
216
|
+
this.adapter.visitQuery(query, node.model, this);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
column(name: string) {
|
|
220
|
+
this.node.columns?.add(name);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
allColumns() {
|
|
224
|
+
if (!this.asQuery) {
|
|
225
|
+
this.node.columns = null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
relation(name: string, model: Model, query: Query) {
|
|
230
|
+
const parent = this.node;
|
|
231
|
+
|
|
232
|
+
if (this.lenient) {
|
|
233
|
+
const existing = parent.relations.get(name);
|
|
234
|
+
|
|
235
|
+
// The subtree is checked whole, so once the relation is accepted it merges whole.
|
|
236
|
+
if (existing && !new MergeCheck(this.adapter).run(existing, query)) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const child = relation(parent, name, model, query);
|
|
242
|
+
const { asQuery, lenient } = this;
|
|
243
|
+
|
|
244
|
+
this.node = child;
|
|
245
|
+
this.asQuery = false;
|
|
246
|
+
this.lenient = false;
|
|
247
|
+
this.adapter.visitQuery(query, model, this);
|
|
248
|
+
this.node = parent;
|
|
249
|
+
this.asQuery = asQuery;
|
|
250
|
+
this.lenient = lenient;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
computed(name: string, value: unknown) {
|
|
254
|
+
if (this.lenient && this.adapter.computedConflicts(this.node.computed, name, value)) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.node.computed.set(name, value);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
args(args: object) {
|
|
262
|
+
if (hasKeys(args)) {
|
|
263
|
+
this.node.args = args;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Whether a query merges into a node without changing what is already selected. Runs the whole
|
|
270
|
+
* query even after the first failure (a rejection is the rare path) so a `visitQuery` stays a
|
|
271
|
+
* plain loop.
|
|
272
|
+
*/
|
|
273
|
+
class MergeCheck<Model, Query> implements QueryVisitor<Model, Query> {
|
|
274
|
+
private node!: Node<Model>;
|
|
275
|
+
private ignoreArgs = false;
|
|
276
|
+
private ok = true;
|
|
277
|
+
|
|
278
|
+
constructor(private readonly adapter: NodeAdapter<Model, Query>) {}
|
|
279
|
+
|
|
280
|
+
run(node: Node<Model>, query: Query, options?: MergeOptions) {
|
|
281
|
+
this.node = node;
|
|
282
|
+
this.ignoreArgs = options?.ignoreArgs ?? false;
|
|
283
|
+
this.ok = true;
|
|
284
|
+
this.adapter.visitQuery(query, node.model, this);
|
|
285
|
+
|
|
286
|
+
return this.ok;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
column() {}
|
|
290
|
+
|
|
291
|
+
allColumns() {}
|
|
292
|
+
|
|
293
|
+
relation(name: string, model: Model, query: Query) {
|
|
294
|
+
const child = this.node.relations.get(name);
|
|
295
|
+
|
|
296
|
+
if (!this.ok || !child) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const parent = this.node;
|
|
301
|
+
const { ignoreArgs } = this;
|
|
302
|
+
|
|
303
|
+
// Below the top, a relation's own arguments are part of what is already selected.
|
|
304
|
+
this.node = child;
|
|
305
|
+
this.ignoreArgs = false;
|
|
306
|
+
this.adapter.visitQuery(query, model, this);
|
|
307
|
+
this.node = parent;
|
|
308
|
+
this.ignoreArgs = ignoreArgs;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
computed(name: string, value: unknown) {
|
|
312
|
+
if (this.ok && this.adapter.computedConflicts(this.node.computed, name, value)) {
|
|
313
|
+
this.ok = false;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
args(args: object) {
|
|
318
|
+
if (this.ok && !this.ignoreArgs && !deepEqual(this.node.args, args)) {
|
|
319
|
+
this.ok = false;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Whether an object has any own enumerable key. */
|
|
325
|
+
function hasKeys(value: object) {
|
|
326
|
+
return Object.keys(value).length > 0;
|
|
327
|
+
}
|