@prisma/sqlcommenter-query-insights 0.0.1 → 7.2.0-dev.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.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/dist/index.js CHANGED
@@ -26,19 +26,7 @@ module.exports = __toCommonJS(index_exports);
26
26
 
27
27
  // src/parameterize/parameterize.ts
28
28
  var PARAM_PLACEHOLDER = { $type: "Param" };
29
- function isTaggedValue(value) {
30
- return typeof value === "object" && value !== null && "$type" in value && typeof value.$type === "string";
31
- }
32
- function isSelectionMarker(key) {
33
- return key === "$scalars" || key === "$composites";
34
- }
35
- var STRUCTURE_KEYS = /* @__PURE__ */ new Set([
36
- // Selection/field specifiers
37
- "select",
38
- "include",
39
- "omit",
40
- "selection",
41
- // Relation operations
29
+ var RELATION_OPERATION_KEYS = /* @__PURE__ */ new Set([
42
30
  "connect",
43
31
  "connectOrCreate",
44
32
  "disconnect",
@@ -49,88 +37,61 @@ var STRUCTURE_KEYS = /* @__PURE__ */ new Set([
49
37
  "updateMany",
50
38
  "upsert",
51
39
  "delete",
52
- "deleteMany",
53
- // Query structure
40
+ "deleteMany"
41
+ ]);
42
+ var STRUCTURAL_KEYS_IN_DATA = /* @__PURE__ */ new Set([
54
43
  "where",
55
- "data",
56
- "orderBy",
57
- "cursor",
58
- "distinct",
59
- "relationLoadStrategy",
60
- // Aggregation
44
+ "select",
45
+ "include",
46
+ "omit",
61
47
  "_count",
62
48
  "_sum",
63
49
  "_avg",
64
50
  "_min",
65
51
  "_max"
66
52
  ]);
67
- var STRUCTURAL_VALUE_KEYS = /* @__PURE__ */ new Set([
68
- // Sort directions
69
- "sort",
70
- // Null handling
71
- "nulls",
72
- // Relation load strategy
73
- "relationLoadStrategy"
74
- ]);
75
- var NUMERIC_STRUCTURE_KEYS = /* @__PURE__ */ new Set(["take", "skip"]);
76
- var FILTER_OPERATORS = /* @__PURE__ */ new Set([
77
- "equals",
78
- "not",
79
- "in",
80
- "notIn",
81
- "lt",
82
- "lte",
83
- "gt",
84
- "gte",
85
- "contains",
86
- "startsWith",
87
- "endsWith",
88
- "search",
89
- "mode",
90
- "has",
91
- "hasEvery",
92
- "hasSome",
93
- "isEmpty",
94
- // JSON operators
95
- "path",
96
- "string_contains",
97
- "string_starts_with",
98
- "string_ends_with",
99
- "array_contains",
100
- "array_starts_with",
101
- "array_ends_with"
102
- ]);
103
- var LOGICAL_OPERATORS = /* @__PURE__ */ new Set(["AND", "OR", "NOT", "is", "isNot", "some", "every", "none"]);
104
- var SORT_DIRECTIONS = /* @__PURE__ */ new Set(["asc", "desc"]);
105
- function parameterizeValue(value, context) {
53
+ var STRUCTURAL_VALUE_KEYS = /* @__PURE__ */ new Set(["take", "skip", "sort", "nulls", "mode", "relationLoadStrategy", "distinct"]);
54
+ function isTaggedValue(value) {
55
+ return typeof value === "object" && value !== null && "$type" in value && typeof value.$type === "string";
56
+ }
57
+ function isSelectionMarker(key) {
58
+ return key === "$scalars" || key === "$composites";
59
+ }
60
+ function isSortDirection(value) {
61
+ return value === "asc" || value === "desc";
62
+ }
63
+ function getChildContext(key, parentContext) {
64
+ if (key === "arguments") return "default";
65
+ if (key === "selection") return "selection";
66
+ if (key === "orderBy") return "orderBy";
67
+ if (key === "data") return "data";
68
+ if (parentContext === "data") {
69
+ if (RELATION_OPERATION_KEYS.has(key) || STRUCTURAL_KEYS_IN_DATA.has(key)) {
70
+ return "default";
71
+ }
72
+ }
73
+ return parentContext;
74
+ }
75
+ function parameterize(value, context, key) {
106
76
  if (value === null || value === void 0) {
107
77
  return value;
108
78
  }
109
79
  if (isTaggedValue(value)) {
110
- if (value.$type === "FieldRef") {
111
- return value;
112
- }
113
- if (value.$type === "Enum" && context.isStructuralEnum) {
114
- return value;
115
- }
116
- return PARAM_PLACEHOLDER;
80
+ return value.$type === "FieldRef" ? value : PARAM_PLACEHOLDER;
117
81
  }
118
82
  if (Array.isArray(value)) {
119
- return value.map((item) => parameterizeValue(item, context));
83
+ return value.map((item) => parameterize(item, context, key));
120
84
  }
121
85
  if (typeof value === "object") {
122
86
  return parameterizeObject(value, context);
123
87
  }
124
- if (context.isDataContext) {
125
- return PARAM_PLACEHOLDER;
126
- }
127
- if (context.isSelectionContext && typeof value === "boolean") {
88
+ if (key && STRUCTURAL_VALUE_KEYS.has(key)) {
128
89
  return value;
129
90
  }
130
- if (context.isStructuralValue) {
91
+ if (context === "selection" && typeof value === "boolean") {
131
92
  return value;
132
93
  }
133
- if (context.isOrderByContext && typeof value === "string" && SORT_DIRECTIONS.has(value)) {
94
+ if (context === "orderBy" && typeof value === "string" && isSortDirection(value)) {
134
95
  return value;
135
96
  }
136
97
  return PARAM_PLACEHOLDER;
@@ -142,264 +103,82 @@ function parameterizeObject(obj, context) {
142
103
  result[key] = value;
143
104
  continue;
144
105
  }
145
- const newContext = getContextForKey(key, context);
146
- result[key] = parameterizeValue(value, newContext);
106
+ const childContext = getChildContext(key, context);
107
+ result[key] = parameterize(value, childContext, key);
147
108
  }
148
109
  return result;
149
110
  }
150
- function isStructuralKey(key) {
151
- return key === "arguments" || STRUCTURE_KEYS.has(key);
152
- }
153
- function getContextForKey(key, parentContext) {
154
- if (key === "arguments") {
155
- return {
156
- isDataContext: false,
157
- isStructuralValue: false,
158
- isStructuralEnum: false,
159
- isSelectionContext: false,
160
- isOrderByContext: false,
161
- parentKey: key
162
- };
163
- }
164
- if (key === "selection" || parentContext.isSelectionContext && !isStructuralKey(key)) {
165
- return {
166
- isDataContext: false,
167
- isStructuralValue: false,
168
- isStructuralEnum: false,
169
- isSelectionContext: true,
170
- isOrderByContext: false,
171
- parentKey: key
172
- };
173
- }
174
- if (key === "data" || parentContext.isDataContext) {
175
- if (STRUCTURE_KEYS.has(key)) {
176
- return {
177
- isDataContext: false,
178
- isStructuralValue: false,
179
- isStructuralEnum: false,
180
- isSelectionContext: false,
181
- isOrderByContext: false,
182
- parentKey: key
183
- };
184
- }
185
- return {
186
- isDataContext: true,
187
- isStructuralValue: false,
188
- isStructuralEnum: false,
189
- isSelectionContext: false,
190
- isOrderByContext: false,
191
- parentKey: key
192
- };
193
- }
194
- if (STRUCTURAL_VALUE_KEYS.has(key)) {
195
- return {
196
- isDataContext: false,
197
- isStructuralValue: true,
198
- isStructuralEnum: true,
199
- isSelectionContext: false,
200
- isOrderByContext: false,
201
- parentKey: key
202
- };
203
- }
204
- if (NUMERIC_STRUCTURE_KEYS.has(key)) {
205
- return {
206
- isDataContext: false,
207
- isStructuralValue: true,
208
- isStructuralEnum: false,
209
- isSelectionContext: false,
210
- isOrderByContext: false,
211
- parentKey: key
212
- };
213
- }
214
- if (key === "orderBy" || parentContext.isOrderByContext) {
215
- return {
216
- isDataContext: false,
217
- isStructuralValue: false,
218
- isStructuralEnum: false,
219
- isSelectionContext: false,
220
- isOrderByContext: true,
221
- parentKey: key
222
- };
223
- }
224
- if (FILTER_OPERATORS.has(key)) {
225
- if (key === "mode") {
226
- return {
227
- isDataContext: false,
228
- isStructuralValue: true,
229
- isStructuralEnum: true,
230
- isSelectionContext: false,
231
- isOrderByContext: false,
232
- parentKey: key
233
- };
234
- }
235
- return {
236
- isDataContext: false,
237
- isStructuralValue: false,
238
- isStructuralEnum: false,
239
- isSelectionContext: false,
240
- isOrderByContext: false,
241
- parentKey: key
242
- };
243
- }
244
- if (LOGICAL_OPERATORS.has(key)) {
245
- return {
246
- isDataContext: false,
247
- isStructuralValue: false,
248
- isStructuralEnum: false,
249
- isSelectionContext: false,
250
- isOrderByContext: false,
251
- parentKey: key
252
- };
253
- }
254
- if (STRUCTURE_KEYS.has(key)) {
255
- return {
256
- isDataContext: false,
257
- isStructuralValue: false,
258
- isStructuralEnum: false,
259
- isSelectionContext: key === "selection",
260
- isOrderByContext: false,
261
- parentKey: key
262
- };
263
- }
264
- return {
265
- isDataContext: parentContext.isDataContext,
266
- isStructuralValue: false,
267
- isStructuralEnum: false,
268
- isSelectionContext: parentContext.isSelectionContext,
269
- isOrderByContext: parentContext.isOrderByContext,
270
- parentKey: key
271
- };
272
- }
273
111
  function parameterizeQuery(query) {
274
- const initialContext = {
275
- isDataContext: false,
276
- isStructuralValue: false,
277
- isStructuralEnum: false,
278
- isSelectionContext: false,
279
- isOrderByContext: false
280
- };
281
- return parameterizeValue(query, initialContext);
112
+ return parameterize(query, "default");
282
113
  }
283
114
 
284
115
  // src/shape/shape.ts
285
- function isNestedQuery(value) {
286
- return typeof value === "object" && value !== null && ("arguments" in value || "selection" in value);
287
- }
288
- function isEmptyObject(value) {
289
- return typeof value === "object" && value !== null && Object.keys(value).length === 0;
290
- }
291
- function canSimplifySelectionToTrue(selection) {
292
- if (selection.$scalars !== true) {
293
- return false;
294
- }
295
- for (const key of Object.keys(selection)) {
296
- if (key !== "$scalars" && key !== "$composites") {
297
- return false;
298
- }
299
- }
300
- return true;
301
- }
302
- function canSimplifyNestedQueryToTrue(args, selection) {
303
- const argsEmpty = args === void 0 || isEmptyObject(args);
304
- if (!argsEmpty) {
305
- return false;
306
- }
307
- if (!selection) {
308
- return false;
309
- }
310
- return canSimplifySelectionToTrue(selection);
311
- }
312
- function shapeRelation(relation) {
313
- if (relation === true) {
314
- return true;
315
- }
316
- if (typeof relation !== "object" || relation === null) {
317
- return relation;
318
- }
319
- const relationObj = relation;
320
- if (isNestedQuery(relationObj)) {
321
- const args = relationObj.arguments;
322
- const selection = relationObj.selection;
323
- if (canSimplifyNestedQueryToTrue(args, selection)) {
116
+ function isObject(value) {
117
+ return typeof value === "object" && value !== null && !Array.isArray(value);
118
+ }
119
+ function isEmpty(obj) {
120
+ return !obj || Object.keys(obj).length === 0;
121
+ }
122
+ function isSimpleSelection(selection) {
123
+ if (!selection || selection.$scalars !== true) return false;
124
+ return Object.keys(selection).every((k) => k === "$scalars" || k === "$composites");
125
+ }
126
+ function shapeValue(value) {
127
+ if (value === true || !isObject(value)) return value;
128
+ if ("arguments" in value || "selection" in value) {
129
+ const args = value.arguments;
130
+ const selection = value.selection;
131
+ if (isEmpty(args) && isSimpleSelection(selection)) {
324
132
  return true;
325
133
  }
326
- return shapeQuery(relationObj);
327
- } else {
328
- if (canSimplifySelectionToTrue(relationObj)) {
329
- return true;
330
- }
331
- const shaped = shapeSelectionObject(relationObj);
332
- return shaped || true;
134
+ return shapeQuery(value);
333
135
  }
136
+ return isSimpleSelection(value) ? true : shapeQuery({ selection: value });
334
137
  }
335
- function shapeSelectionObject(selection) {
138
+ function shapeQuery(query) {
139
+ if (!isObject(query)) return query;
140
+ const args = query.arguments;
141
+ const selection = query.selection;
142
+ const result = args ? { ...args } : {};
143
+ if (!selection) return result;
336
144
  const hasScalars = selection.$scalars === true;
337
- const booleanFields = {};
338
- const objectFields = {};
145
+ const fields = {};
339
146
  for (const [key, value] of Object.entries(selection)) {
340
- if (key === "$scalars" || key === "$composites") {
341
- continue;
342
- }
343
- if (typeof value === "boolean") {
344
- booleanFields[key] = value;
345
- } else if (typeof value === "object" && value !== null) {
346
- objectFields[key] = value;
347
- }
147
+ if (key === "$scalars" || key === "$composites") continue;
148
+ if (hasScalars && typeof value === "boolean") continue;
149
+ fields[key] = typeof value === "boolean" ? value : shapeValue(value);
348
150
  }
349
- const hasBooleanFields = Object.keys(booleanFields).length > 0;
350
- const hasObjectFields = Object.keys(objectFields).length > 0;
351
- if (!hasBooleanFields && !hasObjectFields) {
352
- return null;
353
- }
354
- const shapedObjects = {};
355
- for (const [key, value] of Object.entries(objectFields)) {
356
- shapedObjects[key] = shapeRelation(value);
357
- }
358
- if (hasScalars) {
359
- if (!hasObjectFields) {
360
- return null;
361
- }
362
- return { include: shapedObjects };
363
- } else {
364
- return { select: { ...booleanFields, ...shapedObjects } };
365
- }
366
- }
367
- function shapeQuery(query) {
368
- if (typeof query !== "object" || query === null) {
369
- return query;
370
- }
371
- const queryObj = query;
372
- const args = queryObj.arguments;
373
- const selection = queryObj.selection;
374
- const result = args ? { ...args } : {};
375
- if (selection) {
376
- const shaped = shapeSelectionObject(selection);
377
- if (shaped) {
378
- Object.assign(result, shaped);
379
- }
151
+ if (Object.keys(fields).length > 0) {
152
+ result[hasScalars ? "include" : "select"] = fields;
380
153
  }
381
154
  return result;
382
155
  }
383
156
 
384
- // src/format.ts
157
+ // src/format/format.ts
385
158
  function toBase64Url(data) {
386
159
  return Buffer.from(data, "utf-8").toString("base64url");
387
160
  }
388
161
  function formatQueryInsight(queryInfo) {
389
- if (queryInfo.type === "single") {
390
- const { modelName: modelName2, action: action2, query } = queryInfo;
391
- if (!modelName2) {
392
- return action2;
162
+ switch (queryInfo.type) {
163
+ case "single": {
164
+ const { modelName, action, query } = queryInfo;
165
+ if (!modelName) {
166
+ return action;
167
+ }
168
+ const parameterizedQuery = parameterizeQuery(query);
169
+ const shapedQuery = shapeQuery(parameterizedQuery);
170
+ const encoded = toBase64Url(JSON.stringify(shapedQuery));
171
+ return `${modelName}.${action}:${encoded}`;
172
+ }
173
+ case "compacted": {
174
+ const { modelName, action, queries } = queryInfo;
175
+ const shapedQueries = queries.map((q) => shapeQuery(parameterizeQuery(q)));
176
+ const encoded = toBase64Url(JSON.stringify(shapedQueries));
177
+ return `${modelName}.${action}:${encoded}`;
393
178
  }
394
- const parameterizedQuery = parameterizeQuery(query);
395
- const shapedQuery = shapeQuery(parameterizedQuery);
396
- const encoded2 = toBase64Url(JSON.stringify(shapedQuery));
397
- return `${modelName2}.${action2}:${encoded2}`;
179
+ default:
180
+ throw new Error(`invalid queryInfo.type=${queryInfo.type}`);
398
181
  }
399
- const { modelName, action, queries } = queryInfo;
400
- const shapedQueries = queries.map((q) => shapeQuery(parameterizeQuery(q)));
401
- const encoded = toBase64Url(JSON.stringify(shapedQueries));
402
- return `${modelName}.${action}:${encoded}`;
403
182
  }
404
183
 
405
184
  // src/index.ts
package/dist/index.mjs CHANGED
@@ -1,18 +1,6 @@
1
1
  // src/parameterize/parameterize.ts
2
2
  var PARAM_PLACEHOLDER = { $type: "Param" };
3
- function isTaggedValue(value) {
4
- return typeof value === "object" && value !== null && "$type" in value && typeof value.$type === "string";
5
- }
6
- function isSelectionMarker(key) {
7
- return key === "$scalars" || key === "$composites";
8
- }
9
- var STRUCTURE_KEYS = /* @__PURE__ */ new Set([
10
- // Selection/field specifiers
11
- "select",
12
- "include",
13
- "omit",
14
- "selection",
15
- // Relation operations
3
+ var RELATION_OPERATION_KEYS = /* @__PURE__ */ new Set([
16
4
  "connect",
17
5
  "connectOrCreate",
18
6
  "disconnect",
@@ -23,88 +11,61 @@ var STRUCTURE_KEYS = /* @__PURE__ */ new Set([
23
11
  "updateMany",
24
12
  "upsert",
25
13
  "delete",
26
- "deleteMany",
27
- // Query structure
14
+ "deleteMany"
15
+ ]);
16
+ var STRUCTURAL_KEYS_IN_DATA = /* @__PURE__ */ new Set([
28
17
  "where",
29
- "data",
30
- "orderBy",
31
- "cursor",
32
- "distinct",
33
- "relationLoadStrategy",
34
- // Aggregation
18
+ "select",
19
+ "include",
20
+ "omit",
35
21
  "_count",
36
22
  "_sum",
37
23
  "_avg",
38
24
  "_min",
39
25
  "_max"
40
26
  ]);
41
- var STRUCTURAL_VALUE_KEYS = /* @__PURE__ */ new Set([
42
- // Sort directions
43
- "sort",
44
- // Null handling
45
- "nulls",
46
- // Relation load strategy
47
- "relationLoadStrategy"
48
- ]);
49
- var NUMERIC_STRUCTURE_KEYS = /* @__PURE__ */ new Set(["take", "skip"]);
50
- var FILTER_OPERATORS = /* @__PURE__ */ new Set([
51
- "equals",
52
- "not",
53
- "in",
54
- "notIn",
55
- "lt",
56
- "lte",
57
- "gt",
58
- "gte",
59
- "contains",
60
- "startsWith",
61
- "endsWith",
62
- "search",
63
- "mode",
64
- "has",
65
- "hasEvery",
66
- "hasSome",
67
- "isEmpty",
68
- // JSON operators
69
- "path",
70
- "string_contains",
71
- "string_starts_with",
72
- "string_ends_with",
73
- "array_contains",
74
- "array_starts_with",
75
- "array_ends_with"
76
- ]);
77
- var LOGICAL_OPERATORS = /* @__PURE__ */ new Set(["AND", "OR", "NOT", "is", "isNot", "some", "every", "none"]);
78
- var SORT_DIRECTIONS = /* @__PURE__ */ new Set(["asc", "desc"]);
79
- function parameterizeValue(value, context) {
27
+ var STRUCTURAL_VALUE_KEYS = /* @__PURE__ */ new Set(["take", "skip", "sort", "nulls", "mode", "relationLoadStrategy", "distinct"]);
28
+ function isTaggedValue(value) {
29
+ return typeof value === "object" && value !== null && "$type" in value && typeof value.$type === "string";
30
+ }
31
+ function isSelectionMarker(key) {
32
+ return key === "$scalars" || key === "$composites";
33
+ }
34
+ function isSortDirection(value) {
35
+ return value === "asc" || value === "desc";
36
+ }
37
+ function getChildContext(key, parentContext) {
38
+ if (key === "arguments") return "default";
39
+ if (key === "selection") return "selection";
40
+ if (key === "orderBy") return "orderBy";
41
+ if (key === "data") return "data";
42
+ if (parentContext === "data") {
43
+ if (RELATION_OPERATION_KEYS.has(key) || STRUCTURAL_KEYS_IN_DATA.has(key)) {
44
+ return "default";
45
+ }
46
+ }
47
+ return parentContext;
48
+ }
49
+ function parameterize(value, context, key) {
80
50
  if (value === null || value === void 0) {
81
51
  return value;
82
52
  }
83
53
  if (isTaggedValue(value)) {
84
- if (value.$type === "FieldRef") {
85
- return value;
86
- }
87
- if (value.$type === "Enum" && context.isStructuralEnum) {
88
- return value;
89
- }
90
- return PARAM_PLACEHOLDER;
54
+ return value.$type === "FieldRef" ? value : PARAM_PLACEHOLDER;
91
55
  }
92
56
  if (Array.isArray(value)) {
93
- return value.map((item) => parameterizeValue(item, context));
57
+ return value.map((item) => parameterize(item, context, key));
94
58
  }
95
59
  if (typeof value === "object") {
96
60
  return parameterizeObject(value, context);
97
61
  }
98
- if (context.isDataContext) {
99
- return PARAM_PLACEHOLDER;
100
- }
101
- if (context.isSelectionContext && typeof value === "boolean") {
62
+ if (key && STRUCTURAL_VALUE_KEYS.has(key)) {
102
63
  return value;
103
64
  }
104
- if (context.isStructuralValue) {
65
+ if (context === "selection" && typeof value === "boolean") {
105
66
  return value;
106
67
  }
107
- if (context.isOrderByContext && typeof value === "string" && SORT_DIRECTIONS.has(value)) {
68
+ if (context === "orderBy" && typeof value === "string" && isSortDirection(value)) {
108
69
  return value;
109
70
  }
110
71
  return PARAM_PLACEHOLDER;
@@ -116,264 +77,82 @@ function parameterizeObject(obj, context) {
116
77
  result[key] = value;
117
78
  continue;
118
79
  }
119
- const newContext = getContextForKey(key, context);
120
- result[key] = parameterizeValue(value, newContext);
80
+ const childContext = getChildContext(key, context);
81
+ result[key] = parameterize(value, childContext, key);
121
82
  }
122
83
  return result;
123
84
  }
124
- function isStructuralKey(key) {
125
- return key === "arguments" || STRUCTURE_KEYS.has(key);
126
- }
127
- function getContextForKey(key, parentContext) {
128
- if (key === "arguments") {
129
- return {
130
- isDataContext: false,
131
- isStructuralValue: false,
132
- isStructuralEnum: false,
133
- isSelectionContext: false,
134
- isOrderByContext: false,
135
- parentKey: key
136
- };
137
- }
138
- if (key === "selection" || parentContext.isSelectionContext && !isStructuralKey(key)) {
139
- return {
140
- isDataContext: false,
141
- isStructuralValue: false,
142
- isStructuralEnum: false,
143
- isSelectionContext: true,
144
- isOrderByContext: false,
145
- parentKey: key
146
- };
147
- }
148
- if (key === "data" || parentContext.isDataContext) {
149
- if (STRUCTURE_KEYS.has(key)) {
150
- return {
151
- isDataContext: false,
152
- isStructuralValue: false,
153
- isStructuralEnum: false,
154
- isSelectionContext: false,
155
- isOrderByContext: false,
156
- parentKey: key
157
- };
158
- }
159
- return {
160
- isDataContext: true,
161
- isStructuralValue: false,
162
- isStructuralEnum: false,
163
- isSelectionContext: false,
164
- isOrderByContext: false,
165
- parentKey: key
166
- };
167
- }
168
- if (STRUCTURAL_VALUE_KEYS.has(key)) {
169
- return {
170
- isDataContext: false,
171
- isStructuralValue: true,
172
- isStructuralEnum: true,
173
- isSelectionContext: false,
174
- isOrderByContext: false,
175
- parentKey: key
176
- };
177
- }
178
- if (NUMERIC_STRUCTURE_KEYS.has(key)) {
179
- return {
180
- isDataContext: false,
181
- isStructuralValue: true,
182
- isStructuralEnum: false,
183
- isSelectionContext: false,
184
- isOrderByContext: false,
185
- parentKey: key
186
- };
187
- }
188
- if (key === "orderBy" || parentContext.isOrderByContext) {
189
- return {
190
- isDataContext: false,
191
- isStructuralValue: false,
192
- isStructuralEnum: false,
193
- isSelectionContext: false,
194
- isOrderByContext: true,
195
- parentKey: key
196
- };
197
- }
198
- if (FILTER_OPERATORS.has(key)) {
199
- if (key === "mode") {
200
- return {
201
- isDataContext: false,
202
- isStructuralValue: true,
203
- isStructuralEnum: true,
204
- isSelectionContext: false,
205
- isOrderByContext: false,
206
- parentKey: key
207
- };
208
- }
209
- return {
210
- isDataContext: false,
211
- isStructuralValue: false,
212
- isStructuralEnum: false,
213
- isSelectionContext: false,
214
- isOrderByContext: false,
215
- parentKey: key
216
- };
217
- }
218
- if (LOGICAL_OPERATORS.has(key)) {
219
- return {
220
- isDataContext: false,
221
- isStructuralValue: false,
222
- isStructuralEnum: false,
223
- isSelectionContext: false,
224
- isOrderByContext: false,
225
- parentKey: key
226
- };
227
- }
228
- if (STRUCTURE_KEYS.has(key)) {
229
- return {
230
- isDataContext: false,
231
- isStructuralValue: false,
232
- isStructuralEnum: false,
233
- isSelectionContext: key === "selection",
234
- isOrderByContext: false,
235
- parentKey: key
236
- };
237
- }
238
- return {
239
- isDataContext: parentContext.isDataContext,
240
- isStructuralValue: false,
241
- isStructuralEnum: false,
242
- isSelectionContext: parentContext.isSelectionContext,
243
- isOrderByContext: parentContext.isOrderByContext,
244
- parentKey: key
245
- };
246
- }
247
85
  function parameterizeQuery(query) {
248
- const initialContext = {
249
- isDataContext: false,
250
- isStructuralValue: false,
251
- isStructuralEnum: false,
252
- isSelectionContext: false,
253
- isOrderByContext: false
254
- };
255
- return parameterizeValue(query, initialContext);
86
+ return parameterize(query, "default");
256
87
  }
257
88
 
258
89
  // src/shape/shape.ts
259
- function isNestedQuery(value) {
260
- return typeof value === "object" && value !== null && ("arguments" in value || "selection" in value);
261
- }
262
- function isEmptyObject(value) {
263
- return typeof value === "object" && value !== null && Object.keys(value).length === 0;
264
- }
265
- function canSimplifySelectionToTrue(selection) {
266
- if (selection.$scalars !== true) {
267
- return false;
268
- }
269
- for (const key of Object.keys(selection)) {
270
- if (key !== "$scalars" && key !== "$composites") {
271
- return false;
272
- }
273
- }
274
- return true;
275
- }
276
- function canSimplifyNestedQueryToTrue(args, selection) {
277
- const argsEmpty = args === void 0 || isEmptyObject(args);
278
- if (!argsEmpty) {
279
- return false;
280
- }
281
- if (!selection) {
282
- return false;
283
- }
284
- return canSimplifySelectionToTrue(selection);
285
- }
286
- function shapeRelation(relation) {
287
- if (relation === true) {
288
- return true;
289
- }
290
- if (typeof relation !== "object" || relation === null) {
291
- return relation;
292
- }
293
- const relationObj = relation;
294
- if (isNestedQuery(relationObj)) {
295
- const args = relationObj.arguments;
296
- const selection = relationObj.selection;
297
- if (canSimplifyNestedQueryToTrue(args, selection)) {
90
+ function isObject(value) {
91
+ return typeof value === "object" && value !== null && !Array.isArray(value);
92
+ }
93
+ function isEmpty(obj) {
94
+ return !obj || Object.keys(obj).length === 0;
95
+ }
96
+ function isSimpleSelection(selection) {
97
+ if (!selection || selection.$scalars !== true) return false;
98
+ return Object.keys(selection).every((k) => k === "$scalars" || k === "$composites");
99
+ }
100
+ function shapeValue(value) {
101
+ if (value === true || !isObject(value)) return value;
102
+ if ("arguments" in value || "selection" in value) {
103
+ const args = value.arguments;
104
+ const selection = value.selection;
105
+ if (isEmpty(args) && isSimpleSelection(selection)) {
298
106
  return true;
299
107
  }
300
- return shapeQuery(relationObj);
301
- } else {
302
- if (canSimplifySelectionToTrue(relationObj)) {
303
- return true;
304
- }
305
- const shaped = shapeSelectionObject(relationObj);
306
- return shaped || true;
108
+ return shapeQuery(value);
307
109
  }
110
+ return isSimpleSelection(value) ? true : shapeQuery({ selection: value });
308
111
  }
309
- function shapeSelectionObject(selection) {
112
+ function shapeQuery(query) {
113
+ if (!isObject(query)) return query;
114
+ const args = query.arguments;
115
+ const selection = query.selection;
116
+ const result = args ? { ...args } : {};
117
+ if (!selection) return result;
310
118
  const hasScalars = selection.$scalars === true;
311
- const booleanFields = {};
312
- const objectFields = {};
119
+ const fields = {};
313
120
  for (const [key, value] of Object.entries(selection)) {
314
- if (key === "$scalars" || key === "$composites") {
315
- continue;
316
- }
317
- if (typeof value === "boolean") {
318
- booleanFields[key] = value;
319
- } else if (typeof value === "object" && value !== null) {
320
- objectFields[key] = value;
321
- }
121
+ if (key === "$scalars" || key === "$composites") continue;
122
+ if (hasScalars && typeof value === "boolean") continue;
123
+ fields[key] = typeof value === "boolean" ? value : shapeValue(value);
322
124
  }
323
- const hasBooleanFields = Object.keys(booleanFields).length > 0;
324
- const hasObjectFields = Object.keys(objectFields).length > 0;
325
- if (!hasBooleanFields && !hasObjectFields) {
326
- return null;
327
- }
328
- const shapedObjects = {};
329
- for (const [key, value] of Object.entries(objectFields)) {
330
- shapedObjects[key] = shapeRelation(value);
331
- }
332
- if (hasScalars) {
333
- if (!hasObjectFields) {
334
- return null;
335
- }
336
- return { include: shapedObjects };
337
- } else {
338
- return { select: { ...booleanFields, ...shapedObjects } };
339
- }
340
- }
341
- function shapeQuery(query) {
342
- if (typeof query !== "object" || query === null) {
343
- return query;
344
- }
345
- const queryObj = query;
346
- const args = queryObj.arguments;
347
- const selection = queryObj.selection;
348
- const result = args ? { ...args } : {};
349
- if (selection) {
350
- const shaped = shapeSelectionObject(selection);
351
- if (shaped) {
352
- Object.assign(result, shaped);
353
- }
125
+ if (Object.keys(fields).length > 0) {
126
+ result[hasScalars ? "include" : "select"] = fields;
354
127
  }
355
128
  return result;
356
129
  }
357
130
 
358
- // src/format.ts
131
+ // src/format/format.ts
359
132
  function toBase64Url(data) {
360
133
  return Buffer.from(data, "utf-8").toString("base64url");
361
134
  }
362
135
  function formatQueryInsight(queryInfo) {
363
- if (queryInfo.type === "single") {
364
- const { modelName: modelName2, action: action2, query } = queryInfo;
365
- if (!modelName2) {
366
- return action2;
136
+ switch (queryInfo.type) {
137
+ case "single": {
138
+ const { modelName, action, query } = queryInfo;
139
+ if (!modelName) {
140
+ return action;
141
+ }
142
+ const parameterizedQuery = parameterizeQuery(query);
143
+ const shapedQuery = shapeQuery(parameterizedQuery);
144
+ const encoded = toBase64Url(JSON.stringify(shapedQuery));
145
+ return `${modelName}.${action}:${encoded}`;
146
+ }
147
+ case "compacted": {
148
+ const { modelName, action, queries } = queryInfo;
149
+ const shapedQueries = queries.map((q) => shapeQuery(parameterizeQuery(q)));
150
+ const encoded = toBase64Url(JSON.stringify(shapedQueries));
151
+ return `${modelName}.${action}:${encoded}`;
367
152
  }
368
- const parameterizedQuery = parameterizeQuery(query);
369
- const shapedQuery = shapeQuery(parameterizedQuery);
370
- const encoded2 = toBase64Url(JSON.stringify(shapedQuery));
371
- return `${modelName2}.${action2}:${encoded2}`;
153
+ default:
154
+ throw new Error(`invalid queryInfo.type=${queryInfo.type}`);
372
155
  }
373
- const { modelName, action, queries } = queryInfo;
374
- const shapedQueries = queries.map((q) => shapeQuery(parameterizeQuery(q)));
375
- const encoded = toBase64Url(JSON.stringify(shapedQueries));
376
- return `${modelName}.${action}:${encoded}`;
377
156
  }
378
157
 
379
158
  // src/index.ts
@@ -3,16 +3,17 @@
3
3
  *
4
4
  * This module handles the conversion of Prisma queries into parameterized shapes
5
5
  * where user data values are replaced with placeholder markers.
6
+ *
7
+ * Design principle: when in doubt, parameterize. We're building query shapes
8
+ * for observability, not actual query parameterization for the query compiler.
6
9
  */
7
10
  /**
8
11
  * Placeholder object used to replace parameterized values in query shapes.
9
- * In the future, this may include additional fields like `value` containing
10
- * param name and/or param type when queries are parameterized by the query compiler.
11
12
  */
12
13
  export declare const PARAM_PLACEHOLDER: {
13
14
  $type: string;
14
15
  };
15
16
  /**
16
- * Parameterizes a query object, replacing all user data values with placeholders
17
+ * Parameterizes a query object, replacing all user data values with placeholders.
17
18
  */
18
19
  export declare function parameterizeQuery(query: unknown): unknown;
@@ -1,11 +1,7 @@
1
1
  /**
2
2
  * Query shaping logic for Prisma query insights.
3
3
  *
4
- * This module transforms JSON protocol query representations into a format
5
- * that more closely resembles the original Prisma query API, making the
6
- * encoded query shapes more intuitive to read and understand.
7
- *
8
- * Key transformations:
4
+ * Transforms JSON protocol query representations into Prisma-like format:
9
5
  * - Move `arguments` contents one level up
10
6
  * - Convert `selection` to `select`/`include`:
11
7
  * - If `$scalars: true`, use `include` for relations
@@ -14,11 +10,5 @@
14
10
  */
15
11
  /**
16
12
  * Shapes a query object from JSON protocol format to Prisma-like format.
17
- *
18
- * Transforms:
19
- * - `{ arguments: {...}, selection: {...} }` → `{ ..., select/include: {...} }`
20
- *
21
- * @param query - The query object in JSON protocol format
22
- * @returns The shaped query in Prisma-like format
23
13
  */
24
14
  export declare function shapeQuery(query: unknown): unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/sqlcommenter-query-insights",
3
- "version": "0.0.1",
3
+ "version": "7.2.0-dev.7",
4
4
  "description": "Query insights plugin for Prisma SQL commenter",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -25,18 +25,17 @@
25
25
  "directory": "packages/sqlcommenter-query-insights"
26
26
  },
27
27
  "bugs": "https://github.com/prisma/prisma/issues",
28
- "scripts": {
29
- "dev": "DEV=true tsx helpers/build.ts",
30
- "build": "tsx helpers/build.ts",
31
- "prepublishOnly": "pnpm run build",
32
- "test": "vitest run"
33
- },
34
28
  "files": [
35
29
  "dist"
36
30
  ],
37
31
  "sideEffects": false,
38
32
  "devDependencies": {
39
- "@prisma/sqlcommenter": "workspace:*",
40
- "fast-check": "4.3.0"
33
+ "fast-check": "4.3.0",
34
+ "@prisma/sqlcommenter": "7.2.0-dev.7"
35
+ },
36
+ "scripts": {
37
+ "dev": "DEV=true tsx helpers/build.ts",
38
+ "build": "tsx helpers/build.ts",
39
+ "test": "vitest run"
41
40
  }
42
- }
41
+ }
@@ -1,18 +0,0 @@
1
- /**
2
- * Query parameterization logic for Prisma query insights.
3
- *
4
- * This module handles the conversion of Prisma queries into parameterized shapes
5
- * where user data values are replaced with placeholder markers.
6
- */
7
- /**
8
- * Placeholder object used to replace parameterized values in query shapes.
9
- * In the future, this may include additional fields like `value` containing
10
- * param name and/or param type when queries are parameterized by the query compiler.
11
- */
12
- export declare const PARAM_PLACEHOLDER: {
13
- $type: string;
14
- };
15
- /**
16
- * Parameterizes a query object, replacing all user data values with placeholders
17
- */
18
- export declare function parameterizeQuery(query: unknown): unknown;
File without changes