prisma-sql 1.4.0 → 1.6.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/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { convertDMMFToModels, isDynamicParameter, extractDynamicName } from '@dee-wan/schema-parser';
2
- export { convertDMMFToModels } from '@dee-wan/schema-parser';
3
-
1
+ "use strict";
4
2
  var __defProp = Object.defineProperty;
5
3
  var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
5
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
9
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -20,6 +20,19 @@ var __spreadValues = (a, b) => {
20
20
  return a;
21
21
  };
22
22
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
36
  var __async = (__this, __arguments, generator) => {
24
37
  return new Promise((resolve, reject) => {
25
38
  var fulfilled = (value) => {
@@ -41,6 +54,21 @@ var __async = (__this, __arguments, generator) => {
41
54
  });
42
55
  };
43
56
 
57
+ // src/index.ts
58
+ var src_exports = {};
59
+ __export(src_exports, {
60
+ convertDMMFToModels: () => import_schema_parser10.convertDMMFToModels,
61
+ createPrismaSQL: () => createPrismaSQL,
62
+ createToSQL: () => createToSQL,
63
+ generateAllSQL: () => generateAllSQL,
64
+ generateSQL: () => generateSQL2,
65
+ generateSQLByModel: () => generateSQLByModel,
66
+ getGlobalDialect: () => getGlobalDialect,
67
+ setGlobalDialect: () => setGlobalDialect,
68
+ speedExtension: () => speedExtension
69
+ });
70
+ module.exports = __toCommonJS(src_exports);
71
+
44
72
  // src/sql-builder-dialect.ts
45
73
  var globalDialect = "postgres";
46
74
  function setGlobalDialect(dialect) {
@@ -356,6 +384,7 @@ var Wildcards = Object.freeze({
356
384
  [Ops.ENDS_WITH]: (v) => `%${v}`
357
385
  });
358
386
  var REGEX_CACHE = {
387
+ PARAM_PLACEHOLDER: /\$(\d+)/g,
359
388
  VALID_IDENTIFIER: /^[a-z_][a-z0-9_]*$/
360
389
  };
361
390
  var LIMITS = Object.freeze({
@@ -660,7 +689,7 @@ function validateMappingsAgainstPlaceholders(mappings, placeholders, max, dialec
660
689
  const mappingIndices = /* @__PURE__ */ new Set();
661
690
  for (const mapping of mappings) {
662
691
  validateMappingIndex(mapping, max);
663
- ensureUniqueMappingIndex(mappingIndices, mapping.index);
692
+ ensureUniqueMappingIndex(mappingIndices, mapping.index, dialect);
664
693
  ensureMappingIndexExistsInSql(placeholders, mapping.index);
665
694
  validateMappingValueShape(mapping);
666
695
  }
@@ -850,6 +879,9 @@ function joinCondition(field, parentAlias, childAlias) {
850
879
  function getModelByName(schemas, name) {
851
880
  return schemas.find((m) => m.name === name);
852
881
  }
882
+
883
+ // src/builder/where/operators-scalar.ts
884
+ var import_schema_parser = require("@dee-wan/schema-parser");
853
885
  function buildNotComposite(expr, val, params, dialect, buildOp, separator) {
854
886
  const entries = Object.entries(val).filter(
855
887
  ([k, v]) => k !== "mode" && v !== void 0
@@ -878,7 +910,7 @@ function buildScalarOperator(expr, op, val, params, mode, fieldType, dialect) {
878
910
  }
879
911
  if (op === Ops.EQUALS && mode === Modes.INSENSITIVE && isNotNullish(dialect)) {
880
912
  const placeholder = params.addAuto(val);
881
- return caseInsensitiveEquals(expr, placeholder);
913
+ return caseInsensitiveEquals(expr, placeholder, dialect);
882
914
  }
883
915
  const STRING_LIKE_OPS = /* @__PURE__ */ new Set([
884
916
  Ops.CONTAINS,
@@ -949,7 +981,7 @@ function buildDynamicLikePattern(op, placeholder, dialect) {
949
981
  }
950
982
  function handleLikeOperator(expr, op, val, params, mode, dialect) {
951
983
  if (val === void 0) return "";
952
- if (isDynamicParameter(val)) {
984
+ if ((0, import_schema_parser.isDynamicParameter)(val)) {
953
985
  const placeholder2 = params.addAuto(val);
954
986
  const patternExpr = buildDynamicLikePattern(op, placeholder2, dialect);
955
987
  if (mode === Modes.INSENSITIVE) {
@@ -965,7 +997,7 @@ function handleLikeOperator(expr, op, val, params, mode, dialect) {
965
997
  }
966
998
  function handleInOperator(expr, op, val, params, dialect) {
967
999
  if (val === void 0) return "";
968
- if (isDynamicParameter(val)) {
1000
+ if ((0, import_schema_parser.isDynamicParameter)(val)) {
969
1001
  const placeholder2 = params.addAuto(val);
970
1002
  return op === Ops.IN ? inArray(expr, placeholder2, dialect) : notInArray(expr, placeholder2, dialect);
971
1003
  }
@@ -998,8 +1030,11 @@ function handleComparisonOperator(expr, op, val, params) {
998
1030
  const placeholder = params.addAuto(val);
999
1031
  return `${expr} ${sqlOp} ${placeholder}`;
1000
1032
  }
1033
+
1034
+ // src/builder/where/operators-array.ts
1035
+ var import_schema_parser2 = require("@dee-wan/schema-parser");
1001
1036
  function buildArrayParam(val, params, dialect) {
1002
- if (isDynamicParameter(val)) {
1037
+ if ((0, import_schema_parser2.isDynamicParameter)(val)) {
1003
1038
  return params.addAuto(val);
1004
1039
  }
1005
1040
  if (!Array.isArray(val)) {
@@ -1073,7 +1108,7 @@ function handleArrayHas(expr, val, params, cast, dialect) {
1073
1108
  value: val
1074
1109
  });
1075
1110
  }
1076
- if (!isDynamicParameter(val) && Array.isArray(val)) {
1111
+ if (!(0, import_schema_parser2.isDynamicParameter)(val) && Array.isArray(val)) {
1077
1112
  throw createError(`has requires scalar value (single element), not array`, {
1078
1113
  operator: Ops.HAS,
1079
1114
  value: val
@@ -1090,7 +1125,7 @@ function handleArrayHas(expr, val, params, cast, dialect) {
1090
1125
  }
1091
1126
  function handleArrayHasSome(expr, val, params, cast, dialect) {
1092
1127
  if (val === void 0) return "";
1093
- if (isDynamicParameter(val)) {
1128
+ if ((0, import_schema_parser2.isDynamicParameter)(val)) {
1094
1129
  const placeholder2 = params.addAuto(val);
1095
1130
  return arrayOverlaps(expr, placeholder2, cast, dialect);
1096
1131
  }
@@ -1762,6 +1797,9 @@ function createAliasGenerator(maxAliases = 1e4) {
1762
1797
  }
1763
1798
  };
1764
1799
  }
1800
+
1801
+ // src/builder/shared/param-store.ts
1802
+ var import_schema_parser3 = require("@dee-wan/schema-parser");
1765
1803
  var MAX_PARAM_INDEX = Number.MAX_SAFE_INTEGER - 1e3;
1766
1804
  function assertSameLength(params, mappings) {
1767
1805
  if (params.length !== mappings.length) {
@@ -1892,8 +1930,8 @@ function createStoreInternal(startIndex, initialParams = [], initialMappings = [
1892
1930
  return dynamicName === void 0 ? addStatic(value) : addDynamic(dynamicName);
1893
1931
  }
1894
1932
  function addAuto(value) {
1895
- if (isDynamicParameter(value)) {
1896
- const dynamicName = extractDynamicName(value);
1933
+ if ((0, import_schema_parser3.isDynamicParameter)(value)) {
1934
+ const dynamicName = (0, import_schema_parser3.extractDynamicName)(value);
1897
1935
  return add(void 0, dynamicName);
1898
1936
  }
1899
1937
  return add(value);
@@ -1982,10 +2020,13 @@ function buildWhereClause(where, options) {
1982
2020
  }
1983
2021
  return publicResult;
1984
2022
  }
2023
+
2024
+ // src/builder/shared/int-like.ts
2025
+ var import_schema_parser4 = require("@dee-wan/schema-parser");
1985
2026
  function normalizeIntLike(name, v, opts = {}) {
1986
2027
  var _a, _b;
1987
2028
  if (!isNotNullish(v)) return void 0;
1988
- if (isDynamicParameter(v)) return v;
2029
+ if ((0, import_schema_parser4.isDynamicParameter)(v)) return v;
1989
2030
  if (typeof v !== "number" || !Number.isFinite(v) || !Number.isInteger(v)) {
1990
2031
  throw new Error(`${name} must be an integer`);
1991
2032
  }
@@ -2002,6 +2043,9 @@ function normalizeIntLike(name, v, opts = {}) {
2002
2043
  }
2003
2044
  return v;
2004
2045
  }
2046
+
2047
+ // src/builder/shared/dynamic-params.ts
2048
+ var import_schema_parser5 = require("@dee-wan/schema-parser");
2005
2049
  function scopeName(scope, dynamicName) {
2006
2050
  const s = String(scope).trim();
2007
2051
  const dn = String(dynamicName).trim();
@@ -2009,13 +2053,16 @@ function scopeName(scope, dynamicName) {
2009
2053
  return `${s}:${dn}`;
2010
2054
  }
2011
2055
  function addAutoScoped(params, value, scope) {
2012
- if (isDynamicParameter(value)) {
2013
- const dn = extractDynamicName(value);
2056
+ if ((0, import_schema_parser5.isDynamicParameter)(value)) {
2057
+ const dn = (0, import_schema_parser5.extractDynamicName)(value);
2014
2058
  return params.add(void 0, scopeName(scope, dn));
2015
2059
  }
2016
2060
  return params.add(value);
2017
2061
  }
2018
2062
 
2063
+ // src/builder/pagination.ts
2064
+ var import_schema_parser6 = require("@dee-wan/schema-parser");
2065
+
2019
2066
  // src/builder/shared/order-by-utils.ts
2020
2067
  var flipNulls = (v) => {
2021
2068
  const s = String(v).toLowerCase();
@@ -2151,7 +2198,7 @@ function normalizeFiniteInteger(name, v) {
2151
2198
  return v;
2152
2199
  }
2153
2200
  function normalizeNonNegativeInt(name, v) {
2154
- if (isDynamicParameter(v)) return v;
2201
+ if ((0, import_schema_parser6.isDynamicParameter)(v)) return v;
2155
2202
  const n = normalizeFiniteInteger(name, v);
2156
2203
  if (n < 0) {
2157
2204
  throw new Error(`${name} must be >= 0`);
@@ -2165,7 +2212,7 @@ function hasNonNullishProp(v, key) {
2165
2212
  return isPlainObject(v) && key in v && isNotNullish(v[key]);
2166
2213
  }
2167
2214
  function normalizeIntegerOrDynamic(name, v) {
2168
- if (isDynamicParameter(v)) return v;
2215
+ if ((0, import_schema_parser6.isDynamicParameter)(v)) return v;
2169
2216
  return normalizeFiniteInteger(name, v);
2170
2217
  }
2171
2218
  function readSkipTake(relArgs) {
@@ -2398,6 +2445,9 @@ function getPaginationParams(method, args) {
2398
2445
  return {};
2399
2446
  }
2400
2447
 
2448
+ // src/builder/select/assembly.ts
2449
+ var import_schema_parser7 = require("@dee-wan/schema-parser");
2450
+
2401
2451
  // src/builder/select/fields.ts
2402
2452
  function toSelectEntries(select) {
2403
2453
  const out = [];
@@ -3233,7 +3283,7 @@ function appendPagination(sql, spec) {
3233
3283
  const isFindUniqueOrFirst = method === "findUnique" || method === "findFirst";
3234
3284
  if (isFindUniqueOrFirst) {
3235
3285
  const parts2 = [sql, SQL_TEMPLATES.LIMIT, "1"];
3236
- const hasSkip = isNotNullish(pagination.skip) && (isDynamicParameter(pagination.skip) || typeof pagination.skip === "number" && pagination.skip > 0) && method === "findFirst";
3286
+ const hasSkip = isNotNullish(pagination.skip) && ((0, import_schema_parser7.isDynamicParameter)(pagination.skip) || typeof pagination.skip === "number" && pagination.skip > 0) && method === "findFirst";
3237
3287
  if (hasSkip) {
3238
3288
  const placeholder = addAutoScoped(
3239
3289
  params,
@@ -3466,7 +3516,7 @@ function normalizeArgsForNegativeTake(method, args) {
3466
3516
  }
3467
3517
  function normalizeArgsForDialect(dialect, args, model) {
3468
3518
  if (dialect !== "postgres") return args;
3469
- return applyPostgresDistinctOrderBy(args);
3519
+ return applyPostgresDistinctOrderBy(args, model);
3470
3520
  }
3471
3521
  function buildCursorClauseIfAny(input) {
3472
3522
  const { cursor, orderBy, tableName, alias, params, dialect } = input;
@@ -3545,7 +3595,9 @@ function buildSelectSql(input) {
3545
3595
  const argsForSql = normalizeArgsForNegativeTake(method, args);
3546
3596
  const normalizedArgs = normalizeArgsForDialect(
3547
3597
  dialectToUse,
3548
- argsForSql);
3598
+ argsForSql,
3599
+ model
3600
+ );
3549
3601
  validateDistinct(model, normalizedArgs.distinct);
3550
3602
  validateOrderBy(model, normalizedArgs.orderBy);
3551
3603
  validateCursor(model, normalizedArgs.cursor);
@@ -3561,6 +3613,9 @@ function buildSelectSql(input) {
3561
3613
  });
3562
3614
  return constructFinalSql(spec);
3563
3615
  }
3616
+
3617
+ // src/builder/aggregates.ts
3618
+ var import_schema_parser8 = require("@dee-wan/schema-parser");
3564
3619
  var MODEL_FIELD_CACHE = /* @__PURE__ */ new WeakMap();
3565
3620
  var NUMERIC_TYPES = /* @__PURE__ */ new Set(["Int", "Float", "Decimal", "BigInt"]);
3566
3621
  var AGGREGATES = [
@@ -3655,7 +3710,7 @@ function buildNullComparison(expr, op) {
3655
3710
  throw new Error(`Operator '${op}' doesn't support null in HAVING`);
3656
3711
  }
3657
3712
  function buildInComparison(expr, op, val, params, dialect) {
3658
- if (isDynamicParameter(val)) {
3713
+ if ((0, import_schema_parser8.isDynamicParameter)(val)) {
3659
3714
  const placeholder2 = addHavingParam(params, op, val);
3660
3715
  return op === Ops.IN ? inArray(expr, placeholder2, dialect) : notInArray(expr, placeholder2, dialect);
3661
3716
  }
@@ -4053,7 +4108,7 @@ function buildCountSql(whereResult, tableName, alias, skip, dialect) {
4053
4108
  });
4054
4109
  }
4055
4110
  function applyCountSkip(subSelect, normalizedSkip, params, dialect) {
4056
- const shouldApply = isDynamicParameter(normalizedSkip) || typeof normalizedSkip === "number" && normalizedSkip > 0;
4111
+ const shouldApply = (0, import_schema_parser8.isDynamicParameter)(normalizedSkip) || typeof normalizedSkip === "number" && normalizedSkip > 0;
4057
4112
  if (!shouldApply) return subSelect;
4058
4113
  const placeholder = addAutoScoped(params, normalizedSkip, "count.skip");
4059
4114
  if (dialect === "sqlite") {
@@ -4061,6 +4116,9 @@ function applyCountSkip(subSelect, normalizedSkip, params, dialect) {
4061
4116
  }
4062
4117
  return `${subSelect} ${SQL_TEMPLATES.OFFSET} ${placeholder}`;
4063
4118
  }
4119
+
4120
+ // src/sql-generator.ts
4121
+ var import_schema_parser9 = require("@dee-wan/schema-parser");
4064
4122
  function safeAlias(input) {
4065
4123
  const raw = String(input).toLowerCase();
4066
4124
  const cleaned = raw.replace(/[^a-z0-9_]/g, "_");
@@ -4162,7 +4220,7 @@ function buildParamsFromMappings(mappings) {
4162
4220
  }
4163
4221
  function resolveModelContext(directive) {
4164
4222
  const { model, datamodel } = directive.context;
4165
- const schemaModels = convertDMMFToModels(datamodel);
4223
+ const schemaModels = (0, import_schema_parser9.convertDMMFToModels)(datamodel);
4166
4224
  const modelDef = getModelByName(schemaModels, model.name);
4167
4225
  if (!modelDef) throw new Error(`Model ${model.name} not found in schema`);
4168
4226
  return { schemaModels, modelDef };
@@ -4295,6 +4353,9 @@ function transformQueryResults(method, results) {
4295
4353
  const transformer = RESULT_TRANSFORMERS[method];
4296
4354
  return transformer ? transformer(results) : results;
4297
4355
  }
4356
+
4357
+ // src/index.ts
4358
+ var import_schema_parser10 = require("@dee-wan/schema-parser");
4298
4359
  var ACCELERATED_METHODS = /* @__PURE__ */ new Set([
4299
4360
  "findMany",
4300
4361
  "findFirst",
@@ -4621,7 +4682,16 @@ function generateSQLByModel(directives) {
4621
4682
  }
4622
4683
  return byModel;
4623
4684
  }
4624
-
4625
- export { createPrismaSQL, createToSQL, generateAllSQL, generateSQL2 as generateSQL, generateSQLByModel, getGlobalDialect, setGlobalDialect, speedExtension };
4626
- //# sourceMappingURL=index.js.map
4685
+ // Annotate the CommonJS export names for ESM import in node:
4686
+ 0 && (module.exports = {
4687
+ convertDMMFToModels,
4688
+ createPrismaSQL,
4689
+ createToSQL,
4690
+ generateAllSQL,
4691
+ generateSQL,
4692
+ generateSQLByModel,
4693
+ getGlobalDialect,
4694
+ setGlobalDialect,
4695
+ speedExtension
4696
+ });
4627
4697
  //# sourceMappingURL=index.js.map