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.d.cts CHANGED
@@ -2392,6 +2392,7 @@ declare class QueryAstService {
2392
2392
  * @returns Normalized ordering term
2393
2393
  */
2394
2394
  private normalizeOrderingTerm;
2395
+ private toParamNode;
2395
2396
  }
2396
2397
 
2397
2398
  /**
package/dist/index.d.ts CHANGED
@@ -2392,6 +2392,7 @@ declare class QueryAstService {
2392
2392
  * @returns Normalized ordering term
2393
2393
  */
2394
2394
  private normalizeOrderingTerm;
2395
+ private toParamNode;
2395
2396
  }
2396
2397
 
2397
2398
  /**
package/dist/index.js CHANGED
@@ -371,16 +371,27 @@ var operandTypes = /* @__PURE__ */ new Set([
371
371
  "BitwiseExpression",
372
372
  "Collate"
373
373
  ]);
374
- var hasTypeProperty = (value) => typeof value === "object" && value !== null && "type" in value;
374
+ var getNodeType = (value) => {
375
+ if (typeof value !== "object" || value === null) return void 0;
376
+ const descriptor = Object.getOwnPropertyDescriptor(value, "type");
377
+ if (descriptor && typeof descriptor.value === "string") {
378
+ return descriptor.value;
379
+ }
380
+ if ("type" in value) {
381
+ const type = value.type;
382
+ return typeof type === "string" ? type : void 0;
383
+ }
384
+ return void 0;
385
+ };
375
386
  var isOperandNode = (node) => {
376
- if (!hasTypeProperty(node)) return false;
377
- return operandTypes.has(node.type);
378
- };
379
- var isFunctionNode = (node) => isOperandNode(node) && node.type === "Function";
380
- var isCaseExpressionNode = (node) => isOperandNode(node) && node.type === "CaseExpression";
381
- var isCastExpressionNode = (node) => isOperandNode(node) && node.type === "Cast";
382
- var isCollateExpressionNode = (node) => isOperandNode(node) && node.type === "Collate";
383
- var isWindowFunctionNode = (node) => isOperandNode(node) && node.type === "WindowFunction";
387
+ const type = getNodeType(node);
388
+ return type !== void 0 && operandTypes.has(type);
389
+ };
390
+ var isFunctionNode = (node) => isOperandNode(node) && getNodeType(node) === "Function";
391
+ var isCaseExpressionNode = (node) => isOperandNode(node) && getNodeType(node) === "CaseExpression";
392
+ var isCastExpressionNode = (node) => isOperandNode(node) && getNodeType(node) === "Cast";
393
+ var isCollateExpressionNode = (node) => isOperandNode(node) && getNodeType(node) === "Collate";
394
+ var isWindowFunctionNode = (node) => isOperandNode(node) && getNodeType(node) === "WindowFunction";
384
395
  var isExpressionSelectionNode = (node) => isFunctionNode(node) || isCaseExpressionNode(node) || isCastExpressionNode(node) || isWindowFunctionNode(node);
385
396
 
386
397
  // src/core/ast/expression-builders.ts
