metal-orm 1.0.2 → 1.0.4
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 +20 -0
- package/package.json +1 -1
- package/src/ast/expression.ts +433 -175
- package/src/ast/join.ts +8 -1
- package/src/ast/query.ts +64 -9
- package/src/builder/hydration-manager.ts +42 -11
- package/src/builder/hydration-planner.ts +80 -31
- package/src/builder/operations/column-selector.ts +37 -1
- package/src/builder/operations/cte-manager.ts +16 -0
- package/src/builder/operations/filter-manager.ts +32 -0
- package/src/builder/operations/join-manager.ts +17 -7
- package/src/builder/operations/pagination-manager.ts +19 -0
- package/src/builder/operations/relation-manager.ts +58 -3
- package/src/builder/query-ast-service.ts +100 -29
- package/src/builder/relation-conditions.ts +30 -1
- package/src/builder/relation-projection-helper.ts +43 -1
- package/src/builder/relation-service.ts +68 -13
- package/src/builder/relation-types.ts +6 -0
- package/src/builder/select-query-builder-deps.ts +64 -3
- package/src/builder/select-query-state.ts +72 -0
- package/src/builder/select.ts +166 -0
- package/src/codegen/typescript.ts +142 -44
- package/src/constants/sql-operator-config.ts +36 -0
- package/src/constants/sql.ts +125 -58
- package/src/dialect/abstract.ts +97 -22
- package/src/dialect/mssql/index.ts +27 -0
- package/src/dialect/mysql/index.ts +22 -0
- package/src/dialect/postgres/index.ts +22 -0
- package/src/dialect/sqlite/index.ts +22 -0
- package/src/runtime/als.ts +15 -1
- package/src/runtime/hydration.ts +20 -15
- package/src/schema/column.ts +45 -5
- package/src/schema/relation.ts +49 -2
- package/src/schema/table.ts +27 -3
- package/src/utils/join-node.ts +20 -0
- package/src/utils/raw-column-parser.ts +32 -0
- package/src/utils/relation-alias.ts +43 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ColumnNode } from '../ast/expression';
|
|
2
|
+
import { CommonTableExpressionNode } from '../ast/query';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Best-effort helper that tries to convert a raw column expression into a `ColumnNode`.
|
|
6
|
+
* This parser is intentionally limited; use it only for simple references or function calls.
|
|
7
|
+
*/
|
|
8
|
+
export const parseRawColumn = (
|
|
9
|
+
col: string,
|
|
10
|
+
tableName: string,
|
|
11
|
+
ctes?: CommonTableExpressionNode[]
|
|
12
|
+
): ColumnNode => {
|
|
13
|
+
if (col.includes('(')) {
|
|
14
|
+
const [fn, rest] = col.split('(');
|
|
15
|
+
const colName = rest.replace(')', '');
|
|
16
|
+
const [table, name] = colName.includes('.') ? colName.split('.') : [tableName, colName];
|
|
17
|
+
return { type: 'Column', table, name, alias: col };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (col.includes('.')) {
|
|
21
|
+
const [potentialCteName, columnName] = col.split('.');
|
|
22
|
+
const hasCte = ctes?.some(cte => cte.name === potentialCteName);
|
|
23
|
+
|
|
24
|
+
if (hasCte) {
|
|
25
|
+
return { type: 'Column', table: tableName, name: col };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { type: 'Column', table: potentialCteName, name: columnName };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return { type: 'Column', table: tableName, name: col };
|
|
32
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Separator used when projecting relational columns
|
|
3
|
+
*/
|
|
4
|
+
const RELATION_SEPARATOR = '__';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parts of a relation alias
|
|
8
|
+
*/
|
|
9
|
+
export interface RelationAliasParts {
|
|
10
|
+
/**
|
|
11
|
+
* Relation name (left side of the separator)
|
|
12
|
+
*/
|
|
13
|
+
relationName: string;
|
|
14
|
+
/**
|
|
15
|
+
* Column name (right side of the separator)
|
|
16
|
+
*/
|
|
17
|
+
columnName: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Builds a relation alias from the relation name and column name components.
|
|
22
|
+
*/
|
|
23
|
+
export const makeRelationAlias = (relationName: string, columnName: string): string =>
|
|
24
|
+
`${relationName}${RELATION_SEPARATOR}${columnName}`;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parses a relation alias into its relation/column components.
|
|
28
|
+
* Returns `null` when the alias does not follow the `relation__column` pattern.
|
|
29
|
+
*/
|
|
30
|
+
export const parseRelationAlias = (alias: string): RelationAliasParts | null => {
|
|
31
|
+
const idx = alias.indexOf(RELATION_SEPARATOR);
|
|
32
|
+
if (idx === -1) return null;
|
|
33
|
+
return {
|
|
34
|
+
relationName: alias.slice(0, idx),
|
|
35
|
+
columnName: alias.slice(idx + RELATION_SEPARATOR.length)
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Determines whether an alias represents a relation column by checking the `__` convention.
|
|
41
|
+
*/
|
|
42
|
+
export const isRelationAlias = (alias?: string): boolean =>
|
|
43
|
+
!!alias && alias.includes(RELATION_SEPARATOR);
|