metal-orm 1.0.81 → 1.0.82

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metal-orm",
3
- "version": "1.0.81",
3
+ "version": "1.0.82",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "engines": {
@@ -17,13 +17,14 @@ import {
17
17
  LiteralNode,
18
18
  FunctionNode,
19
19
  AliasRefNode,
20
- CastExpressionNode,
21
- CollateExpressionNode,
22
- ExpressionVisitor,
23
- OperandVisitor,
24
- visitExpression,
25
- visitOperand
26
- } from '../core/ast/expression.js';
20
+ CastExpressionNode,
21
+ CollateExpressionNode,
22
+ ParamNode,
23
+ ExpressionVisitor,
24
+ OperandVisitor,
25
+ visitExpression,
26
+ visitOperand
27
+ } from '../core/ast/expression.js';
27
28
  import { SQL_OPERATOR_REGISTRY } from '../core/sql/sql-operator-config.js';
28
29
  import { SqlOperator } from '../core/sql/sql.js';
29
30
  import { isRelationAlias } from '../query-builder/relation-alias.js';
@@ -190,13 +191,14 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
190
191
  case 'ScalarSubquery':
191
192
  case 'CaseExpression':
192
193
  case 'WindowFunction':
193
- case 'Cast':
194
- case 'Collate':
195
- return this.printOperand(term);
196
- default:
197
- return this.printExpression(term);
198
- }
199
- }
194
+ case 'Cast':
195
+ case 'Collate':
196
+ case 'Param':
197
+ return this.printOperand(term);
198
+ default:
199
+ return this.printExpression(term);
200
+ }
201
+ }
200
202
 
