@mikro-orm/knex 7.0.0-dev.9 → 7.0.0-dev.91
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/AbstractSqlConnection.d.ts +11 -5
- package/AbstractSqlConnection.js +78 -32
- package/AbstractSqlDriver.d.ts +9 -5
- package/AbstractSqlDriver.js +267 -227
- package/AbstractSqlPlatform.js +5 -5
- package/PivotCollectionPersister.d.ts +8 -4
- package/PivotCollectionPersister.js +55 -31
- package/README.md +3 -2
- package/SqlEntityManager.d.ts +10 -2
- package/SqlEntityManager.js +11 -2
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +2 -0
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +42 -3
- package/dialects/mysql/MySqlExceptionConverter.d.ts +3 -3
- package/dialects/mysql/MySqlExceptionConverter.js +4 -5
- package/dialects/mysql/MySqlSchemaHelper.js +2 -2
- package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +1 -0
- package/dialects/postgresql/PostgreSqlTableCompiler.js +1 -0
- package/dialects/sqlite/BaseSqliteConnection.d.ts +3 -2
- package/dialects/sqlite/BaseSqliteConnection.js +2 -14
- package/dialects/sqlite/BaseSqlitePlatform.js +1 -2
- package/dialects/sqlite/SqliteExceptionConverter.d.ts +2 -2
- package/dialects/sqlite/SqliteExceptionConverter.js +6 -4
- package/dialects/sqlite/SqliteSchemaHelper.js +5 -6
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/package.json +5 -5
- package/plugin/index.d.ts +53 -0
- package/plugin/index.js +42 -0
- package/plugin/transformer.d.ts +115 -0
- package/plugin/transformer.js +883 -0
- package/query/ArrayCriteriaNode.d.ts +1 -0
- package/query/ArrayCriteriaNode.js +3 -0
- package/query/CriteriaNode.d.ts +4 -5
- package/query/CriteriaNode.js +13 -9
- package/query/CriteriaNodeFactory.js +12 -7
- package/query/NativeQueryBuilder.js +1 -1
- package/query/ObjectCriteriaNode.d.ts +1 -0
- package/query/ObjectCriteriaNode.js +35 -8
- package/query/QueryBuilder.d.ts +59 -10
- package/query/QueryBuilder.js +166 -50
- package/query/QueryBuilderHelper.d.ts +1 -1
- package/query/QueryBuilderHelper.js +20 -14
- package/query/ScalarCriteriaNode.d.ts +3 -3
- package/query/ScalarCriteriaNode.js +9 -7
- package/query/index.d.ts +1 -0
- package/query/index.js +1 -0
- package/query/raw.d.ts +59 -0
- package/query/raw.js +68 -0
- package/query/rawKnex.d.ts +58 -0
- package/query/rawKnex.js +72 -0
- package/schema/DatabaseSchema.js +25 -4
- package/schema/DatabaseTable.d.ts +5 -4
- package/schema/DatabaseTable.js +65 -34
- package/schema/SchemaComparator.js +5 -6
- package/schema/SchemaHelper.d.ts +2 -0
- package/schema/SchemaHelper.js +14 -10
- package/schema/SqlSchemaGenerator.d.ts +13 -6
- package/schema/SqlSchemaGenerator.js +41 -20
- package/typings.d.ts +85 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "7.0.0-dev.
|
|
3
|
+
"version": "7.0.0-dev.91",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://mikro-orm.io",
|
|
40
40
|
"engines": {
|
|
41
|
-
"node": ">= 22.
|
|
41
|
+
"node": ">= 22.17.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "yarn clean && yarn compile && yarn copy",
|
|
@@ -50,13 +50,13 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"kysely": "0.28.
|
|
53
|
+
"kysely": "0.28.8",
|
|
54
54
|
"sqlstring": "2.3.3"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@mikro-orm/core": "^6.
|
|
57
|
+
"@mikro-orm/core": "^6.6.2"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
60
|
+
"@mikro-orm/core": "7.0.0-dev.91"
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type KyselyPlugin, type PluginTransformQueryArgs, type PluginTransformResultArgs, type QueryResult, type RootOperationNode, type UnknownRow } from 'kysely';
|
|
2
|
+
import { MikroTransformer } from './transformer.js';
|
|
3
|
+
import type { SqlEntityManager } from '../SqlEntityManager.js';
|
|
4
|
+
import type { EntityMetadata } from '@mikro-orm/core';
|
|
5
|
+
/**
|
|
6
|
+
* Cache for query transformation data
|
|
7
|
+
* Stores the query node and metadata about tables/aliases
|
|
8
|
+
*/
|
|
9
|
+
interface QueryTransformCache {
|
|
10
|
+
entityMap: Map<string, EntityMetadata>;
|
|
11
|
+
}
|
|
12
|
+
export interface MikroKyselyPluginOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Use database table names ('table') or entity names ('entity') in queries.
|
|
15
|
+
*
|
|
16
|
+
* @default 'table'
|
|
17
|
+
*/
|
|
18
|
+
tableNamingStrategy?: 'table' | 'entity';
|
|
19
|
+
/**
|
|
20
|
+
* Use database column names ('column') or property names ('property') in queries.
|
|
21
|
+
*
|
|
22
|
+
* @default 'column'
|
|
23
|
+
*/
|
|
24
|
+
columnNamingStrategy?: 'column' | 'property';
|
|
25
|
+
/**
|
|
26
|
+
* Automatically process entity `onCreate` hooks in INSERT queries.
|
|
27
|
+
*
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
processOnCreateHooks?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Automatically process entity `onUpdate` hooks in UPDATE queries.
|
|
33
|
+
*
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
processOnUpdateHooks?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Convert JavaScript values to database-compatible values (e.g., Date to timestamp, custom types).
|
|
39
|
+
*
|
|
40
|
+
* @default false
|
|
41
|
+
*/
|
|
42
|
+
convertValues?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export declare class MikroKyselyPlugin implements KyselyPlugin {
|
|
45
|
+
protected readonly em: SqlEntityManager;
|
|
46
|
+
protected readonly options: MikroKyselyPluginOptions;
|
|
47
|
+
protected static queryNodeCache: WeakMap<any, QueryTransformCache>;
|
|
48
|
+
protected readonly transformer: MikroTransformer;
|
|
49
|
+
constructor(em: SqlEntityManager, options?: MikroKyselyPluginOptions);
|
|
50
|
+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode;
|
|
51
|
+
transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>>;
|
|
52
|
+
}
|
|
53
|
+
export {};
|
package/plugin/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { SelectQueryNode as SelectQueryNodeClass, InsertQueryNode as InsertQueryNodeClass, UpdateQueryNode as UpdateQueryNodeClass, DeleteQueryNode as DeleteQueryNodeClass, } from 'kysely';
|
|
2
|
+
import { MikroTransformer } from './transformer.js';
|
|
3
|
+
export class MikroKyselyPlugin {
|
|
4
|
+
em;
|
|
5
|
+
options;
|
|
6
|
+
static queryNodeCache = new WeakMap();
|
|
7
|
+
transformer;
|
|
8
|
+
constructor(em, options = {}) {
|
|
9
|
+
this.em = em;
|
|
10
|
+
this.options = options;
|
|
11
|
+
this.transformer = new MikroTransformer(em, options);
|
|
12
|
+
}
|
|
13
|
+
transformQuery(args) {
|
|
14
|
+
this.transformer.reset();
|
|
15
|
+
const result = this.transformer.transformNode(args.node, args.queryId);
|
|
16
|
+
// Cache the entity map if it is one we can process (for use in transformResult)
|
|
17
|
+
if (SelectQueryNodeClass.is(args.node) ||
|
|
18
|
+
InsertQueryNodeClass.is(args.node) ||
|
|
19
|
+
UpdateQueryNodeClass.is(args.node) ||
|
|
20
|
+
DeleteQueryNodeClass.is(args.node)) {
|
|
21
|
+
MikroKyselyPlugin.queryNodeCache.set(args.queryId, { entityMap: this.transformer.getOutputEntityMap() });
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
async transformResult(args) {
|
|
26
|
+
// Only transform results if columnNamingStrategy is 'property' or convertValues is true
|
|
27
|
+
if (this.options.columnNamingStrategy !== 'property' && !this.options.convertValues) {
|
|
28
|
+
return args.result;
|
|
29
|
+
}
|
|
30
|
+
// Retrieve the cached query node and metadata
|
|
31
|
+
const cache = MikroKyselyPlugin.queryNodeCache.get(args.queryId);
|
|
32
|
+
if (!cache) {
|
|
33
|
+
return args.result;
|
|
34
|
+
}
|
|
35
|
+
// Transform the result rows using the transformer
|
|
36
|
+
const transformedRows = this.transformer.transformResult(args.result.rows ?? [], cache.entityMap);
|
|
37
|
+
return {
|
|
38
|
+
...args.result,
|
|
39
|
+
rows: transformedRows ?? [],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { type EntityMetadata, type EntityProperty, type MetadataStorage } from '@mikro-orm/core';
|
|
2
|
+
import { type CommonTableExpressionNameNode, type DeleteQueryNode, type IdentifierNode, type InsertQueryNode, type JoinNode, type MergeQueryNode, type QueryId, type SelectQueryNode, type UpdateQueryNode, type WithNode, ColumnNode, OperationNodeTransformer, TableNode } from 'kysely';
|
|
3
|
+
import type { MikroKyselyPluginOptions } from './index.js';
|
|
4
|
+
import type { SqlEntityManager } from '../SqlEntityManager.js';
|
|
5
|
+
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
|
|
6
|
+
export declare class MikroTransformer extends OperationNodeTransformer {
|
|
7
|
+
protected readonly em: SqlEntityManager;
|
|
8
|
+
protected readonly options: MikroKyselyPluginOptions;
|
|
9
|
+
/**
|
|
10
|
+
* Context stack to support nested queries (subqueries, CTEs)
|
|
11
|
+
* Each level of query scope has its own Map of table aliases/names to EntityMetadata
|
|
12
|
+
* Top of stack (highest index) is the current scope
|
|
13
|
+
*/
|
|
14
|
+
protected readonly contextStack: Map<string, EntityMetadata | undefined>[];
|
|
15
|
+
/**
|
|
16
|
+
* Subquery alias map: maps subquery/CTE alias to its source table metadata
|
|
17
|
+
* Used to resolve columns from subqueries/CTEs to their original table definitions
|
|
18
|
+
*/
|
|
19
|
+
protected readonly subqueryAliasMap: Map<string, EntityMetadata | undefined>;
|
|
20
|
+
protected readonly metadata: MetadataStorage;
|
|
21
|
+
protected readonly platform: AbstractSqlPlatform;
|
|
22
|
+
/**
|
|
23
|
+
* Global map of all entities involved in the query.
|
|
24
|
+
* Populated during AST transformation and used for result transformation.
|
|
25
|
+
*/
|
|
26
|
+
protected readonly entityMap: Map<string, EntityMetadata<any>>;
|
|
27
|
+
constructor(em: SqlEntityManager, options?: MikroKyselyPluginOptions);
|
|
28
|
+
reset(): void;
|
|
29
|
+
getOutputEntityMap(): Map<string, EntityMetadata>;
|
|
30
|
+
transformSelectQuery(node: SelectQueryNode, queryId: QueryId): SelectQueryNode;
|
|
31
|
+
transformInsertQuery(node: InsertQueryNode, queryId?: QueryId): InsertQueryNode;
|
|
32
|
+
transformUpdateQuery(node: UpdateQueryNode, queryId?: QueryId): UpdateQueryNode;
|
|
33
|
+
transformDeleteQuery(node: DeleteQueryNode, queryId?: QueryId): DeleteQueryNode;
|
|
34
|
+
transformMergeQuery(node: MergeQueryNode, queryId?: QueryId): MergeQueryNode;
|
|
35
|
+
transformIdentifier(node: IdentifierNode, queryId: QueryId): IdentifierNode;
|
|
36
|
+
/**
|
|
37
|
+
* Find owner entity metadata for the current identifier in the context stack.
|
|
38
|
+
* Supports both aliased and non-aliased table references.
|
|
39
|
+
* Searches up the context stack to support correlated subqueries.
|
|
40
|
+
* Also checks subquery/CTE aliases to resolve to their source tables.
|
|
41
|
+
*/
|
|
42
|
+
findOwnerEntityInContext(): EntityMetadata | undefined;
|
|
43
|
+
processOnCreateHooks(node: InsertQueryNode, meta: EntityMetadata): InsertQueryNode;
|
|
44
|
+
processOnUpdateHooks(node: UpdateQueryNode, meta: EntityMetadata): UpdateQueryNode;
|
|
45
|
+
processInsertValues(node: InsertQueryNode, meta: EntityMetadata): InsertQueryNode;
|
|
46
|
+
processUpdateValues(node: UpdateQueryNode, meta: EntityMetadata): UpdateQueryNode;
|
|
47
|
+
mapColumnsToProperties(columns: readonly ColumnNode[], meta: EntityMetadata): (EntityProperty | undefined)[];
|
|
48
|
+
normalizeColumnName(identifier: IdentifierNode): string;
|
|
49
|
+
findProperty(meta: EntityMetadata | undefined, columnName?: string): EntityProperty | undefined;
|
|
50
|
+
shouldConvertValues(): boolean;
|
|
51
|
+
prepareInputValue(prop: EntityProperty | undefined, value: unknown, enabled: boolean): unknown;
|
|
52
|
+
/**
|
|
53
|
+
* Look up a table name/alias in the context stack.
|
|
54
|
+
* Searches from current scope (top of stack) to parent scopes (bottom).
|
|
55
|
+
* This supports correlated subqueries and references to outer query tables.
|
|
56
|
+
*/
|
|
57
|
+
lookupInContextStack(tableNameOrAlias: string): EntityMetadata | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Process WITH node (CTE definitions)
|
|
60
|
+
*/
|
|
61
|
+
processWithNode(withNode: WithNode, context: Map<string, EntityMetadata | undefined>): void;
|
|
62
|
+
/**
|
|
63
|
+
* Extract CTE name from CommonTableExpressionNameNode
|
|
64
|
+
*/
|
|
65
|
+
getCTEName(nameNode: CommonTableExpressionNameNode): string | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Process a FROM item (can be TableNode or AliasNode)
|
|
68
|
+
*/
|
|
69
|
+
processFromItem(from: any, // OperationNode type - can be TableNode, AliasNode, or SelectQueryNode
|
|
70
|
+
context: Map<string, EntityMetadata | undefined>): void;
|
|
71
|
+
/**
|
|
72
|
+
* Process a JOIN node
|
|
73
|
+
*/
|
|
74
|
+
processJoinNode(join: JoinNode, context: Map<string, EntityMetadata | undefined>): void;
|
|
75
|
+
/**
|
|
76
|
+
* Extract the primary source table from a SELECT query
|
|
77
|
+
* This helps resolve columns from subqueries to their original entity tables
|
|
78
|
+
*/
|
|
79
|
+
extractSourceTableFromSelectQuery(selectQuery: SelectQueryNode): EntityMetadata | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Extract alias name from an alias node
|
|
82
|
+
*/
|
|
83
|
+
extractAliasName(alias: any): string | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Extract table name from a TableNode
|
|
86
|
+
*/
|
|
87
|
+
getTableName(node: TableNode | undefined): string | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* Find entity metadata by table name or entity name
|
|
90
|
+
*/
|
|
91
|
+
findEntityMetadata(name: string): EntityMetadata | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* Transform result rows by mapping database column names to property names
|
|
94
|
+
* This is called for SELECT queries when columnNamingStrategy is 'property'
|
|
95
|
+
*/
|
|
96
|
+
transformResult(rows: Record<string, any>[] | undefined, entityMap: Map<string, EntityMetadata>): Record<string, any>[] | undefined;
|
|
97
|
+
buildGlobalFieldMap(entityMap: Map<string, EntityMetadata>): Record<string, EntityProperty>;
|
|
98
|
+
buildGlobalRelationFieldMap(entityMap: Map<string, EntityMetadata>): Record<string, string>;
|
|
99
|
+
/**
|
|
100
|
+
* Build a mapping from database field names to property objects
|
|
101
|
+
* Format: { 'field_name': EntityProperty }
|
|
102
|
+
*/
|
|
103
|
+
buildFieldToPropertyMap(meta: EntityMetadata, alias?: string): Record<string, EntityProperty>;
|
|
104
|
+
/**
|
|
105
|
+
* Build a mapping for relation fields
|
|
106
|
+
* For ManyToOne relations, we need to map from the foreign key field to the relation property
|
|
107
|
+
* Format: { 'foreign_key_field': 'relationPropertyName' }
|
|
108
|
+
*/
|
|
109
|
+
buildRelationFieldMap(meta: EntityMetadata, alias?: string): Record<string, string>;
|
|
110
|
+
/**
|
|
111
|
+
* Transform a single row by mapping column names to property names
|
|
112
|
+
*/
|
|
113
|
+
transformRow(row: Record<string, any>, fieldToPropertyMap: Record<string, EntityProperty>, relationFieldMap: Record<string, string>): Record<string, any>;
|
|
114
|
+
prepareOutputValue(prop: EntityProperty | undefined, value: unknown): unknown;
|
|
115
|
+
}
|