metal-orm 1.0.97 → 1.0.98
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 +18 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/hydration/types.ts +57 -57
- package/src/orm/entity-relations.ts +19 -19
- package/src/orm/lazy-batch/belongs-to-many.ts +134 -134
- package/src/orm/lazy-batch/belongs-to.ts +108 -108
- package/src/orm/lazy-batch/has-many.ts +69 -69
- package/src/orm/lazy-batch/has-one.ts +68 -68
- package/src/orm/lazy-batch/shared.ts +125 -125
- package/src/orm/save-graph.ts +48 -48
- package/src/query-builder/column-selector.ts +9 -9
- package/src/query-builder/hydration-manager.ts +353 -353
- package/src/query-builder/hydration-planner.ts +22 -22
- package/src/query-builder/relation-conditions.ts +80 -80
- package/src/query-builder/select/projection-facet.ts +23 -23
- package/src/query-builder/select-query-state.ts +213 -213
- package/src/query-builder/select.ts +1155 -1133
- package/src/schema/relation.ts +22 -22
|
@@ -1,213 +1,213 @@
|
|
|
1
|
-
import { TableDef } from '../schema/table.js';
|
|
2
|
-
import {
|
|
3
|
-
SelectQueryNode,
|
|
4
|
-
CommonTableExpressionNode,
|
|
5
|
-
OrderByNode,
|
|
6
|
-
SetOperationNode,
|
|
7
|
-
TableSourceNode,
|
|
8
|
-
OrderingTerm
|
|
9
|
-
} from '../core/ast/query.js';
|
|
10
|
-
import { createTableNode } from '../core/ast/builders.js';
|
|
11
|
-
import {
|
|
12
|
-
ColumnNode,
|
|
13
|
-
ExpressionNode,
|
|
14
|
-
FunctionNode,
|
|
15
|
-
ScalarSubqueryNode,
|
|
16
|
-
CaseExpressionNode,
|
|
17
|
-
CastExpressionNode,
|
|
18
|
-
WindowFunctionNode
|
|
19
|
-
} from '../core/ast/expression.js';
|
|
20
|
-
import { JoinNode } from '../core/ast/join.js';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Node types that can be used in query projections
|
|
24
|
-
*/
|
|
25
|
-
export type ProjectionNode =
|
|
26
|
-
| ColumnNode
|
|
27
|
-
| FunctionNode
|
|
28
|
-
| ScalarSubqueryNode
|
|
29
|
-
| CaseExpressionNode
|
|
30
|
-
| CastExpressionNode
|
|
31
|
-
| WindowFunctionNode;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Manages the state of a SELECT query being built
|
|
35
|
-
*/
|
|
36
|
-
export class SelectQueryState {
|
|
37
|
-
/**
|
|
38
|
-
* Table definition for the query
|
|
39
|
-
*/
|
|
40
|
-
public readonly table: TableDef;
|
|
41
|
-
/**
|
|
42
|
-
* Abstract Syntax Tree (AST) representation of the query
|
|
43
|
-
*/
|
|
44
|
-
public readonly ast: SelectQueryNode;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Creates a new SelectQueryState instance
|
|
48
|
-
* @param table - Table definition
|
|
49
|
-
* @param ast - Optional existing AST
|
|
50
|
-
*/
|
|
51
|
-
constructor(table: TableDef, ast?: SelectQueryNode) {
|
|
52
|
-
this.table = table;
|
|
53
|
-
this.ast = ast ?? {
|
|
54
|
-
type: 'SelectQuery',
|
|
55
|
-
from: createTableNode(table),
|
|
56
|
-
columns: [],
|
|
57
|
-
joins: []
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Creates a new SelectQueryState with updated AST
|
|
63
|
-
* @param nextAst - Updated AST
|
|
64
|
-
* @returns New SelectQueryState instance
|
|
65
|
-
*/
|
|
66
|
-
private clone(nextAst: SelectQueryNode): SelectQueryState {
|
|
67
|
-
return new SelectQueryState(this.table, nextAst);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Adds columns to the query
|
|
72
|
-
* @param newCols - Columns to add
|
|
73
|
-
* @returns New SelectQueryState with added columns
|
|
74
|
-
*/
|
|
75
|
-
withColumns(newCols: ProjectionNode[]): SelectQueryState {
|
|
76
|
-
return this.clone({
|
|
77
|
-
...this.ast,
|
|
78
|
-
columns: [...(this.ast.columns ?? []), ...newCols]
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Adds a join to the query
|
|
84
|
-
* @param join - Join node to add
|
|
85
|
-
* @returns New SelectQueryState with added join
|
|
86
|
-
*/
|
|
87
|
-
withJoin(join: JoinNode): SelectQueryState {
|
|
88
|
-
return this.clone({
|
|
89
|
-
...this.ast,
|
|
90
|
-
joins: [...(this.ast.joins ?? []), join]
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Replaces the FROM clause.
|
|
96
|
-
* @param from - Table source for the FROM clause
|
|
97
|
-
* @returns New SelectQueryState with updated FROM
|
|
98
|
-
*/
|
|
99
|
-
withFrom(from: TableSourceNode): SelectQueryState {
|
|
100
|
-
return this.clone({
|
|
101
|
-
...this.ast,
|
|
102
|
-
from
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Adds a WHERE clause to the query
|
|
108
|
-
* @param predicate - WHERE predicate expression
|
|
109
|
-
* @returns New SelectQueryState with WHERE clause
|
|
110
|
-
*/
|
|
111
|
-
withWhere(predicate: ExpressionNode): SelectQueryState {
|
|
112
|
-
return this.clone({
|
|
113
|
-
...this.ast,
|
|
114
|
-
where: predicate
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Adds a HAVING clause to the query
|
|
120
|
-
* @param predicate - HAVING predicate expression
|
|
121
|
-
* @returns New SelectQueryState with HAVING clause
|
|
122
|
-
*/
|
|
123
|
-
withHaving(predicate: ExpressionNode): SelectQueryState {
|
|
124
|
-
return this.clone({
|
|
125
|
-
...this.ast,
|
|
126
|
-
having: predicate
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Adds GROUP BY columns to the query
|
|
132
|
-
* @param columns - Terms to group by
|
|
133
|
-
* @returns New SelectQueryState with GROUP BY clause
|
|
134
|
-
*/
|
|
135
|
-
withGroupBy(columns: OrderingTerm[]): SelectQueryState {
|
|
136
|
-
return this.clone({
|
|
137
|
-
...this.ast,
|
|
138
|
-
groupBy: [...(this.ast.groupBy ?? []), ...columns]
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Adds ORDER BY clauses to the query
|
|
144
|
-
* @param orderBy - ORDER BY nodes
|
|
145
|
-
* @returns New SelectQueryState with ORDER BY clause
|
|
146
|
-
*/
|
|
147
|
-
withOrderBy(orderBy: OrderByNode[]): SelectQueryState {
|
|
148
|
-
return this.clone({
|
|
149
|
-
...this.ast,
|
|
150
|
-
orderBy: [...(this.ast.orderBy ?? []), ...orderBy]
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Adds DISTINCT columns to the query
|
|
156
|
-
* @param columns - Columns to make distinct
|
|
157
|
-
* @returns New SelectQueryState with DISTINCT clause
|
|
158
|
-
*/
|
|
159
|
-
withDistinct(columns: ColumnNode[]): SelectQueryState {
|
|
160
|
-
return this.clone({
|
|
161
|
-
...this.ast,
|
|
162
|
-
distinct: [...(this.ast.distinct ?? []), ...columns]
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Adds a LIMIT clause to the query
|
|
168
|
-
* @param limit - Maximum number of rows to return
|
|
169
|
-
* @returns New SelectQueryState with LIMIT clause
|
|
170
|
-
*/
|
|
171
|
-
withLimit(limit: number): SelectQueryState {
|
|
172
|
-
return this.clone({
|
|
173
|
-
...this.ast,
|
|
174
|
-
limit
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Adds an OFFSET clause to the query
|
|
180
|
-
* @param offset - Number of rows to skip
|
|
181
|
-
* @returns New SelectQueryState with OFFSET clause
|
|
182
|
-
*/
|
|
183
|
-
withOffset(offset: number): SelectQueryState {
|
|
184
|
-
return this.clone({
|
|
185
|
-
...this.ast,
|
|
186
|
-
offset
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Adds a Common Table Expression (CTE) to the query
|
|
192
|
-
* @param cte - CTE node to add
|
|
193
|
-
* @returns New SelectQueryState with CTE
|
|
194
|
-
*/
|
|
195
|
-
withCte(cte: CommonTableExpressionNode): SelectQueryState {
|
|
196
|
-
return this.clone({
|
|
197
|
-
...this.ast,
|
|
198
|
-
ctes: [...(this.ast.ctes ?? []), cte]
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Adds a set operation (UNION/INTERSECT/EXCEPT) to the query
|
|
204
|
-
* @param op - Set operation node to add
|
|
205
|
-
* @returns New SelectQueryState with set operation
|
|
206
|
-
*/
|
|
207
|
-
withSetOperation(op: SetOperationNode): SelectQueryState {
|
|
208
|
-
return this.clone({
|
|
209
|
-
...this.ast,
|
|
210
|
-
setOps: [...(this.ast.setOps ?? []), op]
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
}
|
|
1
|
+
import { TableDef } from '../schema/table.js';
|
|
2
|
+
import {
|
|
3
|
+
SelectQueryNode,
|
|
4
|
+
CommonTableExpressionNode,
|
|
5
|
+
OrderByNode,
|
|
6
|
+
SetOperationNode,
|
|
7
|
+
TableSourceNode,
|
|
8
|
+
OrderingTerm
|
|
9
|
+
} from '../core/ast/query.js';
|
|
10
|
+
import { createTableNode } from '../core/ast/builders.js';
|
|
11
|
+
import {
|
|
12
|
+
ColumnNode,
|
|
13
|
+
ExpressionNode,
|
|
14
|
+
FunctionNode,
|
|
15
|
+
ScalarSubqueryNode,
|
|
16
|
+
CaseExpressionNode,
|
|
17
|
+
CastExpressionNode,
|
|
18
|
+
WindowFunctionNode
|
|
19
|
+
} from '../core/ast/expression.js';
|
|
20
|
+
import { JoinNode } from '../core/ast/join.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Node types that can be used in query projections
|
|
24
|
+
*/
|
|
25
|
+
export type ProjectionNode =
|
|
26
|
+
| ColumnNode
|
|
27
|
+
| FunctionNode
|
|
28
|
+
| ScalarSubqueryNode
|
|
29
|
+
| CaseExpressionNode
|
|
30
|
+
| CastExpressionNode
|
|
31
|
+
| WindowFunctionNode;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Manages the state of a SELECT query being built
|
|
35
|
+
*/
|
|
36
|
+
export class SelectQueryState {
|
|
37
|
+
/**
|
|
38
|
+
* Table definition for the query
|
|
39
|
+
*/
|
|
40
|
+
public readonly table: TableDef;
|
|
41
|
+
/**
|
|
42
|
+
* Abstract Syntax Tree (AST) representation of the query
|
|
43
|
+
*/
|
|
44
|
+
public readonly ast: SelectQueryNode;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new SelectQueryState instance
|
|
48
|
+
* @param table - Table definition
|
|
49
|
+
* @param ast - Optional existing AST
|
|
50
|
+
*/
|
|
51
|
+
constructor(table: TableDef, ast?: SelectQueryNode) {
|
|
52
|
+
this.table = table;
|
|
53
|
+
this.ast = ast ?? {
|
|
54
|
+
type: 'SelectQuery',
|
|
55
|
+
from: createTableNode(table),
|
|
56
|
+
columns: [],
|
|
57
|
+
joins: []
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Creates a new SelectQueryState with updated AST
|
|
63
|
+
* @param nextAst - Updated AST
|
|
64
|
+
* @returns New SelectQueryState instance
|
|
65
|
+
*/
|
|
66
|
+
private clone(nextAst: SelectQueryNode): SelectQueryState {
|
|
67
|
+
return new SelectQueryState(this.table, nextAst);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Adds columns to the query
|
|
72
|
+
* @param newCols - Columns to add
|
|
73
|
+
* @returns New SelectQueryState with added columns
|
|
74
|
+
*/
|
|
75
|
+
withColumns(newCols: ProjectionNode[]): SelectQueryState {
|
|
76
|
+
return this.clone({
|
|
77
|
+
...this.ast,
|
|
78
|
+
columns: [...(this.ast.columns ?? []), ...newCols]
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Adds a join to the query
|
|
84
|
+
* @param join - Join node to add
|
|
85
|
+
* @returns New SelectQueryState with added join
|
|
86
|
+
*/
|
|
87
|
+
withJoin(join: JoinNode): SelectQueryState {
|
|
88
|
+
return this.clone({
|
|
89
|
+
...this.ast,
|
|
90
|
+
joins: [...(this.ast.joins ?? []), join]
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Replaces the FROM clause.
|
|
96
|
+
* @param from - Table source for the FROM clause
|
|
97
|
+
* @returns New SelectQueryState with updated FROM
|
|
98
|
+
*/
|
|
99
|
+
withFrom(from: TableSourceNode): SelectQueryState {
|
|
100
|
+
return this.clone({
|
|
101
|
+
...this.ast,
|
|
102
|
+
from
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Adds a WHERE clause to the query
|
|
108
|
+
* @param predicate - WHERE predicate expression
|
|
109
|
+
* @returns New SelectQueryState with WHERE clause
|
|
110
|
+
*/
|
|
111
|
+
withWhere(predicate: ExpressionNode): SelectQueryState {
|
|
112
|
+
return this.clone({
|
|
113
|
+
...this.ast,
|
|
114
|
+
where: predicate
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Adds a HAVING clause to the query
|
|
120
|
+
* @param predicate - HAVING predicate expression
|
|
121
|
+
* @returns New SelectQueryState with HAVING clause
|
|
122
|
+
*/
|
|
123
|
+
withHaving(predicate: ExpressionNode): SelectQueryState {
|
|
124
|
+
return this.clone({
|
|
125
|
+
...this.ast,
|
|
126
|
+
having: predicate
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Adds GROUP BY columns to the query
|
|
132
|
+
* @param columns - Terms to group by
|
|
133
|
+
* @returns New SelectQueryState with GROUP BY clause
|
|
134
|
+
*/
|
|
135
|
+
withGroupBy(columns: OrderingTerm[]): SelectQueryState {
|
|
136
|
+
return this.clone({
|
|
137
|
+
...this.ast,
|
|
138
|
+
groupBy: [...(this.ast.groupBy ?? []), ...columns]
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Adds ORDER BY clauses to the query
|
|
144
|
+
* @param orderBy - ORDER BY nodes
|
|
145
|
+
* @returns New SelectQueryState with ORDER BY clause
|
|
146
|
+
*/
|
|
147
|
+
withOrderBy(orderBy: OrderByNode[]): SelectQueryState {
|
|
148
|
+
return this.clone({
|
|
149
|
+
...this.ast,
|
|
150
|
+
orderBy: [...(this.ast.orderBy ?? []), ...orderBy]
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Adds DISTINCT columns to the query
|
|
156
|
+
* @param columns - Columns to make distinct
|
|
157
|
+
* @returns New SelectQueryState with DISTINCT clause
|
|
158
|
+
*/
|
|
159
|
+
withDistinct(columns: ColumnNode[]): SelectQueryState {
|
|
160
|
+
return this.clone({
|
|
161
|
+
...this.ast,
|
|
162
|
+
distinct: [...(this.ast.distinct ?? []), ...columns]
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Adds a LIMIT clause to the query
|
|
168
|
+
* @param limit - Maximum number of rows to return
|
|
169
|
+
* @returns New SelectQueryState with LIMIT clause
|
|
170
|
+
*/
|
|
171
|
+
withLimit(limit: number): SelectQueryState {
|
|
172
|
+
return this.clone({
|
|
173
|
+
...this.ast,
|
|
174
|
+
limit
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Adds an OFFSET clause to the query
|
|
180
|
+
* @param offset - Number of rows to skip
|
|
181
|
+
* @returns New SelectQueryState with OFFSET clause
|
|
182
|
+
*/
|
|
183
|
+
withOffset(offset: number): SelectQueryState {
|
|
184
|
+
return this.clone({
|
|
185
|
+
...this.ast,
|
|
186
|
+
offset
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Adds a Common Table Expression (CTE) to the query
|
|
192
|
+
* @param cte - CTE node to add
|
|
193
|
+
* @returns New SelectQueryState with CTE
|
|
194
|
+
*/
|
|
195
|
+
withCte(cte: CommonTableExpressionNode): SelectQueryState {
|
|
196
|
+
return this.clone({
|
|
197
|
+
...this.ast,
|
|
198
|
+
ctes: [...(this.ast.ctes ?? []), cte]
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Adds a set operation (UNION/INTERSECT/EXCEPT) to the query
|
|
204
|
+
* @param op - Set operation node to add
|
|
205
|
+
* @returns New SelectQueryState with set operation
|
|
206
|
+
*/
|
|
207
|
+
withSetOperation(op: SetOperationNode): SelectQueryState {
|
|
208
|
+
return this.clone({
|
|
209
|
+
...this.ast,
|
|
210
|
+
setOps: [...(this.ast.setOps ?? []), op]
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|