@prisma/param-graph-builder 7.4.0-integration-parameterization.5 → 7.4.0-integration-parameterization.7

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.
@@ -1,13 +1,25 @@
1
1
  /**
2
- * Builds a ParamGraph from DMMF at client generation time.
2
+ * Entry point for building ParamGraph from DMMF.
3
3
  *
4
- * The ParamGraph is a compact data structure that enables schema-aware
5
- * parameterization at runtime. It only stores parameterizable paths,
6
- * and uses a string table to de-duplicate field names.
4
+ * This module provides the main API for client generators to build
5
+ * parameterization schemas from DMMF at generation time.
7
6
  */
8
7
  import type * as DMMF from '@prisma/dmmf';
9
- import { ParamGraph } from '@prisma/param-graph';
8
+ import type { ParamGraphData, SerializedParamGraph } from '@prisma/param-graph';
10
9
  /**
11
- * Builds a ParamGraph from DMMF schema.
10
+ * Builds a ParamGraphData from DMMF schema.
11
+ *
12
+ * @param dmmf - The DMMF document from the schema
13
+ * @returns The param graph data structure
14
+ */
15
+ export declare function buildParamGraph(dmmf: DMMF.Document): ParamGraphData;
16
+ /**
17
+ * Builds and serializes a ParamGraph from DMMF schema.
18
+ *
19
+ * This is the primary API for generators - it returns the compact
20
+ * serialized format ready to be embedded in generated clients.
21
+ *
22
+ * @param dmmf - The DMMF document from the schema
23
+ * @returns The serialized param graph
12
24
  */
13
- export declare function buildParamGraph(dmmf: DMMF.Document): ParamGraph;
25
+ export declare function buildAndSerializeParamGraph(dmmf: DMMF.Document): SerializedParamGraph;
@@ -18,414 +18,26 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var build_param_graph_exports = {};
20
20
  __export(build_param_graph_exports, {
21
+ buildAndSerializeParamGraph: () => buildAndSerializeParamGraph,
21
22
  buildParamGraph: () => buildParamGraph
22
23
  });
23
24
  module.exports = __toCommonJS(build_param_graph_exports);
