metal-orm 1.0.85 → 1.0.87

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
@@ -175,8 +175,10 @@ __export(index_exports, {
175
175
  groupConcat: () => groupConcat,
176
176
  gt: () => gt,
177
177
  gte: () => gte,
178
+ hasExpressionDispatcher: () => hasExpressionDispatcher,
178
179
  hasMany: () => hasMany,
179
180
  hasOne: () => hasOne,
181
+ hasOperandDispatcher: () => hasOperandDispatcher,
180
182
  hour: () => hour,
181
183
  hydrateRows: () => hydrateRows,
182
184
  ifNull: () => ifNull,
@@ -1054,6 +1056,8 @@ var registerExpressionDispatcher = (type, dispatcher) => {
1054
1056
  var registerOperandDispatcher = (type, dispatcher) => {
1055
1057
  operandRegistry = operandRegistry.register(type, dispatcher);
1056
1058
  };
1059
+ var hasExpressionDispatcher = (type) => expressionRegistry.get(type) !== void 0;
1060
+ var hasOperandDispatcher = (type) => operandRegistry.get(type) !== void 0;
1057
1061
  var clearExpressionDispatchers = () => {
1058
1062
  expressionRegistry = expressionRegistry.clear();
1059
1063
  };
@@ -1148,6 +1152,12 @@ var visitOperand = (node, visitor) => {
1148
1152
  case "Collate":
1149
1153
  if (visitor.visitCollate) return visitor.visitCollate(node);
1150
1154
  break;
1155
+ case "ArithmeticExpression":
1156
+ if (visitor.visitArithmeticExpression) return visitor.visitArithmeticExpression(node);
1157
+ break;
1158
+ case "BitwiseExpression":
1159
+ if (visitor.visitBitwiseExpression) return visitor.visitBitwiseExpression(node);
1160
+ break;
1151
1161
  default:
1152
1162
  break;
1153
1163
  }
@@ -7651,7 +7661,7 @@ var collectFilterColumns = (expr, table, rootTables) => {
7651
7661
  columns.add(node.name);
7652
7662
  }
7653
7663
  };
7654
- const visitOrderingTerm2 = (term) => {
7664
+ const visitOrderingTerm = (term) => {
7655
7665
  if (!term || typeof term !== "object") return;
7656
7666
  if (isOperandNode(term)) {
7657
7667
  visitOperand2(term);
@@ -7663,7 +7673,7 @@ var collectFilterColumns = (expr, table, rootTables) => {
7663
7673
  };
7664
7674
  const visitOrderBy = (orderBy) => {
7665
7675
  if (!orderBy) return;
7666
- orderBy.forEach((node) => visitOrderingTerm2(node.term));
7676
+ orderBy.forEach((node) => visitOrderingTerm(node.term));
7667
7677
  };
7668
7678
  const visitOperand2 = (node) => {
7669
7679
  switch (node.type) {
@@ -7815,137 +7825,174 @@ var getNodeType3 = (value) => {
7815
7825
  }
7816
7826
  return void 0;
7817
7827
  };
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);
7849
- if (node.escape) {
7850
- visitOperandNode(node.escape, visitor);
7851
- }
7828
+ var visitSelectQuery = (ast, visitor) => {
7829
+ const visitExpressionNode = (node) => {
7830
+ visitExpression(node, expressionVisitor);
7831
+ };
7832
+ const visitOperandNode = (node) => {
7833
+ visitOperand(node, operandVisitor);
7834
+ };
7835
+ const visitOrderingTerm = (term) => {
7836
+ if (!term || typeof term !== "object") return;
7837
+ if (isOperandNode(term)) {
7838
+ visitOperandNode(term);
7852
7839
  return;
7853
- case "LogicalExpression":
7854
- node.operands.forEach((operand) => visitExpressionNode(operand, visitor));
7840
+ }
7841
+ const type = getNodeType3(term);
7842
+ if (type && hasOperandDispatcher(type)) {
7843
+ visitOperandNode(term);
7855
7844
  return;
7856
- case "NullExpression":
7857
- visitOperandNode(node.left, visitor);
7845
+ }
7846
+ if (type) {
7847
+ visitExpressionNode(term);
7848
+ }
7849
+ };
7850
+ const visitOrderByNode = (node) => {
7851
+ visitor.visitOrderBy?.(node);
7852
+ visitOrderingTerm(node.term);
7853
+ };
7854
+ const visitTableSource = (source) => {
7855
+ visitor.visitTableSource?.(source);
7856
+ if (source.type === "DerivedTable") {
7857
+ visitor.visitDerivedTable?.(source);
7858
+ visitSelectQuery(source.query, visitor);
7858
7859
  return;
7859
- case "InExpression":
7860
- visitOperandNode(node.left, visitor);
7860
+ }
7861
+ if (source.type === "FunctionTable") {
7862
+ visitor.visitFunctionTable?.(source);
7863
+ source.args?.forEach((arg) => visitOperandNode(arg));
7864
+ }
7865
+ };
7866
+ const expressionVisitor = {
7867
+ visitBinaryExpression: (node) => {
7868
+ visitor.visitExpression?.(node);
7869
+ visitOperandNode(node.left);
7870
+ visitOperandNode(node.right);
7871
+ if (node.escape) {
7872
+ visitOperandNode(node.escape);
7873
+ }
7874
+ },
7875
+ visitLogicalExpression: (node) => {
7876
+ visitor.visitExpression?.(node);
7877
+ node.operands.forEach((operand) => visitExpressionNode(operand));
7878
+ },
7879
+ visitNullExpression: (node) => {
7880
+ visitor.visitExpression?.(node);
7881
+ visitOperandNode(node.left);
7882
+ },
7883
+ visitInExpression: (node) => {
7884
+ visitor.visitExpression?.(node);
7885
+ visitOperandNode(node.left);
7861
7886
  if (Array.isArray(node.right)) {
7862
- node.right.forEach((operand) => visitOperandNode(operand, visitor));
7887
+ node.right.forEach((operand) => visitOperandNode(operand));
7863
7888
  } else {
7864
- visitOperandNode(node.right, visitor);
7889
+ visitOperandNode(node.right);
7865
7890
  }
7866
- return;
7867
- case "ExistsExpression":
7891
+ },
7892
+ visitExistsExpression: (node) => {
7893
+ visitor.visitExpression?.(node);
7868
7894
  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;
7895
+ },
7896
+ visitBetweenExpression: (node) => {
7897
+ visitor.visitExpression?.(node);
7898
+ visitOperandNode(node.left);
7899
+ visitOperandNode(node.lower);
7900
+ visitOperandNode(node.upper);
7901
+ },
7902
+ visitArithmeticExpression: (node) => {
7903
+ visitor.visitExpression?.(node);
7904
+ visitOperandNode(node.left);
7905
+ visitOperandNode(node.right);
7906
+ },
7907
+ visitBitwiseExpression: (node) => {
7908
+ visitor.visitExpression?.(node);
7909
+ visitOperandNode(node.left);
7910
+ visitOperandNode(node.right);
7911
+ },
7912
+ visitOperand: (node) => {
7913
+ visitOperandNode(node);
7914
+ },
7915
+ visitSelectQuery: (node) => {
7916
+ visitSelectQuery(node, visitor);
7917
+ },
7918
+ otherwise: (node) => {
7919
+ visitor.visitExpression?.(node);
7885
7920
  }
7886
- }
7887
- };
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));
7921
+ };
7922
+ const operandVisitor = {
7923
+ visitColumn: (node) => {
7924
+ visitor.visitOperand?.(node);
7925
+ },
7926
+ visitLiteral: (node) => {
7927
+ visitor.visitOperand?.(node);
7928
+ },
7929
+ visitParam: (node) => {
7930
+ visitor.visitOperand?.(node);
7931
+ visitor.visitParam?.(node);
7932
+ },
7933
+ visitFunction: (node) => {
7934
+ visitor.visitOperand?.(node);
7935
+ node.args?.forEach((arg) => visitOperandNode(arg));
7936
+ node.orderBy?.forEach((order) => visitOrderByNode(order));
7904
7937
  if (node.separator) {
7905
- visitOperandNode(node.separator, visitor);
7938
+ visitOperandNode(node.separator);
7906
7939
  }
7907
- return;
7908
- case "JsonPath":
7909
- visitOperandNode(node.column, visitor);
7910
- return;
7911
- case "ScalarSubquery":
7940
+ },
7941
+ visitJsonPath: (node) => {
7942
+ visitor.visitOperand?.(node);
7943
+ visitOperandNode(node.column);
7944
+ },
7945
+ visitScalarSubquery: (node) => {
7946
+ visitor.visitOperand?.(node);
7912
7947
  visitSelectQuery(node.query, visitor);
7913
- return;
7914
- case "CaseExpression":
7948
+ },
7949
+ visitCaseExpression: (node) => {
7950
+ visitor.visitOperand?.(node);
7915
7951
  node.conditions.forEach((cond) => {
7916
- visitExpressionNode(cond.when, visitor);
7917
- visitOperandNode(cond.then, visitor);
7952
+ visitExpressionNode(cond.when);
7953
+ visitOperandNode(cond.then);
7918
7954
  });
7919
7955
  if (node.else) {
7920
- visitOperandNode(node.else, visitor);
7956
+ visitOperandNode(node.else);
7921
7957
  }
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;
7958
+ },
7959
+ visitCast: (node) => {
7960
+ visitor.visitOperand?.(node);
7961
+ visitOperandNode(node.expression);
7962
+ },
7963
+ visitWindowFunction: (node) => {
7964
+ visitor.visitOperand?.(node);
7965
+ node.args?.forEach((arg) => visitOperandNode(arg));
7966
+ node.partitionBy?.forEach((term) => visitOperandNode(term));
7967
+ node.orderBy?.forEach((order) => visitOrderByNode(order));
7968
+ },
7969
+ visitArithmeticExpression: (node) => {
7970
+ visitor.visitOperand?.(node);
7971
+ visitOperandNode(node.left);
7972
+ visitOperandNode(node.right);
7973
+ },
7974
+ visitBitwiseExpression: (node) => {
7975
+ visitor.visitOperand?.(node);
7976
+ visitOperandNode(node.left);
7977
+ visitOperandNode(node.right);
7978
+ },
7979
+ visitExpression: (node) => {
7980
+ visitExpressionNode(node);
7981
+ },
7982
+ visitSelectQuery: (node) => {
7983
+ visitSelectQuery(node, visitor);
7984
+ },
7985
+ visitCollate: (node) => {
7986
+ visitor.visitOperand?.(node);
7987
+ visitOperandNode(node.expression);
7988
+ },
7989
+ visitAliasRef: (node) => {
7990
+ visitor.visitOperand?.(node);
7991
+ },
7992
+ otherwise: (node) => {
7993
+ visitor.visitOperand?.(node);
7945
7994
  }
7946
- }
7947
- };
7948
- var visitSelectQuery = (ast, visitor) => {
7995
+ };
7949
7996
  visitor.visitSelectQuery?.(ast);
7950
7997
  if (ast.ctes) {
7951
7998
  for (const cte of ast.ctes) {
@@ -7953,29 +8000,29 @@ var visitSelectQuery = (ast, visitor) => {
7953
8000
  visitSelectQuery(cte.query, visitor);
7954
8001
  }
7955
8002
  }
7956
- visitTableSource(ast.from, visitor);
8003
+ visitTableSource(ast.from);
7957
8004
  ast.columns?.forEach((col2) => {
7958
- visitOperandNode(col2, visitor);
8005
+ visitOperandNode(col2);
7959
8006
  });
7960
8007
  ast.joins?.forEach((join) => {
7961
8008
  visitor.visitJoin?.(join);
7962
- visitTableSource(join.table, visitor);
7963
- visitExpressionNode(join.condition, visitor);
8009
+ visitTableSource(join.table);
8010
+ visitExpressionNode(join.condition);
7964
8011
  });
7965
8012
  if (ast.where) {
7966
- visitExpressionNode(ast.where, visitor);
8013
+ visitExpressionNode(ast.where);
7967
8014
  }
7968
8015
  ast.groupBy?.forEach((term) => {
7969
- visitOrderingTerm(term, visitor);
8016
+ visitOrderingTerm(term);
7970
8017
  });
7971
8018
  if (ast.having) {
7972
- visitExpressionNode(ast.having, visitor);
8019
+ visitExpressionNode(ast.having);
7973
8020
  }
7974
8021
  ast.orderBy?.forEach((order) => {
7975
- visitOrderByNode(order, visitor);
8022
+ visitOrderByNode(order);
7976
8023
  });
7977
8024
  ast.distinct?.forEach((col2) => {
7978
- visitOperandNode(col2, visitor);
8025
+ visitOperandNode(col2);
7979
8026
  });
7980
8027
  ast.setOps?.forEach((op) => {
7981
8028
  visitor.visitSetOperation?.(op);
@@ -7984,14 +8031,16 @@ var visitSelectQuery = (ast, visitor) => {
7984
8031
  };
7985
8032
 
7986
8033
  // src/core/ast/ast-validation.ts
7987
- var hasParamOperandsInQuery = (ast) => {
7988
- let hasParams = false;
8034
+ var findFirstParamOperandName = (ast) => {
8035
+ let name;
7989
8036
  visitSelectQuery(ast, {
7990
- visitParam: () => {
7991
- hasParams = true;
8037
+ visitParam: (node) => {
8038
+ if (!name) {
8039
+ name = node.name;
8040
+ }
7992
8041
  }
7993
8042
  });
7994
- return hasParams;
8043
+ return name;
7995
8044
  };
7996
8045
 
7997
8046
  // src/query-builder/select.ts
@@ -8473,9 +8522,9 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
8473
8522
  */
8474
8523
  validateNoParamOperands() {
8475
8524
  const ast = this.context.hydration.applyToAst(this.context.state.ast);
8476
- const hasParams = hasParamOperandsInQuery(ast);
8477
- if (hasParams) {
8478
- throw new Error("Cannot execute query containing Param operands. Param proxies are only for schema generation (getSchema()). If you need real parameters, use literal values.");
8525
+ const paramName = findFirstParamOperandName(ast);
8526
+ if (paramName) {
8527
+ throw new Error(`Cannot execute query containing Param operand "${paramName}". Param proxies are only for schema generation (getSchema()). If you need real parameters, use literal values.`);
8479
8528
  }
8480
8529
  }
8481
8530
  /**
@@ -8802,9 +8851,17 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
8802
8851
  * .compile('postgres');
8803
8852
  * console.log(compiled.sql); // SELECT "id", "name" FROM "users" WHERE "active" = true
8804
8853
  */
8805
- compile(dialect) {
8854
+ compile(dialect, options) {
8806
8855
  const resolved = resolveDialectInput(dialect);
8807
- return resolved.compileSelect(this.getAST());
8856
+ const ast = this.getAST();
8857
+ if (!options?.allowParamOperands) {
8858
+ const paramName = findFirstParamOperandName(ast);
8859
+ if (paramName) {
8860
+ throw new Error(`Cannot compile query containing Param operand "${paramName}". Param proxies are only for schema generation (getSchema()). If you need real parameters, use literal values.`);
8861
+ }
8862
+ return resolved.compileSelect(ast);
8863
+ }
8864
+ return resolved.compileSelectWithOptions(ast, { allowParams: true });
8808
8865
  }
8809
8866
  /**
8810
8867
  * Converts the query to SQL string for a specific dialect
@@ -8816,8 +8873,8 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
8816
8873
  * .toSql('postgres');
8817
8874
  * console.log(sql); // SELECT "id", "name" FROM "users" WHERE "active" = true
8818
8875
  */
8819
- toSql(dialect) {
8820
- return this.compile(dialect).sql;
8876
+ toSql(dialect, options) {
8877
+ return this.compile(dialect, options).sql;
8821
8878
  }
8822
8879
  /**
8823
8880
  * Gets hydration plan for query
@@ -14506,8 +14563,10 @@ function createPooledExecutorFactory(opts) {
14506
14563
  groupConcat,
14507
14564
  gt,
14508
14565
  gte,
14566
+ hasExpressionDispatcher,
14509
14567
  hasMany,
14510
14568
  hasOne,
14569
+ hasOperandDispatcher,
14511
14570
  hour,
14512
14571
  hydrateRows,
14513
14572
  ifNull,