metal-orm 1.0.14 → 1.0.15

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 (115) hide show
  1. package/README.md +40 -45
  2. package/dist/decorators/index.cjs +1600 -27
  3. package/dist/decorators/index.cjs.map +1 -1
  4. package/dist/decorators/index.d.cts +6 -2
  5. package/dist/decorators/index.d.ts +6 -2
  6. package/dist/decorators/index.js +1599 -27
  7. package/dist/decorators/index.js.map +1 -1
  8. package/dist/index.cjs +4608 -3429
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +511 -159
  11. package/dist/index.d.ts +511 -159
  12. package/dist/index.js +4526 -3415
  13. package/dist/index.js.map +1 -1
  14. package/dist/{select-CCp1oz9p.d.cts → select-Bkv8g8u_.d.cts} +193 -67
  15. package/dist/{select-CCp1oz9p.d.ts → select-Bkv8g8u_.d.ts} +193 -67
  16. package/package.json +1 -1
  17. package/src/codegen/typescript.ts +38 -35
  18. package/src/core/ast/adapters.ts +21 -0
  19. package/src/core/ast/aggregate-functions.ts +13 -13
  20. package/src/core/ast/builders.ts +56 -43
  21. package/src/core/ast/expression-builders.ts +34 -34
  22. package/src/core/ast/expression-nodes.ts +18 -16
  23. package/src/core/ast/expression-visitor.ts +122 -69
  24. package/src/core/ast/expression.ts +6 -4
  25. package/src/core/ast/join-metadata.ts +15 -0
  26. package/src/core/ast/join-node.ts +22 -20
  27. package/src/core/ast/join.ts +5 -5
  28. package/src/core/ast/query.ts +52 -88
  29. package/src/core/ast/types.ts +20 -0
  30. package/src/core/ast/window-functions.ts +55 -55
  31. package/src/core/ddl/dialects/base-schema-dialect.ts +20 -6
  32. package/src/core/ddl/dialects/mssql-schema-dialect.ts +32 -8
  33. package/src/core/ddl/dialects/mysql-schema-dialect.ts +21 -10
  34. package/src/core/ddl/dialects/postgres-schema-dialect.ts +52 -7
  35. package/src/core/ddl/dialects/sqlite-schema-dialect.ts +23 -9
  36. package/src/core/ddl/introspect/catalogs/index.ts +1 -0
  37. package/src/core/ddl/introspect/catalogs/postgres.ts +143 -0
  38. package/src/core/ddl/introspect/context.ts +9 -0
  39. package/src/core/ddl/introspect/functions/postgres.ts +26 -0
  40. package/src/core/ddl/introspect/mssql.ts +149 -149
  41. package/src/core/ddl/introspect/mysql.ts +99 -99
  42. package/src/core/ddl/introspect/postgres.ts +245 -154
  43. package/src/core/ddl/introspect/registry.ts +26 -0
  44. package/src/core/ddl/introspect/run-select.ts +25 -0
  45. package/src/core/ddl/introspect/sqlite.ts +7 -7
  46. package/src/core/ddl/introspect/types.ts +23 -19
  47. package/src/core/ddl/introspect/utils.ts +1 -1
  48. package/src/core/ddl/naming-strategy.ts +10 -0
  49. package/src/core/ddl/schema-dialect.ts +41 -0
  50. package/src/core/ddl/schema-diff.ts +211 -179
  51. package/src/core/ddl/schema-generator.ts +16 -90
  52. package/src/core/ddl/schema-introspect.ts +25 -32
  53. package/src/core/ddl/schema-plan-executor.ts +17 -0
  54. package/src/core/ddl/schema-types.ts +46 -39
  55. package/src/core/ddl/sql-writing.ts +170 -0
  56. package/src/core/dialect/abstract.ts +144 -126
  57. package/src/core/dialect/base/cte-compiler.ts +33 -0
  58. package/src/core/dialect/base/function-table-formatter.ts +132 -0
  59. package/src/core/dialect/base/groupby-compiler.ts +21 -0
  60. package/src/core/dialect/base/join-compiler.ts +26 -0
  61. package/src/core/dialect/base/orderby-compiler.ts +21 -0
  62. package/src/core/dialect/base/pagination-strategy.ts +32 -0
  63. package/src/core/dialect/base/returning-strategy.ts +56 -0
  64. package/src/core/dialect/base/sql-dialect.ts +181 -204
  65. package/src/core/dialect/dialect-factory.ts +91 -0
  66. package/src/core/dialect/mssql/functions.ts +101 -0
  67. package/src/core/dialect/mssql/index.ts +128 -126
  68. package/src/core/dialect/mysql/functions.ts +101 -0
  69. package/src/core/dialect/mysql/index.ts +20 -18
  70. package/src/core/dialect/postgres/functions.ts +95 -0
  71. package/src/core/dialect/postgres/index.ts +30 -28
  72. package/src/core/dialect/sqlite/functions.ts +115 -0
  73. package/src/core/dialect/sqlite/index.ts +30 -28
  74. package/src/core/driver/database-driver.ts +11 -0
  75. package/src/core/driver/mssql-driver.ts +20 -0
  76. package/src/core/driver/mysql-driver.ts +20 -0
  77. package/src/core/driver/postgres-driver.ts +20 -0
  78. package/src/core/driver/sqlite-driver.ts +20 -0
  79. package/src/core/execution/db-executor.ts +63 -0
  80. package/src/core/execution/executors/mssql-executor.ts +39 -0
  81. package/src/core/execution/executors/mysql-executor.ts +47 -0
  82. package/src/core/execution/executors/postgres-executor.ts +32 -0
  83. package/src/core/execution/executors/sqlite-executor.ts +31 -0
  84. package/src/core/functions/datetime.ts +132 -0
  85. package/src/core/functions/numeric.ts +179 -0
  86. package/src/core/functions/standard-strategy.ts +47 -0
  87. package/src/core/functions/text.ts +147 -0
  88. package/src/core/functions/types.ts +18 -0
  89. package/src/core/hydration/types.ts +57 -0
  90. package/src/decorators/bootstrap.ts +10 -0
  91. package/src/decorators/relations.ts +15 -0
  92. package/src/index.ts +30 -19
  93. package/src/orm/entity-metadata.ts +7 -0
  94. package/src/orm/entity.ts +58 -27
  95. package/src/orm/hydration.ts +25 -17
  96. package/src/orm/lazy-batch.ts +46 -2
  97. package/src/orm/orm-context.ts +60 -60
  98. package/src/orm/query-logger.ts +1 -1
  99. package/src/orm/relation-change-processor.ts +43 -2
  100. package/src/orm/relations/has-one.ts +139 -0
  101. package/src/orm/transaction-runner.ts +1 -1
  102. package/src/orm/unit-of-work.ts +60 -60
  103. package/src/query-builder/delete.ts +22 -5
  104. package/src/query-builder/hydration-manager.ts +2 -1
  105. package/src/query-builder/hydration-planner.ts +8 -7
  106. package/src/query-builder/insert.ts +22 -5
  107. package/src/query-builder/relation-conditions.ts +9 -8
  108. package/src/query-builder/relation-service.ts +3 -2
  109. package/src/query-builder/select.ts +66 -61
  110. package/src/query-builder/update.ts +22 -5
  111. package/src/schema/column.ts +246 -246
  112. package/src/schema/relation.ts +35 -1
  113. package/src/schema/table.ts +28 -28
  114. package/src/schema/types.ts +41 -31
  115. package/src/orm/db-executor.ts +0 -11
