metal-orm 1.0.43 → 1.0.44

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 (84) hide show
  1. package/README.md +173 -30
  2. package/dist/index.cjs +896 -476
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +1146 -275
  5. package/dist/index.d.ts +1146 -275
  6. package/dist/index.js +896 -474
  7. package/dist/index.js.map +1 -1
  8. package/package.json +1 -1
  9. package/src/core/ast/adapters.ts +8 -2
  10. package/src/core/ast/builders.ts +105 -81
  11. package/src/core/ast/expression-builders.ts +430 -390
  12. package/src/core/ast/expression-visitor.ts +47 -8
  13. package/src/core/ast/helpers.ts +23 -0
  14. package/src/core/ast/join-node.ts +17 -1
  15. package/src/core/ddl/dialects/base-schema-dialect.ts +7 -1
  16. package/src/core/ddl/dialects/index.ts +1 -0
  17. package/src/core/ddl/dialects/mssql-schema-dialect.ts +1 -0
  18. package/src/core/ddl/dialects/mysql-schema-dialect.ts +1 -0
  19. package/src/core/ddl/dialects/postgres-schema-dialect.ts +1 -0
  20. package/src/core/ddl/dialects/sqlite-schema-dialect.ts +1 -0
  21. package/src/core/ddl/introspect/catalogs/index.ts +1 -0
  22. package/src/core/ddl/introspect/catalogs/postgres.ts +2 -0
  23. package/src/core/ddl/introspect/context.ts +6 -0
  24. package/src/core/ddl/introspect/functions/postgres.ts +13 -0
  25. package/src/core/ddl/introspect/mssql.ts +11 -0
  26. package/src/core/ddl/introspect/mysql.ts +2 -0
  27. package/src/core/ddl/introspect/postgres.ts +14 -0
  28. package/src/core/ddl/introspect/registry.ts +14 -0
  29. package/src/core/ddl/introspect/run-select.ts +13 -0
  30. package/src/core/ddl/introspect/sqlite.ts +22 -0
  31. package/src/core/ddl/introspect/utils.ts +18 -0
  32. package/src/core/ddl/naming-strategy.ts +6 -0
  33. package/src/core/ddl/schema-dialect.ts +19 -6
  34. package/src/core/ddl/schema-diff.ts +22 -0
  35. package/src/core/ddl/schema-generator.ts +22 -0
  36. package/src/core/ddl/schema-plan-executor.ts +6 -0
  37. package/src/core/ddl/schema-types.ts +6 -0
  38. package/src/core/dialect/abstract.ts +2 -2
  39. package/src/core/execution/pooling/pool.ts +12 -7
  40. package/src/core/functions/datetime.ts +57 -33
  41. package/src/core/functions/numeric.ts +95 -30
  42. package/src/core/functions/standard-strategy.ts +35 -0
  43. package/src/core/functions/text.ts +83 -22
  44. package/src/core/functions/types.ts +23 -8
  45. package/src/decorators/bootstrap.ts +16 -4
  46. package/src/decorators/column.ts +17 -0
  47. package/src/decorators/decorator-metadata.ts +27 -0
  48. package/src/decorators/entity.ts +8 -0
  49. package/src/decorators/index.ts +3 -0
  50. package/src/decorators/relations.ts +32 -0
  51. package/src/orm/als.ts +34 -9
  52. package/src/orm/entity-context.ts +54 -0
  53. package/src/orm/entity-metadata.ts +122 -9
  54. package/src/orm/execute.ts +15 -0
  55. package/src/orm/lazy-batch.ts +68 -98
  56. package/src/orm/relations/has-many.ts +44 -0
  57. package/src/query/index.ts +74 -0
  58. package/src/query/target.ts +46 -0
  59. package/src/query-builder/delete-query-state.ts +30 -0
  60. package/src/query-builder/delete.ts +64 -19
  61. package/src/query-builder/hydration-manager.ts +46 -0
  62. package/src/query-builder/insert-query-state.ts +30 -0
  63. package/src/query-builder/insert.ts +46 -2
  64. package/src/query-builder/query-ast-service.ts +5 -0
  65. package/src/query-builder/query-resolution.ts +78 -0
  66. package/src/query-builder/raw-column-parser.ts +5 -0
  67. package/src/query-builder/relation-alias.ts +7 -0
  68. package/src/query-builder/relation-conditions.ts +61 -48
  69. package/src/query-builder/relation-service.ts +68 -63
  70. package/src/query-builder/relation-utils.ts +3 -0
  71. package/src/query-builder/select/cte-facet.ts +40 -0
  72. package/src/query-builder/select/from-facet.ts +80 -0
  73. package/src/query-builder/select/join-facet.ts +62 -0
  74. package/src/query-builder/select/predicate-facet.ts +103 -0
  75. package/src/query-builder/select/projection-facet.ts +69 -0
  76. package/src/query-builder/select/relation-facet.ts +81 -0
  77. package/src/query-builder/select/setop-facet.ts +36 -0
  78. package/src/query-builder/select-helpers.ts +13 -0
  79. package/src/query-builder/select-query-builder-deps.ts +19 -1
  80. package/src/query-builder/select-query-state.ts +2 -1
  81. package/src/query-builder/select.ts +795 -1163
  82. package/src/query-builder/update-query-state.ts +52 -0
  83. package/src/query-builder/update.ts +69 -19
  84. package/src/schema/table-guards.ts +31 -0
