metal-orm 1.0.56 → 1.0.57
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 +21 -20
- package/dist/index.cjs +821 -112
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +429 -70
- package/dist/index.d.ts +429 -70
- package/dist/index.js +785 -112
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/naming-strategy.ts +3 -1
- package/src/codegen/typescript.ts +20 -10
- package/src/core/ast/aggregate-functions.ts +14 -0
- package/src/core/ast/builders.ts +38 -20
- package/src/core/ast/expression-builders.ts +70 -2
- package/src/core/ast/expression-nodes.ts +305 -274
- package/src/core/ast/expression-visitor.ts +11 -1
- package/src/core/ast/expression.ts +4 -0
- package/src/core/ast/query.ts +3 -0
- package/src/core/ddl/introspect/catalogs/mysql.ts +5 -0
- package/src/core/ddl/introspect/catalogs/sqlite.ts +3 -0
- package/src/core/ddl/introspect/functions/mssql.ts +13 -0
- package/src/core/ddl/introspect/mssql.ts +4 -0
- package/src/core/ddl/introspect/mysql.ts +4 -0
- package/src/core/ddl/introspect/sqlite.ts +4 -0
- package/src/core/dialect/abstract.ts +552 -531
- package/src/core/dialect/base/function-table-formatter.ts +9 -30
- package/src/core/dialect/base/sql-dialect.ts +24 -0
- package/src/core/dialect/mssql/functions.ts +40 -2
- package/src/core/dialect/mysql/functions.ts +16 -2
- package/src/core/dialect/postgres/functions.ts +66 -2
- package/src/core/dialect/postgres/index.ts +17 -4
- package/src/core/dialect/postgres/table-functions.ts +27 -0
- package/src/core/dialect/sqlite/functions.ts +34 -0
- package/src/core/dialect/sqlite/index.ts +17 -1
- package/src/core/driver/database-driver.ts +9 -1
- package/src/core/driver/mssql-driver.ts +3 -0
- package/src/core/driver/mysql-driver.ts +3 -0
- package/src/core/driver/postgres-driver.ts +3 -0
- package/src/core/driver/sqlite-driver.ts +3 -0
- package/src/core/execution/executors/mssql-executor.ts +5 -0
- package/src/core/execution/executors/mysql-executor.ts +5 -0
- package/src/core/execution/executors/postgres-executor.ts +5 -0
- package/src/core/execution/executors/sqlite-executor.ts +5 -0
- package/src/core/functions/array.ts +26 -0
- package/src/core/functions/control-flow.ts +69 -0
- package/src/core/functions/datetime.ts +50 -0
- package/src/core/functions/definitions/aggregate.ts +16 -0
- package/src/core/functions/definitions/control-flow.ts +24 -0
- package/src/core/functions/definitions/datetime.ts +36 -0
- package/src/core/functions/definitions/helpers.ts +29 -0
- package/src/core/functions/definitions/json.ts +49 -0
- package/src/core/functions/definitions/numeric.ts +55 -0
- package/src/core/functions/definitions/string.ts +43 -0
- package/src/core/functions/function-registry.ts +48 -0
- package/src/core/functions/group-concat-helpers.ts +57 -0
- package/src/core/functions/json.ts +38 -0
- package/src/core/functions/numeric.ts +14 -0
- package/src/core/functions/standard-strategy.ts +86 -115
- package/src/core/functions/standard-table-strategy.ts +13 -0
- package/src/core/functions/table-types.ts +15 -0
- package/src/core/functions/text.ts +57 -0
- package/src/core/sql/sql.ts +59 -38
- package/src/decorators/bootstrap.ts +5 -4
- package/src/index.ts +18 -11
- package/src/orm/hydration-context.ts +10 -0
- package/src/orm/identity-map.ts +19 -0
- package/src/orm/interceptor-pipeline.ts +4 -0
- package/src/orm/relations/belongs-to.ts +17 -0
- package/src/orm/relations/has-one.ts +17 -0
- package/src/orm/relations/many-to-many.ts +41 -0
- package/src/query-builder/select.ts +68 -68
- package/src/schema/table-guards.ts +6 -0
- package/src/schema/types.ts +8 -1
package/package.json
CHANGED
|
@@ -36,7 +36,9 @@ export class DefaultNamingStrategy implements NamingStrategy {
|
|
|
36
36
|
? table
|
|
37
37
|
: table.type === 'DerivedTable'
|
|
38
38
|
? table.alias
|
|
39
|
-
: table.
|
|
39
|
+
: table.type === 'FunctionTable'
|
|
40
|
+
? table.alias ?? table.name
|
|
41
|
+
: table.name;
|
|
40
42
|
|
|
41
43
|
// Handle schema-qualified names (e.g., "auth.user" → "AuthUser")
|
|
42
44
|
if (tableName.includes('.')) {
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
FunctionNode,
|
|
19
19
|
AliasRefNode,
|
|
20
20
|
CastExpressionNode,
|
|
21
|
+
CollateExpressionNode,
|
|
21
22
|
ExpressionVisitor,
|
|
22
23
|
OperandVisitor,
|
|
23
24
|
visitExpression,
|
|
@@ -183,16 +184,17 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
|
|
|
183
184
|
return `${this.namingStrategy.tableToSymbol(term.table)}.${term.name}`;
|
|
184
185
|
case 'AliasRef':
|
|
185
186
|
return this.visitAliasRef(term);
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
187
|
+
case 'Literal':
|
|
188
|
+
case 'Function':
|
|
189
|
+
case 'JsonPath':
|
|
190
|
+
case 'ScalarSubquery':
|
|
191
|
+
case 'CaseExpression':
|
|
192
|
+
case 'WindowFunction':
|
|
193
|
+
case 'Cast':
|
|
194
|
+
case 'Collate':
|
|
195
|
+
return this.printOperand(term);
|
|
196
|
+
default:
|
|
197
|
+
return this.printExpression(term);
|
|
196
198
|
}
|
|
197
199
|
}
|
|
198
200
|
|
|
@@ -270,6 +272,10 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
|
|
|
270
272
|
return this.printCastOperand(node);
|
|
271
273
|
}
|
|
272
274
|
|
|
275
|
+
public visitCollate(node: CollateExpressionNode): string {
|
|
276
|
+
return this.printCollateOperand(node);
|
|
277
|
+
}
|
|
278
|
+
|
|
273
279
|
public visitAliasRef(node: AliasRefNode): string {
|
|
274
280
|
return `aliasRef('${node.name}')`;
|
|
275
281
|
}
|
|
@@ -466,6 +472,10 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
|
|
|
466
472
|
return `cast(${this.printOperand(node.expression)}, '${typeLiteral}')`;
|
|
467
473
|
}
|
|
468
474
|
|
|
475
|
+
private printCollateOperand(node: CollateExpressionNode): string {
|
|
476
|
+
return `collate(${this.printOperand(node.expression)}, '${node.collation}')`;
|
|
477
|
+
}
|
|
478
|
+
|
|
469
479
|
/**
|
|
470
480
|
* Converts method chain lines to inline format
|
|
471
481
|
* @param lines - Method chain lines
|
|
@@ -84,3 +84,17 @@ export const groupConcat = (
|
|
|
84
84
|
orderBy: options?.orderBy?.map(toOrderByNode),
|
|
85
85
|
separator: options?.separator !== undefined ? valueToOperand(options.separator) : undefined
|
|
86
86
|
});
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Creates a STDDEV function expression
|
|
90
|
+
* @param col - Column to calculate standard deviation for
|
|
91
|
+
* @returns Function node with STDDEV
|
|
92
|
+
*/
|
|
93
|
+
export const stddev = buildAggregate('STDDEV');
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Creates a VARIANCE function expression
|
|
97
|
+
* @param col - Column to calculate variance for
|
|
98
|
+
* @returns Function node with VARIANCE
|
|
99
|
+
*/
|
|
100
|
+
export const variance = buildAggregate('VARIANCE');
|
package/src/core/ast/builders.ts
CHANGED
|
@@ -74,26 +74,44 @@ export const createTableNode = (table: TableRef): TableNode => ({
|
|
|
74
74
|
/**
|
|
75
75
|
* Creates a FunctionTable node for expressions like `function_name(args...)` used in FROM
|
|
76
76
|
*/
|
|
77
|
-
export const fnTable = (
|
|
78
|
-
name: string,
|
|
79
|
-
args: OperandNode[] = [],
|
|
80
|
-
alias?: string,
|
|
81
|
-
opts?: { lateral?: boolean; withOrdinality?: boolean; columnAliases?: string[]; schema?: string }
|
|
82
|
-
): FunctionTableNode => ({
|
|
83
|
-
type: 'FunctionTable',
|
|
84
|
-
name,
|
|
85
|
-
args,
|
|
86
|
-
alias,
|
|
87
|
-
lateral: opts?.lateral,
|
|
88
|
-
withOrdinality: opts?.withOrdinality,
|
|
89
|
-
columnAliases: opts?.columnAliases,
|
|
90
|
-
schema: opts?.schema
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
77
|
+
export const fnTable = (
|
|
78
|
+
name: string,
|
|
79
|
+
args: OperandNode[] = [],
|
|
80
|
+
alias?: string,
|
|
81
|
+
opts?: { lateral?: boolean; withOrdinality?: boolean; columnAliases?: string[]; schema?: string }
|
|
82
|
+
): FunctionTableNode => ({
|
|
83
|
+
type: 'FunctionTable',
|
|
84
|
+
name,
|
|
85
|
+
args,
|
|
86
|
+
alias,
|
|
87
|
+
lateral: opts?.lateral,
|
|
88
|
+
withOrdinality: opts?.withOrdinality,
|
|
89
|
+
columnAliases: opts?.columnAliases,
|
|
90
|
+
schema: opts?.schema
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Dialect-aware table function (portable intent key, not a real SQL function name).
|
|
94
|
+
export const tvf = (
|
|
95
|
+
key: string,
|
|
96
|
+
args: OperandNode[] = [],
|
|
97
|
+
alias?: string,
|
|
98
|
+
opts?: { lateral?: boolean; withOrdinality?: boolean; columnAliases?: string[]; schema?: string }
|
|
99
|
+
): FunctionTableNode => ({
|
|
100
|
+
type: 'FunctionTable',
|
|
101
|
+
key,
|
|
102
|
+
name: key,
|
|
103
|
+
args,
|
|
104
|
+
alias,
|
|
105
|
+
lateral: opts?.lateral,
|
|
106
|
+
withOrdinality: opts?.withOrdinality,
|
|
107
|
+
columnAliases: opts?.columnAliases,
|
|
108
|
+
schema: opts?.schema
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Creates a derived table node wrapping a subquery.
|
|
113
|
+
*/
|
|
114
|
+
export const derivedTable = (
|
|
97
115
|
query: import('./query.js').SelectQueryNode,
|
|
98
116
|
alias: string,
|
|
99
117
|
columnAliases?: string[]
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SelectQueryNode } from './query.js';
|
|
2
|
-
import { SqlOperator } from '../sql/sql.js';
|
|
2
|
+
import { SqlOperator, BitwiseOperator } from '../sql/sql.js';
|
|
3
3
|
import { ColumnRef } from './types.js';
|
|
4
4
|
import {
|
|
5
5
|
ColumnNode,
|
|
@@ -19,7 +19,9 @@ import {
|
|
|
19
19
|
BetweenExpressionNode,
|
|
20
20
|
isOperandNode,
|
|
21
21
|
AliasRefNode,
|
|
22
|
-
ArithmeticExpressionNode
|
|
22
|
+
ArithmeticExpressionNode,
|
|
23
|
+
BitwiseExpressionNode,
|
|
24
|
+
CollateExpressionNode
|
|
23
25
|
} from './expression-nodes.js';
|
|
24
26
|
|
|
25
27
|
export type LiteralValue = LiteralNode['value'];
|
|
@@ -378,6 +380,57 @@ export const div = (
|
|
|
378
380
|
right: OperandNode | ColumnRef | string | number
|
|
379
381
|
): ArithmeticExpressionNode => createArithmeticExpression('/', left, right);
|
|
380
382
|
|
|
383
|
+
const createBitwiseExpression = (
|
|
384
|
+
operator: BitwiseOperator,
|
|
385
|
+
left: OperandNode | ColumnRef,
|
|
386
|
+
right: OperandNode | ColumnRef | string | number
|
|
387
|
+
): BitwiseExpressionNode => ({
|
|
388
|
+
type: 'BitwiseExpression',
|
|
389
|
+
left: toOperand(left),
|
|
390
|
+
operator,
|
|
391
|
+
right: toOperand(right)
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Creates a bitwise AND expression (left & right)
|
|
396
|
+
*/
|
|
397
|
+
export const bitAnd = (
|
|
398
|
+
left: OperandNode | ColumnRef,
|
|
399
|
+
right: OperandNode | ColumnRef | string | number
|
|
400
|
+
): BitwiseExpressionNode => createBitwiseExpression('&', left, right);
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Creates a bitwise OR expression (left | right)
|
|
404
|
+
*/
|
|
405
|
+
export const bitOr = (
|
|
406
|
+
left: OperandNode | ColumnRef,
|
|
407
|
+
right: OperandNode | ColumnRef | string | number
|
|
408
|
+
): BitwiseExpressionNode => createBitwiseExpression('|', left, right);
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Creates a bitwise XOR expression (left ^ right)
|
|
412
|
+
*/
|
|
413
|
+
export const bitXor = (
|
|
414
|
+
left: OperandNode | ColumnRef,
|
|
415
|
+
right: OperandNode | ColumnRef | string | number
|
|
416
|
+
): BitwiseExpressionNode => createBitwiseExpression('^', left, right);
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Creates a bitwise shift left expression (left << right)
|
|
420
|
+
*/
|
|
421
|
+
export const shiftLeft = (
|
|
422
|
+
left: OperandNode | ColumnRef,
|
|
423
|
+
right: OperandNode | ColumnRef | string | number
|
|
424
|
+
): BitwiseExpressionNode => createBitwiseExpression('<<', left, right);
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Creates a bitwise shift right expression (left >> right)
|
|
428
|
+
*/
|
|
429
|
+
export const shiftRight = (
|
|
430
|
+
left: OperandNode | ColumnRef,
|
|
431
|
+
right: OperandNode | ColumnRef | string | number
|
|
432
|
+
): BitwiseExpressionNode => createBitwiseExpression('>>', left, right);
|
|
433
|
+
|
|
381
434
|
/**
|
|
382
435
|
* Creates a JSON path expression
|
|
383
436
|
* @param col - Source column
|
|
@@ -441,3 +494,18 @@ export const notExists = (subquery: SelectQueryNode): ExistsExpressionNode => ({
|
|
|
441
494
|
operator: 'NOT EXISTS',
|
|
442
495
|
subquery
|
|
443
496
|
});
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Creates a COLLATE expression (expression COLLATE collationName)
|
|
500
|
+
* @param expression - Expression to be collated
|
|
501
|
+
* @param collation - Collation name
|
|
502
|
+
* @returns COLLATE expression node
|
|
503
|
+
*/
|
|
504
|
+
export const collate = (
|
|
505
|
+
expression: OperandNode | ColumnRef | string | number | boolean | null,
|
|
506
|
+
collation: string
|
|
507
|
+
): CollateExpressionNode => ({
|
|
508
|
+
type: 'Collate',
|
|
509
|
+
expression: toOperand(expression),
|
|
510
|
+
collation
|
|
511
|
+
});
|