metal-orm 1.0.83 → 1.0.85

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.cjs CHANGED
@@ -655,16 +655,27 @@ var operandTypes = /* @__PURE__ */ new Set([
655
655
  "BitwiseExpression",
656
656
  "Collate"
657
657
  ]);
658
- var hasTypeProperty = (value) => typeof value === "object" && value !== null && "type" in value;
658
+ var getNodeType = (value) => {
659
+ if (typeof value !== "object" || value === null) return void 0;
660
+ const descriptor = Object.getOwnPropertyDescriptor(value, "type");
661
+ if (descriptor && typeof descriptor.value === "string") {
662
+ return descriptor.value;
663
+ }
664
+ if ("type" in value) {
665
+ const type = value.type;
666
+ return typeof type === "string" ? type : void 0;
667
+ }
668
+ return void 0;
669
+ };
659
670
  var isOperandNode = (node) => {
660
- if (!hasTypeProperty(node)) return false;
661
- return operandTypes.has(node.type);
662
- };
663
- var isFunctionNode = (node) => isOperandNode(node) && node.type === "Function";
664
- var isCaseExpressionNode = (node) => isOperandNode(node) && node.type === "CaseExpression";
665
- var isCastExpressionNode = (node) => isOperandNode(node) && node.type === "Cast";
666
- var isCollateExpressionNode = (node) => isOperandNode(node) && node.type === "Collate";
667
- var isWindowFunctionNode = (node) => isOperandNode(node) && node.type === "WindowFunction";
671
+ const type = getNodeType(node);
672
+ return type !== void 0 && operandTypes.has(type);
673
+ };
674
+ var isFunctionNode = (node) => isOperandNode(node) && getNodeType(node) === "Function";
675
+ var isCaseExpressionNode = (node) => isOperandNode(node) && getNodeType(node) === "CaseExpression";
676
+ var isCastExpressionNode = (node) => isOperandNode(node) && getNodeType(node) === "Cast";
677
+ var isCollateExpressionNode = (node) => isOperandNode(node) && getNodeType(node) === "Collate";
678
+ var isWindowFunctionNode = (node) => isOperandNode(node) && getNodeType(node) === "WindowFunction";
668
679
  var isExpressionSelectionNode = (node) => isFunctionNode(node) || isCaseExpressionNode(node) || isCastExpressionNode(node) || isWindowFunctionNode(node);
669
680
 
670
681
  // src/core/ast/expression-builders.ts