@@ -765,17 +776,26 @@ var clearExpressionDispatchers = () => {
765
776
  var clearOperandDispatchers = () => {
766
777
  operandRegistry = operandRegistry.clear();
767
778
  };
768
- var getNodeType = (node) => typeof node === "object" && node !== null && typeof node.type === "string" ? node.type : void 0;
779
+ var getNodeType2 = (node) => {
780
+ if (typeof node !== "object" || node === null) return void 0;
781
+ const descriptor = Object.getOwnPropertyDescriptor(node, "type");
782
+ if (descriptor && typeof descriptor.value === "string") {
783
+ return descriptor.value;
784
+ }
785
+ const type = node.type;
786
+ return typeof type === "string" ? type : void 0;
787
+ };
769
788
  var unsupportedExpression = (node) => {
770
- throw new Error(`Unsupported expression type "${getNodeType(node) ?? "unknown"}"`);
789
+ throw new Error(`Unsupported expression type "${getNodeType2(node) ?? "unknown"}"`);
771
790
  };
772
791
  var unsupportedOperand = (node) => {
773
- throw new Error(`Unsupported operand type "${getNodeType(node) ?? "unknown"}"`);
792
+ throw new Error(`Unsupported operand type "${getNodeType2(node) ?? "unknown"}"`);
774
793
  };
775
794
  var visitExpression = (node, visitor) => {
776
- const dynamic = expressionRegistry.get(node.type);
795
+ const type = getNodeType2(node);
796
+ const dynamic = type ? expressionRegistry.get(type) : void 0;
777
797
  if (dynamic) return dynamic(node, visitor);
778
- switch (node.type) {
798
+ switch (type) {
779
799
  case "BinaryExpression":
780
800
  if (visitor.visitBinaryExpression) return visitor.visitBinaryExpression(node);
781
801
  break;
@@ -807,9 +827,10 @@ var visitExpression = (node, visitor) => {
807
827
  return unsupportedExpression(node);
808
828
  };
809
829
  var visitOperand = (node, visitor) => {
810
- const dynamic = operandRegistry.get(node.type);
830
+ const type = getNodeType2(node);
831
+ const dynamic = type ? operandRegistry.get(type) : void 0;
811
832
  if (dynamic) return dynamic(node, visitor);
812
- switch (node.type) {
833
+ switch (type) {
813
834
  case "Column":
814
835
  if (visitor.visitColumn) return visitor.visitColumn(node);
815
836
  break;
@@ -1538,9 +1559,11 @@ var Dialect = class _Dialect {
1538
1559
  * @returns Compiled SQL operand
1539
1560
  */
1540
1561
  compileOperand(node, ctx) {
1541
- const compiler = this.operandCompilers.get(node.type);
1562
+ const descriptor = Object.getOwnPropertyDescriptor(node, "type");
1563
+ const nodeType = typeof descriptor?.value === "string" ? descriptor.value : typeof node.type === "string" ? node.type : void 0;
1564
+ const compiler = nodeType ? this.operandCompilers.get(nodeType) : void 0;
1542
1565
  if (!compiler) {
1543
- throw new Error(`Unsupported operand node type "${node.type}" for ${this.constructor.name}`);
1566
+ throw new Error(`Unsupported operand node type "${nodeType ?? "unknown"}" for ${this.constructor.name}`);
1544
1567
  }
1545
1568
  return compiler(node, ctx);
1546
1569
  }
@@ -3861,6 +3884,10 @@ var QueryAstService = class {
3861
3884
  * @returns Normalized ordering term
3862
3885
  */
3863
3886
  normalizeOrderingTerm(term) {
3887
+ const paramNode = this.toParamNode(term);
3888
+ if (paramNode) {
3889
+ return paramNode;
3890
+ }
3864
3891
  const from = this.state.ast.from;
3865
3892
  const tableRef2 = from.type === "Table" && from.alias ? { ...this.table, alias: from.alias } : this.table;
3866
3893
  const termType = term.type;
@@ -3878,6 +3905,14 @@ var QueryAstService = class {
3878
3905
  }
3879
3906
  return buildColumnNode(tableRef2, term);
3880
3907
  }
3908
+ toParamNode(value) {
3909
+ if (typeof value !== "object" || value === null) return void 0;
3910
+ const type = Object.getOwnPropertyDescriptor(value, "type")?.value;
3911
+ if (type !== "Param") return void 0;
3912
+ const name = Object.getOwnPropertyDescriptor(value, "name")?.value;
3913
+ if (typeof name !== "string") return void 0;
3914
+ return { type: "Param", name };
3915
+ }
3881
3916
  };
3882
3917
 
3883
3918
  // src/query-builder/relation-projection-helper.ts
@@ -7332,7 +7367,7 @@ var collectFilterColumns = (expr, table, rootTables) => {
7332
7367
  columns.add(node.name);
7333
7368
  }
7334
7369
  };
7335
- const visitOrderingTerm = (term) => {
7370
+ const visitOrderingTerm2 = (term) => {
7336
7371
  if (!term || typeof term !== "object") return;
7337
7372
  if (isOperandNode(term)) {
7338
7373
  visitOperand2(term);
@@ -7344,7 +7379,7 @@ var collectFilterColumns = (expr, table, rootTables) => {
7344
7379
  };
7345
7380
  const visitOrderBy = (orderBy) => {
7346
7381
  if (!orderBy) return;
7347
- orderBy.forEach((node) => visitOrderingTerm(node.term));
7382
+ orderBy.forEach((node) => visitOrderingTerm2(node.term));
7348
7383
  };
7349
7384
  const visitOperand2 = (node) => {
7350
7385
  switch (node.type) {
@@ -7483,225 +7518,196 @@ var buildFilterParameters = (table, where, from, options = {}) => {
7483
7518
  }];
7484
7519
  };
7485
7520
 
7486
- // src/core/ast/ast-validation.ts
7487
- var hasParamOperandsInExpression = (expr) => {
7488
- let hasParams = false;
7489
- visitExpression(expr, {
7490
- visitBinaryExpression: (node) => {
7491
- visitOperand(node.left, {
7492
- visitParam: () => {
7493
- hasParams = true;
7494
- },
7495
- otherwise: () => {
7496
- }
7497
- });
7498
- visitOperand(node.right, {
7499
- visitParam: () => {
7500
- hasParams = true;
7501
- },
7502
- otherwise: () => {
7503
- }
7504
- });
7521
+ // src/core/ast/query-visitor.ts
7522
+ var getNodeType3 = (value) => {
7523
+ if (typeof value !== "object" || value === null) return void 0;
7524
+ const descriptor = Object.getOwnPropertyDescriptor(value, "type");
7525
+ if (descriptor && typeof descriptor.value === "string") {
7526
+ return descriptor.value;
7527
+ }
7528
+ if ("type" in value) {
7529
+ const type = value.type;
7530
+ return typeof type === "string" ? type : void 0;
7531
+ }
7532
+ return void 0;
7533
+ };
7534
+ var visitOrderingTerm = (term, visitor) => {
7535
+ if (isOperandNode(term)) {
7536
+ visitOperandNode(term, visitor);
7537
+ return;
7538
+ }
7539
+ visitExpressionNode(term, visitor);
7540
+ };
7541
+ var visitOrderByNode = (node, visitor) => {
7542
+ visitor.visitOrderBy?.(node);
7543
+ visitOrderingTerm(node.term, visitor);
7544
+ };
7545
+ var visitTableSource = (source, visitor) => {
7546
+ visitor.visitTableSource?.(source);
7547
+ if (source.type === "DerivedTable") {
7548
+ visitor.visitDerivedTable?.(source);
7549
+ visitSelectQuery(source.query, visitor);
7550
+ return;
7551
+ }
7552
+ if (source.type === "FunctionTable") {
7553
+ visitor.visitFunctionTable?.(source);
7554
+ source.args?.forEach((arg) => visitOperandNode(arg, visitor));
7555
+ }
7556
+ };
7557
+ var visitExpressionNode = (node, visitor) => {
7558
+ visitor.visitExpression?.(node);
7559
+ const type = getNodeType3(node);
7560
+ if (!type) return;
7561
+ switch (type) {
7562
+ case "BinaryExpression":
7563
+ visitOperandNode(node.left, visitor);
7564
+ visitOperandNode(node.right, visitor);
7505
7565
  if (node.escape) {
7506
- visitOperand(node.escape, {
7507
- visitParam: () => {
7508
- hasParams = true;
7509
- },
7510
- otherwise: () => {
7511
- }
7512
- });
7566
+ visitOperandNode(node.escape, visitor);
7513
7567
  }
7514
- },
7515
- visitLogicalExpression: (node) => {
7516
- node.operands.forEach((operand) => {
7517
- if (hasParamOperandsInExpression(operand)) {
7518
- hasParams = true;
7519
- }
7520
- });
7521
- },
7522
- visitNullExpression: () => {
7523
- },
7524
- visitInExpression: (node) => {
7525
- visitOperand(node.left, {
7526
- visitParam: () => {
7527
- hasParams = true;
7528
- },
7529
- otherwise: () => {
7530
- }
7531
- });
7568
+ return;
7569
+ case "LogicalExpression":
7570
+ node.operands.forEach((operand) => visitExpressionNode(operand, visitor));
7571
+ return;
7572
+ case "NullExpression":
7573
+ visitOperandNode(node.left, visitor);
7574
+ return;
7575
+ case "InExpression":
7576
+ visitOperandNode(node.left, visitor);
7532
7577
  if (Array.isArray(node.right)) {
7533
- node.right.forEach((operand) => visitOperand(operand, {
7534
- visitParam: () => {
7535
- hasParams = true;
7536
- },
7537
- otherwise: () => {
7538
- }
7539
- }));
7578
+ node.right.forEach((operand) => visitOperandNode(operand, visitor));
7579
+ } else {
7580
+ visitOperandNode(node.right, visitor);
7540
7581
  }
7541
- },
7542
- visitExistsExpression: () => {
7543
- },
7544
- visitBetweenExpression: (node) => {
7545
- visitOperand(node.left, {
7546
- visitParam: () => {
7547
- hasParams = true;
7548
- },
7549
- otherwise: () => {
7550
- }
7551
- });
7552
- visitOperand(node.lower, {
7553
- visitParam: () => {
7554
- hasParams = true;
7555
- },
7556
- otherwise: () => {
7557
- }
7558
- });
7559
- visitOperand(node.upper, {
7560
- visitParam: () => {
7561
- hasParams = true;
7562
- },
7563
- otherwise: () => {
7564
- }
7565
- });
7566
- },
7567
- visitArithmeticExpression: (node) => {
7568
- visitOperand(node.left, {
7569
- visitParam: () => {
7570
- hasParams = true;
7571
- },
7572
- otherwise: () => {
7573
- }
7574
- });
7575
- visitOperand(node.right, {
7576
- visitParam: () => {
7577
- hasParams = true;
7578
- },
7579
- otherwise: () => {
7580
- }
7581
- });
7582
- },
7583
- visitBitwiseExpression: (node) => {
7584
- visitOperand(node.left, {
7585
- visitParam: () => {
7586
- hasParams = true;
7587
- },
7588
- otherwise: () => {
7589
- }
7590
- });
7591
- visitOperand(node.right, {
7592
- visitParam: () => {
7593
- hasParams = true;
7594
- },
7595
- otherwise: () => {
7596
- }
7597
- });
7598
- },
7599
- otherwise: () => {
7582
+ return;
7583
+ case "ExistsExpression":
7584
+ visitSelectQuery(node.subquery, visitor);
7585
+ return;
7586
+ case "BetweenExpression":
7587
+ visitOperandNode(node.left, visitor);
7588
+ visitOperandNode(node.lower, visitor);
7589
+ visitOperandNode(node.upper, visitor);
7590
+ return;
7591
+ case "ArithmeticExpression":
7592
+ visitOperandNode(node.left, visitor);
7593
+ visitOperandNode(node.right, visitor);
7594
+ return;
7595
+ case "BitwiseExpression":
7596
+ visitOperandNode(node.left, visitor);
7597
+ visitOperandNode(node.right, visitor);
7598
+ return;
7599
+ default: {
7600
+ return;
7600
7601
  }
7601
- });
7602
- return hasParams;
7602
+ }
7603
7603
  };
7604
- var hasParamOperandsInOperand = (operand) => {
7605
- let hasParams = false;
7606
- visitOperand(operand, {
7607
- visitColumn: () => {
7608
- },
7609
- visitLiteral: () => {
7610
- },
7611
- visitParam: () => {
7612
- hasParams = true;
7613
- },
7614
- visitFunction: (node) => {
7615
- node.args?.forEach((arg) => {
7616
- if (hasParamOperandsInOperand(arg)) {
7617
- hasParams = true;
7618
- }
7619
- });
7620
- },
7621
- visitJsonPath: () => {
7622
- },
7623
- visitScalarSubquery: () => {
7624
- },
7625
- visitCaseExpression: (node) => {
7626
- node.conditions.forEach((cond) => {
7627
- if (hasParamOperandsInExpression(cond.when)) {
7628
- hasParams = true;
7629
- }
7630
- if (hasParamOperandsInOperand(cond.then)) {
7631
- hasParams = true;
7632
- }
7633
- });
7634
- if (node.else && hasParamOperandsInOperand(node.else)) {
7635
- hasParams = true;
7636
- }
7637
- },
7638
- visitCast: (node) => {
7639
- if (hasParamOperandsInOperand(node.expression)) {
7640
- hasParams = true;
7604
+ var visitOperandNode = (node, visitor) => {
7605
+ visitor.visitOperand?.(node);
7606
+ const type = getNodeType3(node);
7607
+ if (type === "Param") {
7608
+ visitor.visitParam?.(node);
7609
+ }
7610
+ if (!type) return;
7611
+ switch (type) {
7612
+ case "Column":
7613
+ case "Literal":
7614
+ case "Param":
7615
+ case "AliasRef":
7616
+ return;
7617
+ case "Function":
7618
+ node.args?.forEach((arg) => visitOperandNode(arg, visitor));
7619
+ node.orderBy?.forEach((order) => visitOrderByNode(order, visitor));
7620
+ if (node.separator) {
7621
+ visitOperandNode(node.separator, visitor);
7641
7622
  }
7642
- },
7643
- visitWindowFunction: (node) => {
7644
- node.args?.forEach((arg) => {
7645
- if (hasParamOperandsInOperand(arg)) {
7646
- hasParams = true;
7647
- }
7648
- });
7649
- node.orderBy?.forEach((ord) => {
7650
- if (ord.term) {
7651
- if (hasParamOperandsInOperand(ord.term)) {
7652
- hasParams = true;
7653
- }
7654
- }
7623
+ return;
7624
+ case "JsonPath":
7625
+ visitOperandNode(node.column, visitor);
7626
+ return;
7627
+ case "ScalarSubquery":
7628
+ visitSelectQuery(node.query, visitor);
7629
+ return;
7630
+ case "CaseExpression":
7631
+ node.conditions.forEach((cond) => {
7632
+ visitExpressionNode(cond.when, visitor);
7633
+ visitOperandNode(cond.then, visitor);
7655
7634
  });
7656
- },
7657
- visitCollate: (node) => {
7658
- if (hasParamOperandsInOperand(node.expression)) {
7659
- hasParams = true;
7635
+ if (node.else) {
7636
+ visitOperandNode(node.else, visitor);
7660
7637
  }
7661
- },
7662
- visitAliasRef: () => {
7663
- },
7664
- otherwise: () => {
7638
+ return;
7639
+ case "Cast":
7640
+ visitOperandNode(node.expression, visitor);
7641
+ return;
7642
+ case "WindowFunction":
7643
+ node.args?.forEach((arg) => visitOperandNode(arg, visitor));
7644
+ node.partitionBy?.forEach((term) => visitOperandNode(term, visitor));
7645
+ node.orderBy?.forEach((order) => visitOrderByNode(order, visitor));
7646
+ return;
7647
+ case "ArithmeticExpression":
7648
+ visitOperandNode(node.left, visitor);
7649
+ visitOperandNode(node.right, visitor);
7650
+ return;
7651
+ case "BitwiseExpression":
7652
+ visitOperandNode(node.left, visitor);
7653
+ visitOperandNode(node.right, visitor);
7654
+ return;
7655
+ case "Collate":
7656
+ visitOperandNode(node.expression, visitor);
7657
+ return;
7658
+ default: {
7659
+ const _exhaustive = node;
7660
+ return _exhaustive;
7665
7661
  }
7666
- });
7667
- return hasParams;
7668
- };
7669
- var hasParamOperandsInQuery = (ast) => {
7670
- if (ast.where && hasParamOperandsInExpression(ast.where)) {
7671
- return true;
7672
7662
  }
7673
- if (ast.having && hasParamOperandsInExpression(ast.having)) {
7674
- return true;
7663
+ };
7664
+ var visitSelectQuery = (ast, visitor) => {
7665
+ visitor.visitSelectQuery?.(ast);
7666
+ if (ast.ctes) {
7667
+ for (const cte of ast.ctes) {
7668
+ visitor.visitCte?.(cte);
7669
+ visitSelectQuery(cte.query, visitor);
7670
+ }
7675
7671
  }
7672
+ visitTableSource(ast.from, visitor);
7676
7673
  ast.columns?.forEach((col2) => {
7677
- if (typeof col2 === "object" && col2 !== null && "type" in col2) {
7678
- if (hasParamOperandsInOperand(col2)) {
7679
- return true;
7680
- }
7681
- }
7674
+ visitOperandNode(col2, visitor);
7682
7675
  });
7683
- ast.orderBy?.forEach((ord) => {
7684
- if (ord.term) {
7685
- if (hasParamOperandsInOperand(ord.term)) {
7686
- return true;
7687
- }
7688
- }
7676
+ ast.joins?.forEach((join) => {
7677
+ visitor.visitJoin?.(join);
7678
+ visitTableSource(join.table, visitor);
7679
+ visitExpressionNode(join.condition, visitor);
7689
7680
  });
7690
- if (ast.ctes) {
7691
- for (const cte of ast.ctes) {
7692
- if (cte.query.where && hasParamOperandsInExpression(cte.query.where)) {
7693
- return true;
7694
- }
7695
- }
7681
+ if (ast.where) {
7682
+ visitExpressionNode(ast.where, visitor);
7696
7683
  }
7697
- if (ast.setOps) {
7698
- for (const op of ast.setOps) {
7699
- if (hasParamOperandsInQuery(op.query)) {
7700
- return true;
7701
- }
7702
- }
7684
+ ast.groupBy?.forEach((term) => {
7685
+ visitOrderingTerm(term, visitor);
7686
+ });
7687
+ if (ast.having) {
7688
+ visitExpressionNode(ast.having, visitor);
7703
7689
  }
7704
- return false;
7690
+ ast.orderBy?.forEach((order) => {
7691
+ visitOrderByNode(order, visitor);
7692
+ });
7693
+ ast.distinct?.forEach((col2) => {
7694
+ visitOperandNode(col2, visitor);
7695
+ });
7696
+ ast.setOps?.forEach((op) => {
7697
+ visitor.visitSetOperation?.(op);
7698
+ visitSelectQuery(op.query, visitor);
7699
+ });
7700
+ };
7701
+
7702
+ // src/core/ast/ast-validation.ts
7703
+ var hasParamOperandsInQuery = (ast) => {
7704
+ let hasParams = false;
7705
+ visitSelectQuery(ast, {
7706
+ visitParam: () => {
7707
+ hasParams = true;
7708
+ }
7709
+ });
7710
+ return hasParams;
7705
7711
  };
7706
7712
 
7707
7713
  // src/query-builder/select.ts