@@ -14,9 +14,21 @@ import {
14
14
  import { JoinNode } from '../core/ast/join.js';
15
15
  import { createTableNode } from '../core/ast/builders.js';
16
16
 
17
+ /**
18
+ * Literal values that can be used in UPDATE statements
19
+ */
17
20
  type LiteralValue = string | number | boolean | null;
21
+
22
+ /**
23
+ * Values allowed in UPDATE SET clauses
24
+ */
18
25
  type UpdateValue = OperandNode | LiteralValue;
19
26
 
27
+ /**
28
+ * Type guard to check if a value is valid for UPDATE operations
29
+ * @param value - Value to check
30
+ * @returns True if value is a valid update value
31
+ */
20
32
  const isUpdateValue = (value: unknown): value is UpdateValue => {
21
33
  if (value === null) return true;
22
34
  switch (typeof value) {
@@ -36,6 +48,11 @@ export class UpdateQueryState {
36
48
  public readonly table: TableDef;
37
49
  public readonly ast: UpdateQueryNode;
38
50
 
51
+ /**
52
+ * Creates a new UpdateQueryState instance
53
+ * @param table - Table definition for the update
54
+ * @param ast - Optional existing AST
55
+ */
39
56
  constructor(table: TableDef, ast?: UpdateQueryNode) {
40
57
  this.table = table;
41
58
  this.ast = ast ?? {
@@ -46,10 +63,20 @@ export class UpdateQueryState {
46
63
  };
47
64
  }
48
65
 
66
+ /**
67
+ * Creates a new UpdateQueryState with updated AST
68
+ * @param nextAst - Updated AST
69
+ * @returns New UpdateQueryState instance
70
+ */
49
71
  private clone(nextAst: UpdateQueryNode): UpdateQueryState {
50
72
  return new UpdateQueryState(this.table, nextAst);
51
73
  }
52
74
 
75
+ /**
76
+ * Sets the columns to update with their new values
77
+ * @param values - Record of column names to values
78
+ * @returns New UpdateQueryState with SET clause
79
+ */
53
80
  withSet(values: Record<string, unknown>): UpdateQueryState {
54
81
  const assignments: UpdateAssignmentNode[] = Object.entries(values).map(([column, rawValue]) => {
55
82
  if (!isUpdateValue(rawValue)) {
@@ -74,6 +101,11 @@ export class UpdateQueryState {
74
101
  });
75
102
  }
76
103
 
104
+ /**
105
+ * Adds a WHERE condition to the update query
106
+ * @param expr - WHERE expression
107
+ * @returns New UpdateQueryState with WHERE clause
108
+ */
77
109
  withWhere(expr: ExpressionNode): UpdateQueryState {
78
110
  return this.clone({
79
111
  ...this.ast,
@@ -81,6 +113,11 @@ export class UpdateQueryState {
81
113
  });
82
114
  }
83
115
 
116
+ /**
117
+ * Adds a RETURNING clause to the update query
118
+ * @param columns - Columns to return
119
+ * @returns New UpdateQueryState with RETURNING clause
120
+ */
84
121
  withReturning(columns: ColumnNode[]): UpdateQueryState {
85
122
  return this.clone({
86
123
  ...this.ast,
@@ -88,6 +125,11 @@ export class UpdateQueryState {
88
125
  });
89
126
  }
90
127
 
128
+ /**
129
+ * Sets the FROM clause for the update query
130
+ * @param from - Table source for FROM
131
+ * @returns New UpdateQueryState with FROM clause
132
+ */
91
133
  withFrom(from: TableSourceNode): UpdateQueryState {
92
134
  return this.clone({
93
135
  ...this.ast,
@@ -95,6 +137,11 @@ export class UpdateQueryState {
95
137
  });
96
138
  }
97
139
 
140
+ /**
141
+ * Adds a JOIN to the update query
142
+ * @param join - Join node to add
143
+ * @returns New UpdateQueryState with JOIN
144
+ */
98
145
  withJoin(join: JoinNode): UpdateQueryState {
99
146
  return this.clone({
100
147
  ...this.ast,
@@ -102,6 +149,11 @@ export class UpdateQueryState {
102
149
  });
103
150
  }
104
151
 
152
+ /**
153
+ * Applies an alias to the table being updated
154
+ * @param alias - Alias for the table
155
+ * @returns New UpdateQueryState with table alias
156
+ */
105
157
  withTableAlias(alias: string): UpdateQueryState {
106
158
  return this.clone({
107
159
  ...this.ast,
@@ -2,12 +2,14 @@ import { TableDef } from '../schema/table.js';
2
2
  import { ColumnDef } from '../schema/column.js';
3
3
  import { ColumnNode, ExpressionNode } from '../core/ast/expression.js';
4
4
  import { JOIN_KINDS, JoinKind } from '../core/sql/sql.js';
5
- import { CompiledQuery, UpdateCompiler, Dialect } from '../core/dialect/abstract.js';
5
+ import { CompiledQuery, Dialect } from '../core/dialect/abstract.js';
6
6
  import { DialectKey, resolveDialectInput } from '../core/dialect/dialect-factory.js';
7
7
  import { TableSourceNode, UpdateQueryNode } from '../core/ast/query.js';
8
8
  import { UpdateQueryState } from './update-query-state.js';
9
9
  import { createJoinNode } from '../core/ast/join-node.js';
10
10
  import { buildColumnNode } from '../core/ast/builders.js';
11
+ import { OrmSession } from '../orm/orm-session.js';
12
+ import { QueryResult } from '../core/execution/db-executor.js';
11
13
 
12
14
  type UpdateDialectInput = Dialect | DialectKey;
13
15
 
@@ -18,6 +20,11 @@ export class UpdateQueryBuilder<T> {
18
20
  private readonly table: TableDef;
19
21
  private readonly state: UpdateQueryState;
20
22
 
23
+ /**
24
+ * Creates a new UpdateQueryBuilder instance
25
+ * @param table - The table definition for the UPDATE query
26
+ * @param state - Optional initial query state, defaults to a new UpdateQueryState
27
+ */
21
28
  constructor(table: TableDef, state?: UpdateQueryState) {
22
29
  this.table = table;
23
30
  this.state = state ?? new UpdateQueryState(table);
@@ -27,15 +34,33 @@ export class UpdateQueryBuilder<T> {
27
34
  return new UpdateQueryBuilder(this.table, state);
28
35
  }
29
36
 
37
+ /**
38
+ * Sets an alias for the table in the UPDATE query
39
+ * @param alias - The alias to assign to the table
40
+ * @returns A new UpdateQueryBuilder with the table alias set
41
+ */
30
42
  as(alias: string): UpdateQueryBuilder<T> {
31
43
  return this.clone(this.state.withTableAlias(alias));
32
44
  }
33
45
 
46
+ /**
47
+ * Adds a FROM clause to the UPDATE query
48
+ * @param source - The table source to use in the FROM clause
49
+ * @returns A new UpdateQueryBuilder with the FROM clause added
50
+ */
34
51
  from(source: TableDef | TableSourceNode): UpdateQueryBuilder<T> {
35
52
  const tableSource = this.resolveTableSource(source);
36
53
  return this.clone(this.state.withFrom(tableSource));
37
54
  }
38
55
 
56
+ /**
57
+ * Adds a JOIN clause to the UPDATE query
58
+ * @param table - The table to join with
59
+ * @param condition - The join condition expression
60
+ * @param kind - The type of join (defaults to INNER)
61
+ * @param relationName - Optional name for the relation
62
+ * @returns A new UpdateQueryBuilder with the JOIN clause added
63
+ */
39
64
  join(
40
65
  table: TableDef | TableSourceNode | string,
41
66
  condition: ExpressionNode,
@@ -47,14 +72,29 @@ export class UpdateQueryBuilder<T> {
47
72
  return this.clone(this.state.withJoin(joinNode));
48
73
  }
49
74
 
75
+ /**
76
+ * Adds a SET clause to the UPDATE query
77
+ * @param values - The column-value pairs to update
78
+ * @returns A new UpdateQueryBuilder with the SET clause added
79
+ */
50
80
  set(values: Record<string, unknown>): UpdateQueryBuilder<T> {
51
81
  return this.clone(this.state.withSet(values));
52
82
  }
53
83
 
84
+ /**
85
+ * Adds a WHERE clause to the UPDATE query
86
+ * @param expr - The expression to use as the WHERE condition
87
+ * @returns A new UpdateQueryBuilder with the WHERE clause added
88
+ */
54
89
  where(expr: ExpressionNode): UpdateQueryBuilder<T> {
55
90
  return this.clone(this.state.withWhere(expr));
56
91
  }
57
92
 
93
+ /**
94
+ * Adds a RETURNING clause to the UPDATE query
95
+ * @param columns - Columns to return after update
96
+ * @returns A new UpdateQueryBuilder with the RETURNING clause added
97
+ */
58
98
  returning(...columns: (ColumnDef | ColumnNode)[]): UpdateQueryBuilder<T> {
59
99
  if (!columns.length) return this;
60
100
  const nodes = columns.map(column => buildColumnNode(this.table, column));
@@ -73,29 +113,39 @@ export class UpdateQueryBuilder<T> {
73
113
  return this.resolveTableSource(table);
74
114
  }
75
115
 
76
- // Existing compiler-based compile stays, but we add a new overload.
77
-
78
- // 1) Keep the old behavior (used internally / tests, if any):
79
- compile(compiler: UpdateCompiler): CompiledQuery;
80
- // 2) New ergonomic overload:
81
- compile(dialect: UpdateDialectInput): CompiledQuery;
82
-
83
- compile(arg: UpdateCompiler | UpdateDialectInput): CompiledQuery {
84
- const candidate = arg as { compileUpdate?: (ast: UpdateQueryNode) => CompiledQuery };
85
- if (typeof candidate.compileUpdate === 'function') {
86
- // UpdateCompiler path – old behavior
87
- return candidate.compileUpdate(this.state.ast);
88
- }
116
+ /**
117
+ * Compiles the UPDATE query for the specified dialect
118
+ * @param dialect - The SQL dialect to compile for
119
+ * @returns The compiled query with SQL and parameters
120
+ */
121
+ compile(dialect: UpdateDialectInput): CompiledQuery {
122
+ const resolved = resolveDialectInput(dialect);
123
+ return resolved.compileUpdate(this.state.ast);
124
+ }
89
125
 
90
- // Dialect | string path – new behavior
91
- const dialect = resolveDialectInput(arg as UpdateDialectInput);
92
- return dialect.compileUpdate(this.state.ast);
126
+ /**
127
+ * Returns the SQL string for the UPDATE query
128
+ * @param dialect - The SQL dialect to generate SQL for
129
+ * @returns The SQL string representation of the query
130
+ */
131
+ toSql(dialect: UpdateDialectInput): string {
132
+ return this.compile(dialect).sql;
93
133
  }
94
134
 
95
- toSql(arg: UpdateCompiler | UpdateDialectInput): string {
96
- return this.compile(arg as UpdateCompiler).sql;
135
+ /**
136
+ * Executes the UPDATE query using the provided session
137
+ * @param session - The ORM session to execute the query with
138
+ * @returns A promise that resolves to the query results
139
+ */
140
+ async execute(session: OrmSession): Promise<QueryResult[]> {
141
+ const compiled = this.compile(session.dialect);
142
+ return session.executor.executeSql(compiled.sql, compiled.params);
97
143
  }
98
144
 
145
+ /**
146
+ * Returns the Abstract Syntax Tree (AST) representation of the query
147
+ * @returns The AST node for the UPDATE query
148
+ */
99
149
  getAST(): UpdateQueryNode {
100
150
  return this.state.ast;
101
151
  }
@@ -0,0 +1,31 @@
1
+ import { ColumnDef } from './column.js';
2
+ import type { TableDef } from './table.js';
3
+
4
+ const isColumnsRecord = (columns: unknown): columns is Record<string, ColumnDef> => {
5
+ return typeof columns === 'object' && columns !== null;
6
+ };
7
+
8
+ const isRelationsRecord = (relations: unknown): relations is Record<string, unknown> => {
9
+ return typeof relations === 'object' && relations !== null;
10
+ };
11
+
12
+ export const isTableDef = (value: unknown): value is TableDef => {
13
+ if (typeof value !== 'object' || value === null) {
14
+ return false;
15
+ }
16
+
17
+ const candidate = value as Partial<TableDef>;
18
+ if (typeof candidate.name !== 'string') {
19
+ return false;
20
+ }
21
+
22
+ if (!isColumnsRecord(candidate.columns)) {
23
+ return false;
24
+ }
25
+
26
+ if (!isRelationsRecord(candidate.relations)) {
27
+ return false;
28
+ }
29
+
30
+ return true;
31
+ };