@@ -1049,17 +1060,26 @@ var clearExpressionDispatchers = () => {
1049
1060
  var clearOperandDispatchers = () => {
1050
1061
  operandRegistry = operandRegistry.clear();
1051
1062
  };
1052
- var getNodeType = (node) => typeof node === "object" && node !== null && typeof node.type === "string" ? node.type : void 0;
1063
+ var getNodeType2 = (node) => {
1064
+ if (typeof node !== "object" || node === null) return void 0;
1065
+ const descriptor = Object.getOwnPropertyDescriptor(node, "type");
1066
+ if (descriptor && typeof descriptor.value === "string") {
1067
+ return descriptor.value;
1068
+ }
1069
+ const type = node.type;
1070
+ return typeof type === "string" ? type : void 0;
1071
+ };
1053
1072
  var unsupportedExpression = (node) => {
1054
- throw new Error(`Unsupported expression type "${getNodeType(node) ?? "unknown"}"`);
1073
+ throw new Error(`Unsupported expression type "${getNodeType2(node) ?? "unknown"}"`);
1055
1074
  };
1056
1075
  var unsupportedOperand = (node) => {
1057
- throw new Error(`Unsupported operand type "${getNodeType(node) ?? "unknown"}"`);
1076
+ throw new Error(`Unsupported operand type "${getNodeType2(node) ?? "unknown"}"`);
1058
1077
  };
1059
1078
  var visitExpression = (node, visitor) => {
1060
- const dynamic = expressionRegistry.get(node.type);
1079
+ const type = getNodeType2(node);
1080
+ const dynamic = type ? expressionRegistry.get(type) : void 0;
1061
1081
  if (dynamic) return dynamic(node, visitor);
1062
- switch (node.type) {
1082
+ switch (type) {
1063
1083
  case "BinaryExpression":
1064
1084
  if (visitor.visitBinaryExpression) return visitor.visitBinaryExpression(node);
1065
1085
  break;
@@ -1091,9 +1111,10 @@ var visitExpression = (node, visitor) => {
1091
1111
  return unsupportedExpression(node);
1092
1112
  };
1093
1113
  var visitOperand = (node, visitor) => {
1094
- const dynamic = operandRegistry.get(node.type);
1114
+ const type = getNodeType2(node);
1115
+ const dynamic = type ? operandRegistry.get(type) : void 0;
1095
1116
  if (dynamic) return dynamic(node, visitor);
1096
- switch (node.type) {
1117
+ switch (type) {
1097
1118
  case "Column":
1098
1119
  if (visitor.visitColumn) return visitor.visitColumn(node);
1099
1120
  break;
@@ -1822,9 +1843,11 @@ var Dialect = class _Dialect {
1822
1843
  * @returns Compiled SQL operand
1823
1844
  */
1824
1845
  compileOperand(node, ctx) {
1825
- const compiler = this.operandCompilers.get(node.type);
1846
+ const descriptor = Object.getOwnPropertyDescriptor(node, "type");
1847
+ const nodeType = typeof descriptor?.value === "string" ? descriptor.value : typeof node.type === "string" ? node.type : void 0;
1848
+ const compiler = nodeType ? this.operandCompilers.get(nodeType) : void 0;
1826
1849
  if (!compiler) {
1827
- throw new Error(`Unsupported operand node type "${node.type}" for ${this.constructor.name}`);
1850
+ throw new Error(`Unsupported operand node type "${nodeType ?? "unknown"}" for ${this.constructor.name}`);
1828
1851
  }
1829
1852
  return compiler(node, ctx);
1830
1853
  }
@@ -4145,6 +4168,10 @@ var QueryAstService = class {
4145
4168
  * @returns Normalized ordering term
4146
4169
  */
4147
4170
  normalizeOrderingTerm(term) {
4171
+ const paramNode = this.toParamNode(term);
4172
+ if (paramNode) {
4173
+ return paramNode;
4174
+ }
4148
4175
  const from = this.state.ast.from;
4149
4176
  const tableRef2 = from.type === "Table" && from.alias ? { ...this.table, alias: from.alias } : this.table;
4150
4177
  const termType = term.type;
@@ -4162,6 +4189,14 @@ var QueryAstService = class {
4162
4189
  }
4163
4190
  return buildColumnNode(tableRef2, term);
4164
4191
  }
4192
+ toParamNode(value) {
4193
+ if (typeof value !== "object" || value === null) return void 0;
4194
+ const type = Object.getOwnPropertyDescriptor(value, "type")?.value;
4195
+ if (type !== "Param") return void 0;
4196
+ const name = Object.getOwnPropertyDescriptor(value, "name")?.value;
4197
+ if (typeof name !== "string") return void 0;
4198
+ return { type: "Param", name };
4199
+ }
4165
4200
  };
4166
4201
 
4167
4202
  // src/query-builder/relation-projection-helper.ts
@@ -7616,7 +7651,7 @@ var collectFilterColumns = (expr, table, rootTables) => {
7616
7651
  columns.add(node.name);
7617
7652
  }
7618
7653
  };
7619
- const visitOrderingTerm = (term) => {
7654
+ const visitOrderingTerm2 = (term) => {
7620
7655
  if (!term || typeof term !== "object") return;
7621
7656
  if (isOperandNode(term)) {
7622
7657
  visitOperand2(term);
@@ -7628,7 +7663,7 @@ var collectFilterColumns = (expr, table, rootTables) => {
7628
7663
  };
7629
7664
  const visitOrderBy = (orderBy) => {
7630
7665
  if (!orderBy) return;
7631
- orderBy.forEach((node) => visitOrderingTerm(node.term));
7666
+ orderBy.forEach((node) => visitOrderingTerm2(node.term));
7632
7667
  };
7633
7668
  const visitOperand2 = (node) => {
7634
7669
  switch (node.type) {
@@ -7767,225 +7802,196 @@ var buildFilterParameters = (table, where, from, options = {}) => {
7767
7802
  }];
7768
7803
  };
7769
7804
 
7770
- // src/core/ast/ast-validation.ts
7771
- var hasParamOperandsInExpression = (expr) => {
7772
- let hasParams = false;
7773
- visitExpression(expr, {
7774
- visitBinaryExpression: (node) => {
7775
- visitOperand(node.left, {
7776
- visitParam: () => {
7777
- hasParams = true;
7778
- },
7779
- otherwise: () => {
7780
- }
7781
- });
7782
- visitOperand(node.right, {
7783
- visitParam: () => {
7784
- hasParams = true;
7785
- },
7786
- otherwise: () => {
7787
- }
7788
- });
7805
+ // src/core/ast/query-visitor.ts
7806
+ var getNodeType3 = (value) => {
7807
+ if (typeof value !== "object" || value === null) return void 0;
7808
+ const descriptor = Object.getOwnPropertyDescriptor(value, "type");
7809
+ if (descriptor && typeof descriptor.value === "string") {
7810
+ return descriptor.value;
7811
+ }
7812
+ if ("type" in value) {
7813
+ const type = value.type;
7814
+ return typeof type === "string" ? type : void 0;
7815
+ }
7816
+ return void 0;
7817
+ };
7818
+ var visitOrderingTerm = (term, visitor) => {
7819
+ if (isOperandNode(term)) {
7820
+ visitOperandNode(term, visitor);
7821
+ return;
7822
+ }
7823
+ visitExpressionNode(term, visitor);
7824
+ };
7825
+ var visitOrderByNode = (node, visitor) => {
7826
+ visitor.visitOrderBy?.(node);
7827
+ visitOrderingTerm(node.term, visitor);
7828
+ };
7829
+ var visitTableSource = (source, visitor) => {
7830
+ visitor.visitTableSource?.(source);
7831
+ if (source.type === "DerivedTable") {
7832
+ visitor.visitDerivedTable?.(source);
7833
+ visitSelectQuery(source.query, visitor);
7834
+ return;
7835
+ }
7836
+ if (source.type === "FunctionTable") {
7837
+ visitor.visitFunctionTable?.(source);
7838
+ source.args?.forEach((arg) => visitOperandNode(arg, visitor));
7839
+ }
7840
+ };
7841
+ var visitExpressionNode = (node, visitor) => {
7842
+ visitor.visitExpression?.(node);
7843
+ const type = getNodeType3(node);
7844
+ if (!type) return;
7845
+ switch (type) {
7846
+ case "BinaryExpression":
7847
+ visitOperandNode(node.left, visitor);
7848
+ visitOperandNode(node.right, visitor);
7789
7849
  if (node.escape) {
7790
- visitOperand(node.escape, {
7791
- visitParam: () => {
7792
- hasParams = true;
7793
- },
7794
- otherwise: () => {
7795
- }
7796
- });
7850
+ visitOperandNode(node.escape, visitor);
7797
7851
  }
7798
- },
7799
- visitLogicalExpression: (node) => {
7800
- node.operands.forEach((operand) => {
7801
- if (hasParamOperandsInExpression(operand)) {
7802
- hasParams = true;
7803
- }
7804
- });
7805
- },
7806
- visitNullExpression: () => {
7807
- },
7808
- visitInExpression: (node) => {
7809
- visitOperand(node.left, {
7810
- visitParam: () => {
7811
- hasParams = true;
7812
- },
7813
- otherwise: () => {
7814
- }
7815
- });
7852
+ return;
7853
+ case "LogicalExpression":
7854
+ node.operands.forEach((operand) => visitExpressionNode(operand, visitor));
7855
+ return;
7856
+ case "NullExpression":
7857
+ visitOperandNode(node.left, visitor);
7858
+ return;
7859
+ case "InExpression":
7860
+ visitOperandNode(node.left, visitor);
7816
7861
  if (Array.isArray(node.right)) {
7817
- node.right.forEach((operand) => visitOperand(operand, {
7818
- visitParam: () => {
7819
- hasParams = true;
7820
- },
7821
- otherwise: () => {
7822
- }
7823
- }));
7862
+ node.right.forEach((operand) => visitOperandNode(operand, visitor));
7863
+ } else {
7864
+ visitOperandNode(node.right, visitor);
7824
7865
  }
7825
- },
7826
- visitExistsExpression: () => {
7827
- },
7828
- visitBetweenExpression: (node) => {
7829
- visitOperand(node.left, {
7830
- visitParam: () => {
7831
- hasParams = true;
7832
- },
7833
- otherwise: () => {
7834
- }
7835
- });
7836
- visitOperand(node.lower, {
7837
- visitParam: () => {
7838
- hasParams = true;
7839
- },
7840
- otherwise: () => {
7841
- }
7842
- });
7843
- visitOperand(node.upper, {
7844
- visitParam: () => {
7845
- hasParams = true;
7846
- },
7847
- otherwise: () => {
7848
- }
7849
- });
7850
- },
7851
- visitArithmeticExpression: (node) => {
7852
- visitOperand(node.left, {
7853
- visitParam: () => {
7854
- hasParams = true;
7855
- },
7856
- otherwise: () => {
7857
- }
7858
- });
7859
- visitOperand(node.right, {
7860
- visitParam: () => {
7861
- hasParams = true;
7862
- },
7863
- otherwise: () => {
7864
- }
7865
- });
7866
- },
7867
- visitBitwiseExpression: (node) => {
7868
- visitOperand(node.left, {
7869
- visitParam: () => {
7870
- hasParams = true;
7871
- },
7872
- otherwise: () => {
7873
- }
7874
- });
7875
- visitOperand(node.right, {
7876
- visitParam: () => {
7877
- hasParams = true;
7878
- },
7879
- otherwise: () => {
7880
- }
7881
- });
7882
- },
7883
- otherwise: () => {
7866
+ return;
7867
+ case "ExistsExpression":
7868
+ visitSelectQuery(node.subquery, visitor);
7869
+ return;
7870
+ case "BetweenExpression":
7871
+ visitOperandNode(node.left, visitor);
7872
+ visitOperandNode(node.lower, visitor);
7873
+ visitOperandNode(node.upper, visitor);
7874
+ return;
7875
+ case "ArithmeticExpression":
7876
+ visitOperandNode(node.left, visitor);
7877
+ visitOperandNode(node.right, visitor);
7878
+ return;
7879
+ case "BitwiseExpression":
7880
+ visitOperandNode(node.left, visitor);
7881
+ visitOperandNode(node.right, visitor);
7882
+ return;
7883
+ default: {
7884
+ return;
7884
7885
  }
7885
- });
7886
- return hasParams;
7886
+ }
7887
7887
  };
7888
- var hasParamOperandsInOperand = (operand) => {
7889
- let hasParams = false;
7890
- visitOperand(operand, {
7891
- visitColumn: () => {
7892
- },
7893
- visitLiteral: () => {
7894
- },
7895
- visitParam: () => {
7896
- hasParams = true;
7897
- },
7898
- visitFunction: (node) => {
7899
- node.args?.forEach((arg) => {
7900
- if (hasParamOperandsInOperand(arg)) {
7901
- hasParams = true;
7902
- }
7903
- });
7904
- },
7905
- visitJsonPath: () => {
7906
- },
7907
- visitScalarSubquery: () => {
7908
- },
7909
- visitCaseExpression: (node) => {
7910
- node.conditions.forEach((cond) => {
7911
- if (hasParamOperandsInExpression(cond.when)) {
7912
- hasParams = true;
7913
- }
7914
- if (hasParamOperandsInOperand(cond.then)) {
7915
- hasParams = true;
7916
- }
7917
- });
7918
- if (node.else && hasParamOperandsInOperand(node.else)) {
7919
- hasParams = true;
7920
- }
7921
- },
7922
- visitCast: (node) => {
7923
- if (hasParamOperandsInOperand(node.expression)) {
7924
- hasParams = true;
7888
+ var visitOperandNode = (node, visitor) => {
7889
+ visitor.visitOperand?.(node);
7890
+ const type = getNodeType3(node);
7891
+ if (type === "Param") {
7892
+ visitor.visitParam?.(node);
7893
+ }
7894
+ if (!type) return;
7895
+ switch (type) {
7896
+ case "Column":
7897
+ case "Literal":
7898
+ case "Param":
7899
+ case "AliasRef":
7900
+ return;
7901
+ case "Function":
7902
+ node.args?.forEach((arg) => visitOperandNode(arg, visitor));
7903
+ node.orderBy?.forEach((order) => visitOrderByNode(order, visitor));
7904
+ if (node.separator) {
7905
+ visitOperandNode(node.separator, visitor);
7925
7906
  }
7926
- },
7927
- visitWindowFunction: (node) => {
7928
- node.args?.forEach((arg) => {
7929
- if (hasParamOperandsInOperand(arg)) {
7930
- hasParams = true;
7931
- }
7932
- });
7933
- node.orderBy?.forEach((ord) => {
7934
- if (ord.term) {
7935
- if (hasParamOperandsInOperand(ord.term)) {
7936
- hasParams = true;
7937
- }
7938
- }
7907
+ return;
7908
+ case "JsonPath":
7909
+ visitOperandNode(node.column, visitor);
7910
+ return;
7911
+ case "ScalarSubquery":
7912
+ visitSelectQuery(node.query, visitor);
7913
+ return;
7914
+ case "CaseExpression":
7915
+ node.conditions.forEach((cond) => {
7916
+ visitExpressionNode(cond.when, visitor);
7917
+ visitOperandNode(cond.then, visitor);
7939
7918
  });
7940
- },
7941
- visitCollate: (node) => {
7942
- if (hasParamOperandsInOperand(node.expression)) {
7943
- hasParams = true;
7919
+ if (node.else) {
7920
+ visitOperandNode(node.else, visitor);
7944
7921
  }
7945
- },
7946
- visitAliasRef: () => {
7947
- },
7948
- otherwise: () => {
7922
+ return;
7923
+ case "Cast":
7924
+ visitOperandNode(node.expression, visitor);
7925
+ return;
7926
+ case "WindowFunction":
7927
+ node.args?.forEach((arg) => visitOperandNode(arg, visitor));
7928
+ node.partitionBy?.forEach((term) => visitOperandNode(term, visitor));
7929
+ node.orderBy?.forEach((order) => visitOrderByNode(order, visitor));
7930
+ return;
7931
+ case "ArithmeticExpression":
7932
+ visitOperandNode(node.left, visitor);
7933
+ visitOperandNode(node.right, visitor);
7934
+ return;
7935
+ case "BitwiseExpression":
7936
+ visitOperandNode(node.left, visitor);
7937
+ visitOperandNode(node.right, visitor);
7938
+ return;
7939
+ case "Collate":
7940
+ visitOperandNode(node.expression, visitor);
7941
+ return;
7942
+ default: {
7943
+ const _exhaustive = node;
7944
+ return _exhaustive;
7949
7945
  }
7950
- });
7951
- return hasParams;
7952
- };
7953
- var hasParamOperandsInQuery = (ast) => {
7954
- if (ast.where && hasParamOperandsInExpression(ast.where)) {
7955
- return true;
7956
7946
  }
7957
- if (ast.having && hasParamOperandsInExpression(ast.having)) {
7958
- return true;
7947
+ };
7948
+ var visitSelectQuery = (ast, visitor) => {
7949
+ visitor.visitSelectQuery?.(ast);
7950
+ if (ast.ctes) {
7951
+ for (const cte of ast.ctes) {
7952
+ visitor.visitCte?.(cte);
7953
+ visitSelectQuery(cte.query, visitor);
7954
+ }
7959
7955
  }
7956
+ visitTableSource(ast.from, visitor);
7960
7957
  ast.columns?.forEach((col2) => {
7961
- if (typeof col2 === "object" && col2 !== null && "type" in col2) {
7962
- if (hasParamOperandsInOperand(col2)) {
7963
- return true;
7964
- }
7965
- }
7958
+ visitOperandNode(col2, visitor);
7966
7959
  });
7967
- ast.orderBy?.forEach((ord) => {
7968
- if (ord.term) {
7969
- if (hasParamOperandsInOperand(ord.term)) {
7970
- return true;
7971
- }
7972
- }
7960
+ ast.joins?.forEach((join) => {
7961
+ visitor.visitJoin?.(join);
7962
+ visitTableSource(join.table, visitor);
7963
+ visitExpressionNode(join.condition, visitor);
7973
7964
  });
7974
- if (ast.ctes) {
7975
- for (const cte of ast.ctes) {
7976
- if (cte.query.where && hasParamOperandsInExpression(cte.query.where)) {
7977
- return true;
7978
- }
7979
- }
7965
+ if (ast.where) {
7966
+ visitExpressionNode(ast.where, visitor);
7980
7967
  }
7981
- if (ast.setOps) {
7982
- for (const op of ast.setOps) {
7983
- if (hasParamOperandsInQuery(op.query)) {
7984
- return true;
7985
- }
7986
- }
7968
+ ast.groupBy?.forEach((term) => {
7969
+ visitOrderingTerm(term, visitor);
7970
+ });
7971
+ if (ast.having) {
7972
+ visitExpressionNode(ast.having, visitor);
7987
7973
  }
7988
- return false;
7974
+ ast.orderBy?.forEach((order) => {
7975
+ visitOrderByNode(order, visitor);
7976
+ });
7977
+ ast.distinct?.forEach((col2) => {
7978
+ visitOperandNode(col2, visitor);
7979
+ });
7980
+ ast.setOps?.forEach((op) => {
7981
+ visitor.visitSetOperation?.(op);
7982
+ visitSelectQuery(op.query, visitor);
7983
+ });
7984
+ };
7985
+
7986
+ // src/core/ast/ast-validation.ts
7987
+ var hasParamOperandsInQuery = (ast) => {
7988
+ let hasParams = false;
7989
+ visitSelectQuery(ast, {
7990
+ visitParam: () => {
7991
+ hasParams = true;
7992
+ }
7993
+ });
7994
+ return hasParams;
7989
7995
  };
7990
7996
 
7991
7997
  // src/query-builder/select.ts