metal-orm 1.0.39 → 1.0.40

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.
@@ -1,18 +1,19 @@
1
- import {
2
- ColumnNode,
3
- FunctionNode,
4
- ExpressionNode,
5
- ScalarSubqueryNode,
6
- CaseExpressionNode,
7
- WindowFunctionNode,
8
- OperandNode
9
- } from './expression.js';
10
- import { JoinNode } from './join.js';
11
- import { OrderDirection } from '../sql/sql.js';
12
-
13
- /**
14
- * AST node representing a table reference in a query
15
- */
1
+ import {
2
+ ColumnNode,
3
+ FunctionNode,
4
+ ExpressionNode,
5
+ ScalarSubqueryNode,
6
+ CaseExpressionNode,
7
+ WindowFunctionNode,
8
+ OperandNode,
9
+ AliasRefNode
10
+ } from './expression.js';
11
+ import { JoinNode } from './join.js';
12
+ import { OrderDirection } from '../sql/sql.js';
13
+
14
+ /**
15
+ * AST node representing a table reference in a query
16
+ */
16
17
  export interface TableNode {
17
18
  type: 'Table';
18
19
  /** Table name */
@@ -25,18 +26,18 @@ export interface TableNode {
25
26
 
26
27
  /**
27
28
  * AST node representing a function used as a table source (table-valued function)
28
- */
29
- export interface FunctionTableNode {
30
- type: 'FunctionTable';
31
- /** Function name */
32
- name: string;
33
- /** Optional schema for the function (some dialects) */
34
- schema?: string;
35
- /** Function arguments as operand nodes */
36
- args?: any[]; // use any to avoid circular import here; caller should supply OperandNode
37
- /** Optional alias for the function table */
38
- alias?: string;
39
- /** LATERAL flag */
29
+ */
30
+ export interface FunctionTableNode {
31
+ type: 'FunctionTable';
32
+ /** Function name */
33
+ name: string;
34
+ /** Optional schema for the function (some dialects) */
35
+ schema?: string;
36
+ /** Function arguments as operand nodes */
37
+ args?: any[]; // use any to avoid circular import here; caller should supply OperandNode
38
+ /** Optional alias for the function table */
39
+ alias?: string;
40
+ /** LATERAL flag */
40
41
  lateral?: boolean;
41
42
  /** WITH ORDINALITY flag */
42
43
  withOrdinality?: boolean;
@@ -59,118 +60,127 @@ export interface DerivedTableNode {
59
60
 
60
61
  export type TableSourceNode = TableNode | FunctionTableNode | DerivedTableNode;
61
62
 
63
+ /**
64
+ * Any expression that can appear in ORDER BY / GROUP BY terms.
65
+ */
66
+ export type OrderingTerm = OperandNode | ExpressionNode | AliasRefNode;
67
+
62
68
  /**
63
69
  * AST node representing an ORDER BY clause
64
70
  */
65
71
  export interface OrderByNode {
66
- type: 'OrderBy';
67
- /** Column to order by */
68
- column: ColumnNode;
69
- /** Order direction (ASC or DESC) */
70
- direction: OrderDirection;
71
- }
72
-
73
- /**
74
- * AST node representing a Common Table Expression (CTE)
75
- */
76
- export interface CommonTableExpressionNode {
77
- type: 'CommonTableExpression';
78
- /** CTE name */
79
- name: string;
80
- /** Optional column names */
81
- columns?: string[];
82
- /** CTE query */
83
- query: SelectQueryNode;
84
- /** Whether the CTE is recursive */
85
- recursive: boolean;
86
- }
87
-
88
- /**
89
- * Supported set operation kinds for compound SELECT queries
90
- */
91
- export type SetOperationKind = 'UNION' | 'UNION ALL' | 'INTERSECT' | 'EXCEPT';
92
-
93
- /**
94
- * AST node representing a set operation (UNION, INTERSECT, etc.)
95
- */
96
- export interface SetOperationNode {
97
- type: 'SetOperation';
98
- /** Operator to combine queries */
99
- operator: SetOperationKind;
100
- /** Right-hand query in the compound expression */
101
- query: SelectQueryNode;
102
- }
103
-
104
- /**
105
- * AST node representing a complete SELECT query
106
- */
72
+ type: 'OrderBy';
73
+ /** Expression/operand/alias to order by */
74
+ term: OrderingTerm;
75
+ /** Order direction (ASC or DESC) */
76
+ direction: OrderDirection;
77
+ /** Optional nulls ordering (NULLS FIRST/LAST) */
78
+ nulls?: 'FIRST' | 'LAST';
79
+ /** Optional collation */
80
+ collation?: string;
81
+ }
82
+
83
+ /**
84
+ * AST node representing a Common Table Expression (CTE)
85
+ */
86
+ export interface CommonTableExpressionNode {
87
+ type: 'CommonTableExpression';
88
+ /** CTE name */
89
+ name: string;
90
+ /** Optional column names */
91
+ columns?: string[];
92
+ /** CTE query */
93
+ query: SelectQueryNode;
94
+ /** Whether the CTE is recursive */
95
+ recursive: boolean;
96
+ }
97
+
98
+ /**
99
+ * Supported set operation kinds for compound SELECT queries
100
+ */
101
+ export type SetOperationKind = 'UNION' | 'UNION ALL' | 'INTERSECT' | 'EXCEPT';
102
+
103
+ /**
104
+ * AST node representing a set operation (UNION, INTERSECT, etc.)
105
+ */
106
+ export interface SetOperationNode {
107
+ type: 'SetOperation';
108
+ /** Operator to combine queries */
109
+ operator: SetOperationKind;
110
+ /** Right-hand query in the compound expression */
111
+ query: SelectQueryNode;
112
+ }
113
+
114
+ /**
115
+ * AST node representing a complete SELECT query
116
+ */
107
117
  export interface SelectQueryNode {
108
118
  type: 'SelectQuery';
109
119
  /** Optional CTEs (WITH clauses) */
110
120
  ctes?: CommonTableExpressionNode[];
111
121
  /** FROM clause table (either a Table or a FunctionTable) */
112
122
  from: TableSourceNode;
113
- /** SELECT clause columns */
114
- columns: (ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode)[];
115
- /** JOIN clauses */
116
- joins: JoinNode[];
117
- /** Optional WHERE clause */
118
- where?: ExpressionNode;
119
- /** Optional GROUP BY clause */
120
- groupBy?: ColumnNode[];
121
- /** Optional HAVING clause */
122
- having?: ExpressionNode;
123
- /** Optional ORDER BY clause */
124
- orderBy?: OrderByNode[];
125
- /** Optional LIMIT clause */
126
- limit?: number;
127
- /** Optional OFFSET clause */
128
- offset?: number;
129
- /** Optional query metadata */
130
- meta?: Record<string, unknown>;
131
- /** Optional DISTINCT clause */
132
- distinct?: ColumnNode[];
133
- /** Optional set operations chaining this query with others */
134
- setOps?: SetOperationNode[];
135
- }
136
-
137
- export interface InsertQueryNode {
138
- type: 'InsertQuery';
139
- /** Target table */
140
- into: TableNode;
141
- /** Column order for inserted values */
142
- columns: ColumnNode[];
143
- /** Rows of values to insert */
144
- values: OperandNode[][];
145
- /** Optional RETURNING clause */
146
- returning?: ColumnNode[];
147
- }
148
-
149
- export interface UpdateAssignmentNode {
150
- /** Column to update */
151
- column: ColumnNode;
152
- /** Value to set */
153
- value: OperandNode;
154
- }
155
-
156
- export interface UpdateQueryNode {
157
- type: 'UpdateQuery';
158
- /** Table being updated */
159
- table: TableNode;
160
- /** Assignments for SET clause */
161
- set: UpdateAssignmentNode[];
162
- /** Optional WHERE clause */
163
- where?: ExpressionNode;
164
- /** Optional RETURNING clause */
165
- returning?: ColumnNode[];
166
- }
167
-
168
- export interface DeleteQueryNode {
169
- type: 'DeleteQuery';
170
- /** Table to delete from */
171
- from: TableNode;
172
- /** Optional WHERE clause */
173
- where?: ExpressionNode;
174
- /** Optional RETURNING clause */
175
- returning?: ColumnNode[];
176
- }
123
+ /** SELECT clause columns */
124
+ columns: (ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode)[];
125
+ /** JOIN clauses */
126
+ joins: JoinNode[];
127
+ /** Optional WHERE clause */
128
+ where?: ExpressionNode;
129
+ /** Optional GROUP BY clause */
130
+ groupBy?: OrderingTerm[];
131
+ /** Optional HAVING clause */
132
+ having?: ExpressionNode;
133
+ /** Optional ORDER BY clause */
134
+ orderBy?: OrderByNode[];
135
+ /** Optional LIMIT clause */
136
+ limit?: number;
137
+ /** Optional OFFSET clause */
138
+ offset?: number;
139
+ /** Optional query metadata */
140
+ meta?: Record<string, unknown>;
141
+ /** Optional DISTINCT clause */
142
+ distinct?: ColumnNode[];
143
+ /** Optional set operations chaining this query with others */
144
+ setOps?: SetOperationNode[];
145
+ }
146
+
147
+ export interface InsertQueryNode {
148
+ type: 'InsertQuery';
149
+ /** Target table */
150
+ into: TableNode;
151
+ /** Column order for inserted values */
152
+ columns: ColumnNode[];
153
+ /** Rows of values to insert */
154
+ values: OperandNode[][];
155
+ /** Optional RETURNING clause */
156
+ returning?: ColumnNode[];
157
+ }
158
+
159
+ export interface UpdateAssignmentNode {
160
+ /** Column to update */
161
+ column: ColumnNode;
162
+ /** Value to set */
163
+ value: OperandNode;
164
+ }
165
+
166
+ export interface UpdateQueryNode {
167
+ type: 'UpdateQuery';
168
+ /** Table being updated */
169
+ table: TableNode;
170
+ /** Assignments for SET clause */
171
+ set: UpdateAssignmentNode[];
172
+ /** Optional WHERE clause */
173
+ where?: ExpressionNode;
174
+ /** Optional RETURNING clause */
175
+ returning?: ColumnNode[];
176
+ }
177
+
178
+ export interface DeleteQueryNode {
179
+ type: 'DeleteQuery';
180
+ /** Table to delete from */
181
+ from: TableNode;
182
+ /** Optional WHERE clause */
183
+ where?: ExpressionNode;
184
+ /** Optional RETURNING clause */
185
+ returning?: ColumnNode[];
186
+ }
@@ -3,60 +3,60 @@ import { columnOperand } from './expression-builders.js';
3
3
  import { OrderDirection } from '../sql/sql.js';
4
4
  import { OrderByNode } from './query.js';
5
5
  import { ColumnRef } from './types.js';
6
-
7
- const buildWindowFunction = (
8
- name: string,
9
- args: (ColumnNode | LiteralNode | JsonPathNode)[] = [],
10
- partitionBy?: ColumnNode[],
11
- orderBy?: OrderByNode[]
12
- ): WindowFunctionNode => {
13
- const node: WindowFunctionNode = {
14
- type: 'WindowFunction',
15
- name,
16
- args
17
- };
18
-
19
- if (partitionBy && partitionBy.length) {
20
- node.partitionBy = partitionBy;
21
- }
22
-
23
- if (orderBy && orderBy.length) {
24
- node.orderBy = orderBy;
25
- }
26
-
27
- return node;
28
- };
29
-
30
- /**
31
- * Creates a ROW_NUMBER window function
32
- * @returns Window function node for ROW_NUMBER
33
- */
34
- export const rowNumber = (): WindowFunctionNode => buildWindowFunction('ROW_NUMBER');
35
-
36
- /**
37
- * Creates a RANK window function
38
- * @returns Window function node for RANK
39
- */
40
- export const rank = (): WindowFunctionNode => buildWindowFunction('RANK');
41
-
42
- /**
43
- * Creates a DENSE_RANK window function
44
- * @returns Window function node for DENSE_RANK
45
- */
46
- export const denseRank = (): WindowFunctionNode => buildWindowFunction('DENSE_RANK');
47
-
48
- /**
49
- * Creates an NTILE window function
50
- * @param n - Number of buckets
51
- * @returns Window function node for NTILE
52
- */
6
+
7
+ const buildWindowFunction = (
8
+ name: string,
9
+ args: (ColumnNode | LiteralNode | JsonPathNode)[] = [],
10
+ partitionBy?: ColumnNode[],
11
+ orderBy?: OrderByNode[]
12
+ ): WindowFunctionNode => {
13
+ const node: WindowFunctionNode = {
14
+ type: 'WindowFunction',
15
+ name,
16
+ args
17
+ };
18
+
19
+ if (partitionBy && partitionBy.length) {
20
+ node.partitionBy = partitionBy;
21
+ }
22
+
23
+ if (orderBy && orderBy.length) {
24
+ node.orderBy = orderBy;
25
+ }
26
+
27
+ return node;
28
+ };
29
+
30
+ /**
31
+ * Creates a ROW_NUMBER window function
32
+ * @returns Window function node for ROW_NUMBER
33
+ */
34
+ export const rowNumber = (): WindowFunctionNode => buildWindowFunction('ROW_NUMBER');
35
+
36
+ /**
37
+ * Creates a RANK window function
38
+ * @returns Window function node for RANK
39
+ */
40
+ export const rank = (): WindowFunctionNode => buildWindowFunction('RANK');
41
+
42
+ /**
43
+ * Creates a DENSE_RANK window function
44
+ * @returns Window function node for DENSE_RANK
45
+ */
46
+ export const denseRank = (): WindowFunctionNode => buildWindowFunction('DENSE_RANK');
47
+
48
+ /**
49
+ * Creates an NTILE window function
50
+ * @param n - Number of buckets
51
+ * @returns Window function node for NTILE
52
+ */
53
53
  export const ntile = (n: number): WindowFunctionNode =>
54
54
  buildWindowFunction('NTILE', [{ type: 'Literal', value: n }]);
55
-
56
- /**
57
- * Creates a LAG window function
58
- * @param col - Column to lag
59
- * @param offset - Offset (defaults to 1)
55
+
56
+ /**
57
+ * Creates a LAG window function
58
+ * @param col - Column to lag
59
+ * @param offset - Offset (defaults to 1)
60
60
  * @param defaultValue - Default value if no row exists
61
61
  * @returns Window function node for LAG
62
62
  */
@@ -65,16 +65,16 @@ export const lag = (col: ColumnRef | ColumnNode, offset: number = 1, defaultValu
65
65
  columnOperand(col),
66
66
  { type: 'Literal', value: offset }
67
67
  ];
68
- if (defaultValue !== undefined) {
69
- args.push({ type: 'Literal', value: defaultValue });
70
- }
71
- return buildWindowFunction('LAG', args);
72
- };
73
-
74
- /**
75
- * Creates a LEAD window function
76
- * @param col - Column to lead
77
- * @param offset - Offset (defaults to 1)
68
+ if (defaultValue !== undefined) {
69
+ args.push({ type: 'Literal', value: defaultValue });
70
+ }
71
+ return buildWindowFunction('LAG', args);
72
+ };
73
+
74
+ /**
75
+ * Creates a LEAD window function
76
+ * @param col - Column to lead
77
+ * @param offset - Offset (defaults to 1)
78
78
  * @param defaultValue - Default value if no row exists
79
79
  * @returns Window function node for LEAD
80
80
  */
@@ -83,36 +83,36 @@ export const lead = (col: ColumnRef | ColumnNode, offset: number = 1, defaultVal
83
83
  columnOperand(col),
84
84
  { type: 'Literal', value: offset }
85
85
  ];
86
- if (defaultValue !== undefined) {
87
- args.push({ type: 'Literal', value: defaultValue });
88
- }
89
- return buildWindowFunction('LEAD', args);
90
- };
91
-
92
- /**
93
- * Creates a FIRST_VALUE window function
86
+ if (defaultValue !== undefined) {
87
+ args.push({ type: 'Literal', value: defaultValue });
88
+ }
89
+ return buildWindowFunction('LEAD', args);
90
+ };
91
+
92
+ /**
93
+ * Creates a FIRST_VALUE window function
94
94
  * @param col - Column to get first value from
95
95
  * @returns Window function node for FIRST_VALUE
96
96
  */
97
97
  export const firstValue = (col: ColumnRef | ColumnNode): WindowFunctionNode =>
98
98
  buildWindowFunction('FIRST_VALUE', [columnOperand(col)]);
99
-
100
- /**
101
- * Creates a LAST_VALUE window function
99
+
100
+ /**
101
+ * Creates a LAST_VALUE window function
102
102
  * @param col - Column to get last value from
103
103
  * @returns Window function node for LAST_VALUE
104
104
  */
105
105
  export const lastValue = (col: ColumnRef | ColumnNode): WindowFunctionNode =>
106
106
  buildWindowFunction('LAST_VALUE', [columnOperand(col)]);
107
-
108
- /**
109
- * Creates a custom window function
110
- * @param name - Window function name
111
- * @param args - Function arguments
112
- * @param partitionBy - Optional PARTITION BY columns
113
- * @param orderBy - Optional ORDER BY clauses
114
- * @returns Window function node
115
- */
107
+
108
+ /**
109
+ * Creates a custom window function
110
+ * @param name - Window function name
111
+ * @param args - Function arguments
112
+ * @param partitionBy - Optional PARTITION BY columns
113
+ * @param orderBy - Optional ORDER BY clauses
114
+ * @returns Window function node
115
+ */
116
116
  export const windowFunction = (
117
117
  name: string,
118
118
  args: (ColumnRef | ColumnNode | LiteralNode | JsonPathNode)[] = [],
@@ -132,9 +132,9 @@ export const windowFunction = (
132
132
  const partitionNodes = partitionBy?.map(col => columnOperand(col)) ?? undefined;
133
133
  const orderNodes: OrderByNode[] | undefined = orderBy?.map(o => ({
134
134
  type: 'OrderBy',
135
- column: columnOperand(o.column),
135
+ term: columnOperand(o.column),
136
136
  direction: o.direction
137
137
  }));
138
-
139
- return buildWindowFunction(name, nodeArgs, partitionNodes, orderNodes);
140
- };
138
+
139
+ return buildWindowFunction(name, nodeArgs, partitionNodes, orderNodes);
140
+ };