@@ -0,0 +1,132 @@
1
+ import { CompilerContext } from '../abstract.js';
2
+ import { SqlDialectBase } from './sql-dialect.js';
3
+
4
+ export interface FunctionTableNode {
5
+ type: 'FunctionTable';
6
+ schema?: string;
7
+ name: string;
8
+ args?: unknown[];
9
+ lateral?: boolean;
10
+ withOrdinality?: boolean;
11
+ alias?: string;
12
+ columnAliases?: string[];
13
+ }
14
+
15
+ /**
16
+ * Formatter for function table expressions (e.g., LATERAL unnest(...) WITH ORDINALITY).
17
+ * Encapsulates logic for generating SQL function table syntax including LATERAL, aliases, and column lists.
18
+ */
19
+ export class FunctionTableFormatter {
20
+ /**
21
+ * Formats a function table node into SQL syntax.
22
+ * @param fn - The function table node containing schema, name, args, and aliases.
23
+ * @param ctx - Optional compiler context for operand compilation.
24
+ * @param dialect - The dialect instance for compiling operands.
25
+ * @returns SQL function table expression (e.g., "LATERAL schema.func(args) WITH ORDINALITY AS alias(col1, col2)").
26
+ */
27
+ static format(fn: FunctionTableNode, ctx?: CompilerContext, dialect?: SqlDialectBase): string {
28
+ const schemaPart = this.formatSchema(fn, dialect);
29
+ const args = this.formatArgs(fn, ctx, dialect);
30
+ const base = this.formatBase(fn, schemaPart, args, dialect);
31
+ const lateral = this.formatLateral(fn);
32
+ const alias = this.formatAlias(fn, dialect);
33
+ const colAliases = this.formatColumnAliases(fn, dialect);
34
+ return `${lateral}${base}${alias}${colAliases}`;
35
+ }
36
+
37
+ /**
38
+ * Formats the schema prefix for the function name.
39
+ * @param fn - The function table node.
40
+ * @param dialect - The dialect instance for quoting identifiers.
41
+ * @returns Schema prefix (e.g., "schema.") or empty string.
42
+ * @internal
43
+ */
44
+ private static formatSchema(fn: FunctionTableNode, dialect?: SqlDialectBase): string {
45
+ if (!fn.schema) return '';
46
+ const quoted = dialect ? dialect.quoteIdentifier(fn.schema) : fn.schema;
47
+ return `${quoted}.`;
48
+ }
49
+
50
+ /**
51
+ * Formats function arguments into SQL syntax.
52
+ * @param fn - The function table node containing arguments.
53
+ * @param ctx - Optional compiler context for operand compilation.
54
+ * @param dialect - The dialect instance for compiling operands.
55
+ * @returns Comma-separated function arguments.
56
+ * @internal
57
+ */
58
+ private static formatArgs(fn: FunctionTableNode, ctx?: CompilerContext, dialect?: SqlDialectBase): string {
59
+ return (fn.args || [])
60
+ .map((a: any) => {
61
+ if (ctx && dialect) {
62
+ return (dialect as any).compileOperand(a, ctx);
63
+ }
64
+ return String(a);
65
+ })
66
+ .join(', ');
67
+ }
68
+
69
+ /**
70
+ * Formats the base function call with WITH ORDINALITY if present.
71
+ * @param fn - The function table node.
72
+ * @param schemaPart - Formatted schema prefix.
73
+ * @param args - Formatted function arguments.
74
+ * @param dialect - The dialect instance for quoting identifiers.
75
+ * @returns Base function call expression (e.g., "schema.func(args) WITH ORDINALITY").
76
+ * @internal
77
+ */
78
+ private static formatBase(fn: FunctionTableNode, schemaPart: string, args: string, dialect?: SqlDialectBase): string {
79
+ const ordinality = fn.withOrdinality ? ' WITH ORDINALITY' : '';
80
+ const quoted = dialect ? dialect.quoteIdentifier(fn.name) : fn.name;
81
+ return `${schemaPart}${quoted}(${args})${ordinality}`;
82
+ }
83
+
84
+ /**
85
+ * Formats the LATERAL keyword if present.
86
+ * @param fn - The function table node.
87
+ * @returns "LATERAL " or empty string.
88
+ * @internal
89
+ */
90
+ private static formatLateral(fn: FunctionTableNode): string {
91
+ return fn.lateral ? 'LATERAL ' : '';
92
+ }
93
+
94
+ /**
95
+ * Formats the table alias for the function table.
96
+ * @param fn - The function table node.
97
+ * @param dialect - The dialect instance for quoting identifiers.
98
+ * @returns " AS alias" or empty string.
99
+ * @internal
100
+ */
101
+ private static formatAlias(fn: FunctionTableNode, dialect?: SqlDialectBase): string {
102
+ if (!fn.alias) return '';
103
+ const quoted = dialect ? dialect.quoteIdentifier(fn.alias) : fn.alias;
104
+ return ` AS ${quoted}`;
105
+ }
106
+
107
+ /**
108
+ * Formats column aliases for the function table result columns.
109
+ * @param fn - The function table node containing column aliases.
110
+ * @param dialect - The dialect instance for quoting identifiers.
111
+ * @returns "(col1, col2, ...)" or empty string.
112
+ * @internal
113
+ */
114
+ private static formatColumnAliases(fn: FunctionTableNode, dialect?: SqlDialectBase): string {
115
+ if (!fn.columnAliases || !fn.columnAliases.length) return '';
116
+ const aliases = fn.columnAliases
117
+ .map(col => dialect ? dialect.quoteIdentifier(col) : col)
118
+ .join(', ');
119
+ return `(${aliases})`;
120
+ }
121
+ }
122
+
123
+ export interface FunctionTableNode {
124
+ type: 'FunctionTable';
125
+ schema?: string;
126
+ name: string;
127
+ args?: unknown[];
128
+ lateral?: boolean;
129
+ withOrdinality?: boolean;
130
+ alias?: string;
131
+ columnAliases?: string[];
132
+ }
@@ -0,0 +1,21 @@
1
+ import { SelectQueryNode } from '../../ast/query.js';
2
+
3
+ /**
4
+ * Compiler for GROUP BY clauses in SELECT statements.
5
+ * Handles compilation of column grouping expressions.
6
+ */
7
+ export class GroupByCompiler {
8
+ /**
9
+ * Compiles GROUP BY clause from a SELECT query AST.
10
+ * @param ast - The SELECT query AST containing grouping columns.
11
+ * @param quoteIdentifier - Function to quote identifiers according to dialect rules.
12
+ * @returns SQL GROUP BY clause (e.g., " GROUP BY table.col1, table.col2") or empty string if no grouping.
13
+ */
14
+ static compileGroupBy(ast: SelectQueryNode, quoteIdentifier: (id: string) => string): string {
15
+ if (!ast.groupBy || ast.groupBy.length === 0) return '';
16
+ const cols = ast.groupBy
17
+ .map(c => `${quoteIdentifier(c.table)}.${quoteIdentifier(c.name)}`)
18
+ .join(', ');
19
+ return ` GROUP BY ${cols}`;
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ import { SelectQueryNode } from '../../ast/query.js';
2
+ import { CompilerContext } from '../abstract.js';
3
+
4
+ /**
5
+ * Compiler for JOIN clauses in SELECT statements.
6
+ * Handles compilation of all join types (INNER, LEFT, RIGHT, FULL, CROSS).
7
+ */
8
+ export class JoinCompiler {
9
+ /**
10
+ * Compiles all JOIN clauses from a SELECT query AST.
11
+ * @param ast - The SELECT query AST containing join definitions.
12
+ * @param ctx - The compiler context for expression compilation.
13
+ * @param compileFrom - Function to compile table sources (tables or subqueries).
14
+ * @param compileExpression - Function to compile join condition expressions.
15
+ * @returns SQL JOIN clauses (e.g., " LEFT JOIN table ON condition") or empty string if no joins.
16
+ */
17
+ static compileJoins(ast: SelectQueryNode, ctx: CompilerContext, compileFrom: (from: any, ctx: CompilerContext) => string, compileExpression: (expr: any, ctx: CompilerContext) => string): string {
18
+ if (!ast.joins || ast.joins.length === 0) return '';
19
+ const parts = ast.joins.map(j => {
20
+ const table = compileFrom(j.table as any, ctx);
21
+ const cond = compileExpression(j.condition, ctx);
22
+ return `${j.kind} JOIN ${table} ON ${cond}`;
23
+ });
24
+ return ` ${parts.join(' ')}`;
25
+ }
26
+ }
@@ -0,0 +1,21 @@
1
+ import { SelectQueryNode } from '../../ast/query.js';
2
+
3
+ /**
4
+ * Compiler for ORDER BY clauses in SELECT statements.
5
+ * Handles compilation of sorting expressions with direction (ASC/DESC).
6
+ */
7
+ export class OrderByCompiler {
8
+ /**
9
+ * Compiles ORDER BY clause from a SELECT query AST.
10
+ * @param ast - The SELECT query AST containing sort specifications.
11
+ * @param quoteIdentifier - Function to quote identifiers according to dialect rules.
12
+ * @returns SQL ORDER BY clause (e.g., " ORDER BY table.col1 ASC, table.col2 DESC") or empty string if no ordering.
13
+ */
14
+ static compileOrderBy(ast: SelectQueryNode, quoteIdentifier: (id: string) => string): string {
15
+ if (!ast.orderBy || ast.orderBy.length === 0) return '';
16
+ const parts = ast.orderBy
17
+ .map(o => `${quoteIdentifier(o.column.table)}.${quoteIdentifier(o.column.name)} ${o.direction}`)
18
+ .join(', ');
19
+ return ` ORDER BY ${parts}`;
20
+ }
21
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Strategy interface for compiling pagination clauses.
3
+ * Allows dialects to customize how pagination (LIMIT/OFFSET, ROWS FETCH, etc.) is generated.
4
+ */
5
+ export interface PaginationStrategy {
6
+ /**
7
+ * Compiles pagination logic into SQL clause.
8
+ * @param limit - The limit value, if present.
9
+ * @param offset - The offset value, if present.
10
+ * @returns SQL pagination clause (e.g., " LIMIT 10 OFFSET 0") or empty string if no pagination.
11
+ */
12
+ compilePagination(limit?: number, offset?: number): string;
13
+ }
14
+
15
+ /**
16
+ * Standard SQL pagination using LIMIT and OFFSET.
17
+ * Implements the ANSI SQL-style pagination with LIMIT/OFFSET syntax.
18
+ */
19
+ export class StandardLimitOffsetPagination implements PaginationStrategy {
20
+ /**
21
+ * Compiles LIMIT/OFFSET pagination clause.
22
+ * @param limit - The maximum number of rows to return.
23
+ * @param offset - The number of rows to skip.
24
+ * @returns SQL pagination clause with LIMIT and/or OFFSET.
25
+ */
26
+ compilePagination(limit?: number, offset?: number): string {
27
+ const parts: string[] = [];
28
+ if (limit !== undefined) parts.push(`LIMIT ${limit}`);
29
+ if (offset !== undefined) parts.push(`OFFSET ${offset}`);
30
+ return parts.length ? ` ${parts.join(' ')}` : '';
31
+ }
32
+ }
@@ -0,0 +1,56 @@
1
+ import { ColumnNode } from '../../ast/expression.js';
2
+ import { CompilerContext } from '../abstract.js';
3
+
4
+ /**
5
+ * Strategy interface for handling RETURNING clauses in DML statements (INSERT, UPDATE, DELETE).
6
+ * Different SQL dialects have varying levels of support for RETURNING clauses.
7
+ */
8
+ export interface ReturningStrategy {
9
+ /**
10
+ * Compiles a RETURNING clause for DML statements.
11
+ * @param returning - Array of columns to return, or undefined if none.
12
+ * @param ctx - The compiler context for expression compilation.
13
+ * @returns SQL RETURNING clause or empty string if not supported.
14
+ * @throws Error if RETURNING is not supported by this dialect.
15
+ */
16
+ compileReturning(returning: ColumnNode[] | undefined, ctx: CompilerContext): string;
17
+ /**
18
+ * Formats column list for RETURNING clause.
19
+ * @param returning - Array of columns to format.
20
+ * @param quoteIdentifier - Function to quote identifiers according to dialect rules.
21
+ * @returns Formatted column list (e.g., "table.col1, table.col2").
22
+ */
23
+ formatReturningColumns(returning: ColumnNode[], quoteIdentifier: (id: string) => string): string;
24
+ }
25
+
26
+ /**
27
+ * Default RETURNING strategy that throws an error when RETURNING is used.
28
+ * Use this for dialects that don't support RETURNING clauses.
29
+ */
30
+ export class NoReturningStrategy implements ReturningStrategy {
31
+ /**
32
+ * Throws an error as RETURNING is not supported.
33
+ * @param returning - Columns to return (causes error if non-empty).
34
+ * @param _ctx - Compiler context (unused).
35
+ * @throws Error indicating RETURNING is not supported.
36
+ */
37
+ compileReturning(returning: ColumnNode[] | undefined, _ctx: CompilerContext): string {
38
+ if (!returning || returning.length === 0) return '';
39
+ throw new Error('RETURNING is not supported by this dialect.');
40
+ }
41
+ /**
42
+ * Formats column names for RETURNING clause.
43
+ * @param returning - Columns to format.
44
+ * @param quoteIdentifier - Function to quote identifiers according to dialect rules.
45
+ * @returns Simple comma-separated column names.
46
+ */
47
+ formatReturningColumns(returning: ColumnNode[], quoteIdentifier: (id: string) => string): string {
48
+ return returning
49
+ .map(column => {
50
+ const tablePart = column.table ? `${quoteIdentifier(column.table)}.` : '';
51
+ const aliasPart = column.alias ? ` AS ${quoteIdentifier(column.alias)}` : '';
52
+ return `${tablePart}${quoteIdentifier(column.name)}${aliasPart}`;
53
+ })
54
+ .join(', ');
55
+ }
56
+ }