201
203
  private getSelectionKey(selection: SelectionColumn, index: number): string {
202
204
  if (selection.alias) {
@@ -244,13 +246,17 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
244
246
  return this.printColumnOperand(node);
245
247
  }
246
248
 
247
- public visitLiteral(node: LiteralNode): string {
248
- return this.printLiteralOperand(node);
249
- }
250
-
251
- public visitFunction(node: FunctionNode): string {
252
- return this.printFunctionOperand(node);
253
- }
249
+ public visitLiteral(node: LiteralNode): string {
250
+ return this.printLiteralOperand(node);
251
+ }
252
+
253
+ public visitParam(node: ParamNode): string {
254
+ return this.printParamOperand(node);
255
+ }
256
+
257
+ public visitFunction(node: FunctionNode): string {
258
+ return this.printFunctionOperand(node);
259
+ }
254
260
 
255
261
  public visitJsonPath(node: JsonPathNode): string {
256
262
  return this.printJsonPathOperand(node);
@@ -379,14 +385,19 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
379
385
  * @param literal - Literal node
380
386
  * @returns TypeScript code representation
381
387
  */
382
- private printLiteralOperand(literal: LiteralNode): string {
383
- if (literal.value === null) return 'null';
384
- return typeof literal.value === 'string' ? `'${literal.value}'` : String(literal.value);
385
- }
386
-
387
- /**
388
- * Prints a function operand to TypeScript code
389
- * @param fn - Function node
388
+ private printLiteralOperand(literal: LiteralNode): string {
389
+ if (literal.value === null) return 'null';
390
+ return typeof literal.value === 'string' ? `'${literal.value}'` : String(literal.value);
391
+ }
392
+
393
+ private printParamOperand(param: ParamNode): string {
394
+ const name = param.name.replace(/'/g, "\\'");
395
+ return `{ type: 'Param', name: '${name}' }`;
396
+ }
397
+
398
+ /**
399
+ * Prints a function operand to TypeScript code
400
+ * @param fn - Function node
390
401
  * @returns TypeScript code representation
391
402
  */
392
403
  private printFunctionOperand(fn: FunctionNode): string {
@@ -2,14 +2,23 @@ import type { SelectQueryNode, OrderByNode } from './query.js';
2
2
  import { SqlOperator, BitwiseOperator } from '../sql/sql.js';
3
3
  import { ColumnRef } from './types.js';
4
4
 
5
- /**
6
- * AST node representing a literal value
7
- */
5
+ /**
6
+ * AST node representing a literal value
7
+ */
8
8
  export interface LiteralNode {
9
9
  type: 'Literal';
10
10
  /** The literal value (string, number, boolean, Date, or null) */
11
11
  value: string | number | boolean | Date | null;
12
12
  }
13
+
14
+ /**
15
+ * AST node representing a named parameter placeholder
16
+ */
17
+ export interface ParamNode {
18
+ type: 'Param';
19
+ /** Stable parameter name */
20
+ name: string;
21
+ }
13
22
 
14
23
  /**
15
24
  * AST node representing a reference to a SELECT alias (for ORDER BY / GROUP BY).
@@ -149,29 +158,31 @@ export interface ArithmeticExpressionNode {
149
158
  /**
150
159
  * Union type representing any operand that can be used in expressions
151
160
  */
152
- export type OperandNode =
153
- | AliasRefNode
154
- | ColumnNode
155
- | LiteralNode
156
- | FunctionNode
157
- | JsonPathNode
158
- | ScalarSubqueryNode
159
- | CaseExpressionNode
160
- | CastExpressionNode
161
+ export type OperandNode =
162
+ | AliasRefNode
163
+ | ColumnNode
164
+ | LiteralNode
165
+ | ParamNode
166
+ | FunctionNode
167
+ | JsonPathNode
168
+ | ScalarSubqueryNode
169
+ | CaseExpressionNode
170
+ | CastExpressionNode
161
171
  | WindowFunctionNode
162
172
  | ArithmeticExpressionNode
163
173
  | BitwiseExpressionNode
164
174
  | CollateExpressionNode;
165
175
 
166
- const operandTypes = new Set<OperandNode['type']>([
167
- 'AliasRef',
168
- 'Column',
169
- 'Literal',
170
- 'Function',
171
- 'JsonPath',
172
- 'ScalarSubquery',
173
- 'CaseExpression',
174
- 'Cast',
176
+ const operandTypes = new Set<OperandNode['type']>([
177
+ 'AliasRef',
178
+ 'Column',
179
+ 'Literal',
180
+ 'Param',
181
+ 'Function',
182
+ 'JsonPath',
183
+ 'ScalarSubquery',
184
+ 'CaseExpression',
185
+ 'Cast',
175
186
  'WindowFunction',
176
187
  'ArithmeticExpression',
177
188
  'BitwiseExpression',
@@ -18,7 +18,8 @@ import {
18
18
  WindowFunctionNode,
19
19
  CollateExpressionNode,
20
20
  AliasRefNode,
21
- BitwiseExpressionNode
21
+ BitwiseExpressionNode,
22
+ ParamNode
22
23
  } from './expression-nodes.js';
23
24
 
24
25
  /**
@@ -42,6 +43,7 @@ export interface ExpressionVisitor<R> {
42
43
  export interface OperandVisitor<R> {
43
44
  visitColumn?(node: ColumnNode): R;
44
45
  visitLiteral?(node: LiteralNode): R;
46
+ visitParam?(node: ParamNode): R;
45
47
  visitFunction?(node: FunctionNode): R;
46
48
  visitJsonPath?(node: JsonPathNode): R;
47
49
  visitScalarSubquery?(node: ScalarSubqueryNode): R;
@@ -187,6 +189,9 @@ export const visitOperand = <R>(node: OperandNode, visitor: OperandVisitor<R>):
187
189
  case 'Literal':
188
190
  if (visitor.visitLiteral) return visitor.visitLiteral(node);
189
191
  break;
192
+ case 'Param':
193
+ if (visitor.visitParam) return visitor.visitParam(node);
194
+ break;
190
195
  case 'Function':
191
196
  if (visitor.visitFunction) return visitor.visitFunction(node);
192
197
  break;
@@ -9,6 +9,7 @@ export * from './expression-builders.js';
9
9
  export * from './window-functions.js';
10
10
  export * from './aggregate-functions.js';
11
11
  export * from './expression-visitor.js';
12
+ export * from './param-proxy.js';
12
13
  export type { ColumnRef, TableRef as AstTableRef } from './types.js';
13
14
  export * from './adapters.js';
14
15
 
@@ -0,0 +1,50 @@
1
+ import type { ParamNode } from './expression-nodes.js';
2
+
3
+ export type ParamProxy = ParamNode & {
4
+ [key: string]: ParamProxy;
5
+ };
6
+
7
+ export type ParamProxyRoot = {
8
+ [key: string]: ParamProxy;
9
+ };
10
+
11
+ const buildParamProxy = (name: string): ParamProxy => {
12
+ const target: ParamNode = { type: 'Param', name };
13
+ return new Proxy(target, {
14
+ get(t, prop, receiver) {
15
+ if (prop === 'then') return undefined;
16
+ if (typeof prop === 'symbol') {
17
+ return Reflect.get(t, prop, receiver);
18
+ }
19
+ if (typeof prop === 'string' && prop.startsWith('$')) {
20
+ const trimmed = prop.slice(1);
21
+ const nextName = name ? `${name}.${trimmed}` : trimmed;
22
+ return buildParamProxy(nextName);
23
+ }
24
+ if (prop in t) {
25
+ return (t as unknown as Record<string, unknown>)[prop];
26
+ }
27
+ const nextName = name ? `${name}.${prop}` : prop;
28
+ return buildParamProxy(nextName);
29
+ }
30
+ }) as ParamProxy;
31
+ };
32
+
33
+ export const createParamProxy = (): ParamProxyRoot => {
34
+ const target: Record<string, unknown> = {};
35
+ return new Proxy(target, {
36
+ get(t, prop, receiver) {
37
+ if (prop === 'then') return undefined;
38
+ if (typeof prop === 'symbol') {
39
+ return Reflect.get(t, prop, receiver);
40
+ }
41
+ if (typeof prop === 'string' && prop.startsWith('$')) {
42
+ return buildParamProxy(prop.slice(1));
43
+ }
44
+ if (prop in t) {
45
+ return (t as unknown as Record<string, unknown>)[prop];
46
+ }
47
+ return buildParamProxy(String(prop));
48
+ }
49
+ }) as ParamProxyRoot;
50
+ };
@@ -26,10 +26,11 @@ import {
26
26
  BetweenExpressionNode,
27
27
  ArithmeticExpressionNode,
28
28
  BitwiseExpressionNode,
29
- CollateExpressionNode,
30
- AliasRefNode,
31
- isOperandNode
32
- } from '../ast/expression.js';
29
+ CollateExpressionNode,
30
+ AliasRefNode,
31
+ isOperandNode,
32
+ ParamNode
33
+ } from '../ast/expression.js';
33
34
  import { DialectName } from '../sql/sql.js';
34
35
  import type { FunctionStrategy } from '../functions/types.js';
35
36
  import { StandardFunctionStrategy } from '../functions/standard-strategy.js';
@@ -453,12 +454,13 @@ export abstract class Dialect
453
454
  }
454
455
 
455
456
  private registerDefaultOperandCompilers(): void {
456
- this.registerOperandCompiler('Literal', (literal: LiteralNode, ctx) => ctx.addParameter(literal.value));
457
-
458
- this.registerOperandCompiler('AliasRef', (alias: AliasRefNode, _ctx) => {
459
- void _ctx;
460
- return this.quoteIdentifier(alias.name);
461
- });
457
+ this.registerOperandCompiler('Literal', (literal: LiteralNode, ctx) => ctx.addParameter(literal.value));
458
+ this.registerOperandCompiler('Param', (_param: ParamNode, ctx) => ctx.addParameter(null));
459
+
460
+ this.registerOperandCompiler('AliasRef', (alias: AliasRefNode, _ctx) => {
461
+ void _ctx;
462
+ return this.quoteIdentifier(alias.name);
463
+ });
462
464
 
463
465
  this.registerOperandCompiler('Column', (column: ColumnNode, _ctx) => {
464
466
  void _ctx;
@@ -117,6 +117,7 @@ const collectFilterColumns = (
117
117
  }
118
118
  case 'AliasRef':
119
119
  case 'Literal':
120
+ case 'Param':
120
121
  return;
121
122
  default:
122
123
  return;
@@ -144,6 +144,7 @@ const collectFromOperand = (node: OperandNode, collector: FilterTableCollector):
144
144
  break;
145
145
  case 'Literal':
146
146
  case 'AliasRef':
147
+ case 'Param':
147
148
  break;
148
149
  default:
149
150
  break;