metal-orm 1.0.56 → 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.
Files changed (82) hide show
  1. package/README.md +41 -33
  2. package/dist/index.cjs +1461 -195
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +541 -114
  5. package/dist/index.d.ts +541 -114
  6. package/dist/index.js +1424 -195
  7. package/dist/index.js.map +1 -1
  8. package/package.json +69 -69
  9. package/src/codegen/naming-strategy.ts +3 -1
  10. package/src/codegen/typescript.ts +20 -10
  11. package/src/core/ast/aggregate-functions.ts +14 -0
  12. package/src/core/ast/builders.ts +38 -20
  13. package/src/core/ast/expression-builders.ts +70 -2
  14. package/src/core/ast/expression-nodes.ts +305 -274
  15. package/src/core/ast/expression-visitor.ts +11 -1
  16. package/src/core/ast/expression.ts +4 -0
  17. package/src/core/ast/query.ts +3 -0
  18. package/src/core/ddl/introspect/catalogs/mysql.ts +5 -0
  19. package/src/core/ddl/introspect/catalogs/sqlite.ts +3 -0
  20. package/src/core/ddl/introspect/functions/mssql.ts +13 -0
  21. package/src/core/ddl/introspect/mssql.ts +4 -0
  22. package/src/core/ddl/introspect/mysql.ts +4 -0
  23. package/src/core/ddl/introspect/sqlite.ts +4 -0
  24. package/src/core/dialect/abstract.ts +552 -531
  25. package/src/core/dialect/base/function-table-formatter.ts +9 -30
  26. package/src/core/dialect/base/sql-dialect.ts +24 -0
  27. package/src/core/dialect/mssql/functions.ts +40 -2
  28. package/src/core/dialect/mysql/functions.ts +16 -2
  29. package/src/core/dialect/postgres/functions.ts +66 -2
  30. package/src/core/dialect/postgres/index.ts +17 -4
  31. package/src/core/dialect/postgres/table-functions.ts +27 -0
  32. package/src/core/dialect/sqlite/functions.ts +34 -0
  33. package/src/core/dialect/sqlite/index.ts +17 -1
  34. package/src/core/driver/database-driver.ts +9 -1
  35. package/src/core/driver/mssql-driver.ts +3 -0
  36. package/src/core/driver/mysql-driver.ts +3 -0
  37. package/src/core/driver/postgres-driver.ts +3 -0
  38. package/src/core/driver/sqlite-driver.ts +3 -0
  39. package/src/core/execution/executors/mssql-executor.ts +5 -0
  40. package/src/core/execution/executors/mysql-executor.ts +5 -0
  41. package/src/core/execution/executors/postgres-executor.ts +5 -0
  42. package/src/core/execution/executors/sqlite-executor.ts +5 -0
  43. package/src/core/functions/array.ts +26 -0
  44. package/src/core/functions/control-flow.ts +69 -0
  45. package/src/core/functions/datetime.ts +50 -0
  46. package/src/core/functions/definitions/aggregate.ts +16 -0
  47. package/src/core/functions/definitions/control-flow.ts +24 -0
  48. package/src/core/functions/definitions/datetime.ts +36 -0
  49. package/src/core/functions/definitions/helpers.ts +29 -0
  50. package/src/core/functions/definitions/json.ts +49 -0
  51. package/src/core/functions/definitions/numeric.ts +55 -0
  52. package/src/core/functions/definitions/string.ts +43 -0
  53. package/src/core/functions/function-registry.ts +48 -0
  54. package/src/core/functions/group-concat-helpers.ts +57 -0
  55. package/src/core/functions/json.ts +38 -0
  56. package/src/core/functions/numeric.ts +14 -0
  57. package/src/core/functions/standard-strategy.ts +86 -115
  58. package/src/core/functions/standard-table-strategy.ts +13 -0
  59. package/src/core/functions/table-types.ts +15 -0
  60. package/src/core/functions/text.ts +57 -0
  61. package/src/core/sql/sql.ts +59 -38
  62. package/src/decorators/bootstrap.ts +41 -4
  63. package/src/index.ts +18 -11
  64. package/src/orm/entity-meta.ts +6 -3
  65. package/src/orm/entity.ts +81 -14
  66. package/src/orm/execute.ts +87 -20
  67. package/src/orm/hydration-context.ts +10 -0
  68. package/src/orm/identity-map.ts +19 -0
  69. package/src/orm/interceptor-pipeline.ts +4 -0
  70. package/src/orm/lazy-batch.ts +237 -54
  71. package/src/orm/relations/belongs-to.ts +19 -2
  72. package/src/orm/relations/has-many.ts +23 -9
  73. package/src/orm/relations/has-one.ts +19 -2
  74. package/src/orm/relations/many-to-many.ts +59 -4
  75. package/src/orm/save-graph-types.ts +2 -2
  76. package/src/orm/save-graph.ts +18 -18
  77. package/src/query-builder/relation-conditions.ts +80 -59
  78. package/src/query-builder/relation-service.ts +399 -95
  79. package/src/query-builder/relation-types.ts +2 -2
  80. package/src/query-builder/select.ts +124 -106
  81. package/src/schema/table-guards.ts +6 -0
  82. package/src/schema/types.ts +109 -85
@@ -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 { findPrimaryKey } from './hydration-planner.js';
5
- import { JoinNode } from '../core/ast/join.js';
6
- import { JoinKind } from '../core/sql/sql.js';
7
- import { createJoinNode } from '../core/ast/join-node.js';
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 = (root: TableDef, relation: RelationDef, rootAlias?: string): ExpressionNode => {
25
- const rootTable = rootAlias || root.name;
26
- const defaultLocalKey =
27
- relation.type === RelationKinds.HasMany || relation.type === RelationKinds.HasOne
28
- ? findPrimaryKey(root)
29
- : findPrimaryKey(relation.target);
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: relation.target.name, name: relation.foreignKey },
37
- { type: 'Column', table: rootTable, name: localKey }
38
- );
39
- case RelationKinds.BelongsTo:
40
- return eq(
41
- { type: 'Column', table: relation.target.name, name: localKey },
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
- ): JoinNode[] => {
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
- let targetCondition: ExpressionNode = eq(
85
- { type: 'Column', table: relation.target.name, name: targetKey },
86
- { type: 'Column', table: relation.pivotTable.name, name: relation.pivotForeignKeyToTarget }
87
- );
88
-
89
- if (extra) {
90
- targetCondition = and(targetCondition, extra);
91
- }
92
-
93
- const targetJoin = createJoinNode(
94
- joinKind,
95
- { type: 'Table', name: relation.target.name, schema: relation.target.schema },
96
- targetCondition,
97
- relationName
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
- ): ExpressionNode => {
117
- const base = baseRelationCondition(root, relation, rootAlias);
118
- return extra ? and(base, extra) : base;
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 = (root: TableDef, relation: RelationDef, rootAlias?: string): ExpressionNode => {
129
- return baseRelationCondition(root, relation, rootAlias);
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
+ };