24
- var import_dmmf = require("@prisma/dmmf");
25
- var import_param_graph = require("@prisma/param-graph");
26
- class ParamGraphBuilder {
27
- stringTable = [];
28
- stringToIndex = /* @__PURE__ */ new Map();
29
- enumNames = [];
30
- enumToIndex = /* @__PURE__ */ new Map();
31
- inputNodes = [];
32
- outputNodes = [];
33
- roots = {};
34
- inputTypeNodeCache = /* @__PURE__ */ new Map();
35
- unionNodeCache = /* @__PURE__ */ new Map();
36
- outputTypeNodeCache = /* @__PURE__ */ new Map();
37
- /**
38
- * Interns a string into the string table, returning its index.
39
- */
40
- internString(str) {
41
- let index = this.stringToIndex.get(str);
42
- if (index === void 0) {
43
- index = this.stringTable.length;
44
- this.stringTable.push(str);
45
- this.stringToIndex.set(str, index);
46
- }
47
- return index;
48
- }
49
- /**
50
- * Registers a user enum name, returning its index.
51
- */
52
- registerEnum(enumName) {
53
- let index = this.enumToIndex.get(enumName);
54
- if (index === void 0) {
55
- index = this.enumNames.length;
56
- this.enumNames.push(enumName);
57
- this.enumToIndex.set(enumName, index);
58
- }
59
- return index;
60
- }
61
- /**
62
- * Allocates a new input node and returns its ID.
63
- */
64
- allocateInputNode() {
65
- const id = this.inputNodes.length;
66
- this.inputNodes.push({});
67
- return id;
68
- }
69
- /**
70
- * Sets fields on an input node.
71
- */
72
- setInputNodeFields(nodeId, fields) {
73
- if (Object.keys(fields).length > 0) {
74
- this.inputNodes[nodeId].f = fields;
75
- }
76
- }
77
- /**
78
- * Allocates a new output node and returns its ID.
79
- */
80
- allocateOutputNode() {
81
- const id = this.outputNodes.length;
82
- this.outputNodes.push({});
83
- return id;
84
- }
85
- /**
86
- * Sets fields on an output node.
87
- */
88
- setOutputNodeFields(nodeId, fields) {
89
- if (Object.keys(fields).length > 0) {
90
- this.outputNodes[nodeId].f = fields;
91
- }
92
- }
93
- /**
94
- * Records a root entry for an operation.
95
- */
96
- setRoot(key, entry) {
97
- if (entry.a !== void 0 || entry.o !== void 0) {
98
- this.roots[key] = entry;
99
- }
100
- }
101
- /**
102
- * Gets or sets a cached input type node ID.
103
- */
104
- getInputTypeNode(typeName) {
105
- return this.inputTypeNodeCache.get(typeName);
106
- }
107
- setInputTypeNode(typeName, nodeId) {
108
- this.inputTypeNodeCache.set(typeName, nodeId);
109
- }
110
- hasInputTypeNode(typeName) {
111
- return this.inputTypeNodeCache.has(typeName);
112
- }
113
- /**
114
- * Gets or sets a cached union node ID.
115
- */
116
- getUnionNode(key) {
117
- return this.unionNodeCache.get(key);
118
- }
119
- setUnionNode(key, nodeId) {
120
- this.unionNodeCache.set(key, nodeId);
121
- }
122
- hasUnionNode(key) {
123
- return this.unionNodeCache.has(key);
124
- }
125
- /**
126
- * Gets or sets a cached output type node ID.
127
- */
128
- getOutputTypeNode(typeName) {
129
- return this.outputTypeNodeCache.get(typeName);
130
- }
131
- setOutputTypeNode(typeName, nodeId) {
132
- this.outputTypeNodeCache.set(typeName, nodeId);
133
- }
134
- hasOutputTypeNode(typeName) {
135
- return this.outputTypeNodeCache.has(typeName);
136
- }
137
- /**
138
- * Builds the final ParamGraph.
139
- */
140
- build() {
141
- return {
142
- s: this.stringTable,
143
- e: this.enumNames,
144
- i: this.inputNodes,
145
- o: this.outputNodes,
146
- r: this.roots
147
- };
148
- }
149
- }
25
+ var import_dmmf_traverser = require("./dmmf-traverser");
26
+ var import_param_graph_builder = require("./param-graph-builder");
150
27
  function buildParamGraph(dmmf) {
151
- const builder = new ParamGraphBuilder();
152
- const inputTypeMap = /* @__PURE__ */ new Map();
153
- const outputTypeMap = /* @__PURE__ */ new Map();
154
- const userEnumNames = /* @__PURE__ */ new Set();
155
- for (const e of dmmf.datamodel.enums) {
156
- userEnumNames.add(e.name);
157
- }
158
- for (const inputType of dmmf.schema.inputObjectTypes.prisma ?? []) {
159
- inputTypeMap.set(getTypeName(inputType.name, "prisma"), inputType);
160
- }
161
- for (const inputType of dmmf.schema.inputObjectTypes.model ?? []) {
162
- inputTypeMap.set(getTypeName(inputType.name, "model"), inputType);
163
- }
164
- for (const outputType of dmmf.schema.outputObjectTypes.prisma ?? []) {
165
- outputTypeMap.set(getTypeName(outputType.name, "prisma"), outputType);
166
- }
167
- for (const outputType of dmmf.schema.outputObjectTypes.model ?? []) {
168
- outputTypeMap.set(getTypeName(outputType.name, "model"), outputType);
169
- }
170
- function buildInputNodeFromArgs(args) {
171
- const fields = {};
172
- let hasAnyField = false;
173
- for (const arg of args) {
174
- const edge = buildInputEdge(arg);
175
- if (edge) {
176
- const stringIndex = builder.internString(arg.name);
177
- fields[stringIndex] = edge;
178
- hasAnyField = true;
179
- }
180
- }
181
- if (!hasAnyField) {
182
- return void 0;
183
- }
184
- const nodeId = builder.allocateInputNode();
185
- builder.setInputNodeFields(nodeId, fields);
186
- return nodeId;
187
- }
188
- function buildInputEdge(arg) {
189
- return mergeFieldVariants([arg]);
190
- }
191
- function buildInputTypeNode(typeName) {
192
- if (builder.hasInputTypeNode(typeName)) {
193
- return builder.getInputTypeNode(typeName);
194
- }
195
- const inputType = inputTypeMap.get(typeName);
196
- if (!inputType) {
197
- builder.setInputTypeNode(typeName, void 0);
198
- return void 0;
199
- }
200
- const nodeId = builder.allocateInputNode();
201
- builder.setInputTypeNode(typeName, nodeId);
202
- const fields = {};
203
- let hasAnyField = false;
204
- for (const field of inputType.fields) {
205
- const edge = buildInputEdge(field);
206
- if (edge) {
207
- const stringIndex = builder.internString(field.name);
208
- fields[stringIndex] = edge;
209
- hasAnyField = true;
210
- }
211
- }
212
- if (hasAnyField) {
213
- builder.setInputNodeFields(nodeId, fields);
214
- return nodeId;
215
- }
216
- return nodeId;
217
- }
218
- function buildUnionNode(typeNames) {
219
- const sortedNames = [...typeNames].sort();
220
- const cacheKey = sortedNames.join("|");
221
- if (builder.hasUnionNode(cacheKey)) {
222
- return builder.getUnionNode(cacheKey);
223
- }
224
- const nodeId = builder.allocateInputNode();
225
- builder.setUnionNode(cacheKey, nodeId);
226
- const fieldsByName = /* @__PURE__ */ new Map();
227
- for (const typeName of typeNames) {
228
- const inputType = inputTypeMap.get(typeName);
229
- if (!inputType) continue;
230
- for (const field of inputType.fields) {
231
- let fieldsForName = fieldsByName.get(field.name);
232
- if (!fieldsForName) {
233
- fieldsForName = [];
234
- fieldsByName.set(field.name, fieldsForName);
235
- }
236
- fieldsForName.push(field);
237
- }
238
- }
239
- const mergedFields = {};
240
- let hasAnyField = false;
241
- for (const [fieldName, variantFields] of fieldsByName) {
242
- const mergedEdge = mergeFieldVariants(variantFields);
243
- if (mergedEdge) {
244
- const stringIndex = builder.internString(fieldName);
245
- mergedFields[stringIndex] = mergedEdge;
246
- hasAnyField = true;
247
- }
248
- }
249
- if (hasAnyField) {
250
- builder.setInputNodeFields(nodeId, mergedFields);
251
- }
252
- return nodeId;
253
- }
254
- function mergeFieldVariants(variants) {
255
- let flags = 0;
256
- let scalarMask = 0;
257
- let childNodeId;
258
- let enumNameId;
259
- const scalarTypes = [];
260
- const enumTypes = [];
261
- const inputObjectTypes = [];
262
- for (const variant of variants) {
263
- for (const inputType of variant.inputTypes) {
264
- switch (inputType.location) {
265
- case "scalar":
266
- if (variant.isParameterizable) {
267
- scalarTypes.push(inputType);
268
- }
269
- break;
270
- case "enumTypes":
271
- if (variant.isParameterizable) {
272
- enumTypes.push(inputType);
273
- }
274
- break;
275
- case "inputObjectTypes":
276
- if (!inputObjectTypes.some(
277
- (ot) => ot.type === inputType.type && ot.namespace === inputType.namespace && ot.isList === inputType.isList
278
- )) {
279
- inputObjectTypes.push(inputType);
280
- }
281
- break;
282
- case "fieldRefTypes":
283
- break;
284
- default:
285
- inputType.location;
286
- }
287
- }
288
- }
289
- for (const st of scalarTypes) {
290
- scalarMask |= (0, import_param_graph.scalarTypeToMask)(st.type);
291
- if (st.isList) {
292
- flags |= import_param_graph.EdgeFlag.ParamListScalar;
293
- } else {
294
- flags |= import_param_graph.EdgeFlag.ParamScalar;
295
- }
296
- }
297
- for (const et of enumTypes) {
298
- if (et.namespace === "model") {
299
- enumNameId = builder.registerEnum(et.type);
300
- if (et.isList) {
301
- flags |= import_param_graph.EdgeFlag.ParamListEnum;
302
- } else {
303
- flags |= import_param_graph.EdgeFlag.ParamEnum;
304
- }
305
- break;
306
- }
307
- }
308
- if (inputObjectTypes.length > 0) {
309
- const hasObjectList = inputObjectTypes.some((iot) => iot.isList);
310
- const hasSingleObject = inputObjectTypes.some((iot) => !iot.isList);
311
- if (hasObjectList) {
312
- flags |= import_param_graph.EdgeFlag.ListObject;
313
- }
314
- if (hasSingleObject) {
315
- flags |= import_param_graph.EdgeFlag.Object;
316
- }
317
- if (inputObjectTypes.length === 1) {
318
- childNodeId = buildInputTypeNode(getTypeName(inputObjectTypes[0].type, inputObjectTypes[0].namespace));
319
- } else {
320
- childNodeId = buildUnionNode(inputObjectTypes.map((iot) => getTypeName(iot.type, iot.namespace)));
321
- }
322
- }
323
- if (flags === 0) {
324
- return void 0;
325
- }
326
- const edge = { k: flags };
327
- if (childNodeId !== void 0) {
328
- edge.c = childNodeId;
329
- }
330
- if (scalarMask !== 0) {
331
- edge.m = scalarMask;
332
- }
333
- if (enumNameId !== void 0) {
334
- edge.e = enumNameId;
335
- }
336
- return edge;
337
- }
338
- function buildOutputTypeNode(typeName) {
339
- if (builder.hasOutputTypeNode(typeName)) {
340
- return builder.getOutputTypeNode(typeName);
341
- }
342
- const outputType = outputTypeMap.get(typeName);
343
- if (!outputType) {
344
- builder.setOutputTypeNode(typeName, void 0);
345
- return void 0;
346
- }
347
- const nodeId = builder.allocateOutputNode();
348
- builder.setOutputTypeNode(typeName, nodeId);
349
- const fields = {};
350
- let hasAnyField = false;
351
- for (const field of outputType.fields) {
352
- const edge = buildOutputEdge(field);
353
- if (edge) {
354
- const stringIndex = builder.internString(field.name);
355
- fields[stringIndex] = edge;
356
- hasAnyField = true;
357
- }
358
- }
359
- if (hasAnyField) {
360
- builder.setOutputNodeFields(nodeId, fields);
361
- return nodeId;
362
- }
363
- return nodeId;
364
- }
365
- function buildOutputEdge(field) {
366
- let argsNodeId;
367
- let childOutputNodeId;
368
- if (field.args.length > 0) {
369
- argsNodeId = buildInputNodeFromArgs(field.args);
370
- }
371
- if (field.outputType.location === "outputObjectTypes") {
372
- childOutputNodeId = buildOutputTypeNode(getTypeName(field.outputType.type, field.outputType.namespace));
373
- }
374
- if (argsNodeId === void 0 && childOutputNodeId === void 0) {
375
- return void 0;
376
- }
377
- const edge = {};
378
- if (argsNodeId !== void 0) {
379
- edge.a = argsNodeId;
380
- }
381
- if (childOutputNodeId !== void 0) {
382
- edge.o = childOutputNodeId;
383
- }
384
- return edge;
385
- }
386
- for (const mapping of dmmf.mappings.modelOperations) {
387
- const modelName = mapping.model;
388
- const actions = Object.keys(import_dmmf.ModelAction);
389
- for (const action of actions) {
390
- const fieldName = mapping[action];
391
- if (!fieldName) continue;
392
- let rootField;
393
- const queryType = outputTypeMap.get("prisma.Query");
394
- if (queryType) {
395
- rootField = queryType.fields.find((f) => f.name === fieldName);
396
- }
397
- if (!rootField) {
398
- const mutationType = outputTypeMap.get("prisma.Mutation");
399
- if (mutationType) {
400
- rootField = mutationType.fields.find((f) => f.name === fieldName);
401
- }
402
- }
403
- if (!rootField) continue;
404
- const argsNodeId = buildInputNodeFromArgs(rootField.args);
405
- let outputNodeId;
406
- if (rootField.outputType.location === "outputObjectTypes") {
407
- outputNodeId = buildOutputTypeNode(getTypeName(rootField.outputType.type, rootField.outputType.namespace));
408
- }
409
- const dmmfActionToJsonAction = {
410
- create: "createOne",
411
- update: "updateOne",
412
- delete: "deleteOne",
413
- upsert: "upsertOne"
414
- };
415
- const jsonAction = dmmfActionToJsonAction[action] ?? action;
416
- const rootKey = `${modelName}.${jsonAction}`;
417
- builder.setRoot(rootKey, { a: argsNodeId, o: outputNodeId });
418
- }
419
- }
28
+ const builder = new import_param_graph_builder.ParamGraphBuilder();
29
+ const traverser = new import_dmmf_traverser.DMMFTraverser(builder, dmmf);
30
+ traverser.processRoots(dmmf.mappings.modelOperations);
420
31
  return builder.build();
421
32
  }
422
- function getTypeName(name, namespace) {
423
- if (namespace === void 0) {
424
- return name;
425
- }
426
- return `${namespace}.${name}`;
33
+ function buildAndSerializeParamGraph(dmmf) {
34
+ const builder = new import_param_graph_builder.ParamGraphBuilder();
35
+ const traverser = new import_dmmf_traverser.DMMFTraverser(builder, dmmf);
36
+ traverser.processRoots(dmmf.mappings.modelOperations);
37
+ return builder.buildAndSerialize();
427
38
  }
428
39
  // Annotate the CommonJS export names for ESM import in node:
429
40
  0 && (module.exports = {
41
+ buildAndSerializeParamGraph,
430
42
  buildParamGraph
431
43
  });