metal-orm 1.0.57 → 1.0.58
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/README.md +22 -15
- package/dist/index.cjs +640 -83
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +113 -45
- package/dist/index.d.ts +113 -45
- package/dist/index.js +639 -83
- package/dist/index.js.map +1 -1
- package/package.json +69 -69
- package/src/decorators/bootstrap.ts +39 -3
- package/src/orm/entity-meta.ts +6 -3
- package/src/orm/entity.ts +81 -14
- package/src/orm/execute.ts +87 -20
- package/src/orm/lazy-batch.ts +237 -54
- package/src/orm/relations/belongs-to.ts +2 -2
- package/src/orm/relations/has-many.ts +23 -9
- package/src/orm/relations/has-one.ts +2 -2
- package/src/orm/relations/many-to-many.ts +27 -13
- package/src/orm/save-graph-types.ts +2 -2
- package/src/orm/save-graph.ts +18 -18
- package/src/query-builder/relation-conditions.ts +80 -59
- package/src/query-builder/relation-service.ts +399 -95
- package/src/query-builder/relation-types.ts +2 -2
- package/src/query-builder/select.ts +58 -40
- package/src/schema/types.ts +106 -89
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { TableDef } from '../schema/table.js';
|
|
2
|
-
import { RelationDef, RelationKinds, BelongsToManyRelation } from '../schema/relation.js';
|
|
3
|
-
import { ExpressionNode, eq, and } from '../core/ast/expression.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import { TableDef } from '../schema/table.js';
|
|
2
|
+
import { RelationDef, RelationKinds, BelongsToManyRelation } from '../schema/relation.js';
|
|
3
|
+
import { ExpressionNode, eq, and } from '../core/ast/expression.js';
|
|
4
|
+
import { TableSourceNode } from '../core/ast/query.js';
|
|
5
|
+
import { findPrimaryKey } from './hydration-planner.js';
|
|
6
|
+
import { JoinNode } from '../core/ast/join.js';
|
|
7
|
+
import { JoinKind } from '../core/sql/sql.js';
|
|
8
|
+
import { createJoinNode } from '../core/ast/join-node.js';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Utility function to handle unreachable code paths
|
|
@@ -21,26 +22,32 @@ const assertNever = (value: never): never => {
|
|
|
21
22
|
* @param relation - Relation definition
|
|
22
23
|
* @returns Expression node representing the join condition
|
|
23
24
|
*/
|
|
24
|
-
const baseRelationCondition = (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
const baseRelationCondition = (
|
|
26
|
+
root: TableDef,
|
|
27
|
+
relation: RelationDef,
|
|
28
|
+
rootAlias?: string,
|
|
29
|
+
targetTableName?: string
|
|
30
|
+
): ExpressionNode => {
|
|
31
|
+
const rootTable = rootAlias || root.name;
|
|
32
|
+
const targetTable = targetTableName ?? relation.target.name;
|
|
33
|
+
const defaultLocalKey =
|
|
34
|
+
relation.type === RelationKinds.HasMany || relation.type === RelationKinds.HasOne
|
|
35
|
+
? findPrimaryKey(root)
|
|
36
|
+
: findPrimaryKey(relation.target);
|
|
30
37
|
const localKey = relation.localKey || defaultLocalKey;
|
|
31
38
|
|
|
32
39
|
switch (relation.type) {
|
|
33
|
-
case RelationKinds.HasMany:
|
|
34
|
-
case RelationKinds.HasOne:
|
|
35
|
-
return eq(
|
|
36
|
-
{ type: 'Column', table:
|
|
37
|
-
{ type: 'Column', table: rootTable, name: localKey }
|
|
38
|
-
);
|
|
39
|
-
case RelationKinds.BelongsTo:
|
|
40
|
-
return eq(
|
|
41
|
-
{ type: 'Column', table:
|
|
42
|
-
{ type: 'Column', table: rootTable, name: relation.foreignKey }
|
|
43
|
-
);
|
|
40
|
+
case RelationKinds.HasMany:
|
|
41
|
+
case RelationKinds.HasOne:
|
|
42
|
+
return eq(
|
|
43
|
+
{ type: 'Column', table: targetTable, name: relation.foreignKey },
|
|
44
|
+
{ type: 'Column', table: rootTable, name: localKey }
|
|
45
|
+
);
|
|
46
|
+
case RelationKinds.BelongsTo:
|
|
47
|
+
return eq(
|
|
48
|
+
{ type: 'Column', table: targetTable, name: localKey },
|
|
49
|
+
{ type: 'Column', table: rootTable, name: relation.foreignKey }
|
|
50
|
+
);
|
|
44
51
|
case RelationKinds.BelongsToMany:
|
|
45
52
|
throw new Error('BelongsToMany relations do not support the standard join condition builder');
|
|
46
53
|
default:
|
|
@@ -58,14 +65,16 @@ const baseRelationCondition = (root: TableDef, relation: RelationDef, rootAlias?
|
|
|
58
65
|
* @param rootAlias - Optional alias for the root table
|
|
59
66
|
* @returns Array of join nodes for the pivot and target tables
|
|
60
67
|
*/
|
|
61
|
-
export const buildBelongsToManyJoins = (
|
|
62
|
-
root: TableDef,
|
|
63
|
-
relationName: string,
|
|
64
|
-
relation: BelongsToManyRelation,
|
|
65
|
-
joinKind: JoinKind,
|
|
66
|
-
extra?: ExpressionNode,
|
|
67
|
-
rootAlias?: string
|
|
68
|
-
|
|
68
|
+
export const buildBelongsToManyJoins = (
|
|
69
|
+
root: TableDef,
|
|
70
|
+
relationName: string,
|
|
71
|
+
relation: BelongsToManyRelation,
|
|
72
|
+
joinKind: JoinKind,
|
|
73
|
+
extra?: ExpressionNode,
|
|
74
|
+
rootAlias?: string,
|
|
75
|
+
targetTable?: TableSourceNode,
|
|
76
|
+
targetTableName?: string
|
|
77
|
+
): JoinNode[] => {
|
|
69
78
|
const rootKey = relation.localKey || findPrimaryKey(root);
|
|
70
79
|
const targetKey = relation.targetKey || findPrimaryKey(relation.target);
|
|
71
80
|
const rootTable = rootAlias || root.name;
|
|
@@ -81,21 +90,27 @@ export const buildBelongsToManyJoins = (
|
|
|
81
90
|
pivotCondition
|
|
82
91
|
);
|
|
83
92
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
targetCondition,
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
const targetSource: TableSourceNode = targetTable ?? {
|
|
94
|
+
type: 'Table',
|
|
95
|
+
name: relation.target.name,
|
|
96
|
+
schema: relation.target.schema
|
|
97
|
+
};
|
|
98
|
+
const effectiveTargetName = targetTableName ?? relation.target.name;
|
|
99
|
+
let targetCondition: ExpressionNode = eq(
|
|
100
|
+
{ type: 'Column', table: effectiveTargetName, name: targetKey },
|
|
101
|
+
{ type: 'Column', table: relation.pivotTable.name, name: relation.pivotForeignKeyToTarget }
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (extra) {
|
|
105
|
+
targetCondition = and(targetCondition, extra);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const targetJoin = createJoinNode(
|
|
109
|
+
joinKind,
|
|
110
|
+
targetSource,
|
|
111
|
+
targetCondition,
|
|
112
|
+
relationName
|
|
113
|
+
);
|
|
99
114
|
|
|
100
115
|
return [pivotJoin, targetJoin];
|
|
101
116
|
};
|
|
@@ -108,15 +123,16 @@ export const buildBelongsToManyJoins = (
|
|
|
108
123
|
* @param rootAlias - Optional alias for the root table
|
|
109
124
|
* @returns Expression node representing the complete join condition
|
|
110
125
|
*/
|
|
111
|
-
export const buildRelationJoinCondition = (
|
|
112
|
-
root: TableDef,
|
|
113
|
-
relation: RelationDef,
|
|
114
|
-
extra?: ExpressionNode,
|
|
115
|
-
rootAlias?: string
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
126
|
+
export const buildRelationJoinCondition = (
|
|
127
|
+
root: TableDef,
|
|
128
|
+
relation: RelationDef,
|
|
129
|
+
extra?: ExpressionNode,
|
|
130
|
+
rootAlias?: string,
|
|
131
|
+
targetTableName?: string
|
|
132
|
+
): ExpressionNode => {
|
|
133
|
+
const base = baseRelationCondition(root, relation, rootAlias, targetTableName);
|
|
134
|
+
return extra ? and(base, extra) : base;
|
|
135
|
+
};
|
|
120
136
|
|
|
121
137
|
/**
|
|
122
138
|
* Builds a relation correlation condition for subqueries
|
|
@@ -125,6 +141,11 @@ export const buildRelationJoinCondition = (
|
|
|
125
141
|
* @param rootAlias - Optional alias for the root table
|
|
126
142
|
* @returns Expression node representing the correlation condition
|
|
127
143
|
*/
|
|
128
|
-
export const buildRelationCorrelation = (
|
|
129
|
-
|
|
130
|
-
|
|
144
|
+
export const buildRelationCorrelation = (
|
|
145
|
+
root: TableDef,
|
|
146
|
+
relation: RelationDef,
|
|
147
|
+
rootAlias?: string,
|
|
148
|
+
targetTableName?: string
|
|
149
|
+
): ExpressionNode => {
|
|
150
|
+
return baseRelationCondition(root, relation, rootAlias, targetTableName);
|
|
151
|
+
};
|