@mikro-orm/knex 6.0.0-dev.9 → 6.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 +5 -2
- package/AbstractSqlConnection.js +25 -17
- package/AbstractSqlDriver.d.ts +19 -18
- package/AbstractSqlDriver.js +171 -70
- package/AbstractSqlPlatform.d.ts +1 -0
- package/AbstractSqlPlatform.js +26 -5
- package/MonkeyPatchable.d.ts +1 -0
- package/MonkeyPatchable.js +3 -0
- package/README.md +64 -107
- package/SqlEntityManager.d.ts +2 -5
- package/SqlEntityManager.js +5 -9
- package/SqlEntityRepository.d.ts +6 -4
- package/SqlEntityRepository.js +10 -8
- package/index.mjs +20 -6
- package/package.json +7 -7
- package/query/ArrayCriteriaNode.d.ts +3 -3
- package/query/CriteriaNode.d.ts +8 -8
- package/query/CriteriaNode.js +9 -9
- package/query/CriteriaNodeFactory.d.ts +6 -6
- package/query/CriteriaNodeFactory.js +10 -13
- package/query/ObjectCriteriaNode.d.ts +3 -3
- package/query/ObjectCriteriaNode.js +12 -8
- package/query/QueryBuilder.d.ts +27 -16
- package/query/QueryBuilder.js +250 -91
- package/query/QueryBuilderHelper.d.ts +6 -9
- package/query/QueryBuilderHelper.js +112 -96
- package/query/ScalarCriteriaNode.d.ts +3 -2
- package/query/ScalarCriteriaNode.js +9 -6
- package/query/enums.js +1 -1
- package/schema/DatabaseSchema.d.ts +4 -1
- package/schema/DatabaseSchema.js +22 -6
- package/schema/DatabaseTable.d.ts +2 -1
- package/schema/DatabaseTable.js +25 -24
- package/schema/SchemaComparator.js +9 -2
- package/schema/SchemaHelper.d.ts +5 -3
- package/schema/SchemaHelper.js +10 -4
- package/schema/SqlSchemaGenerator.d.ts +3 -5
- package/schema/SqlSchemaGenerator.js +41 -20
- package/typings.d.ts +10 -7
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import type { AnyEntity, Configuration, ConnectionOptions, EntityData, IsolationLevel, QueryResult, Transaction, TransactionEventBroadcaster } from '@mikro-orm/core';
|
|
2
|
+
import type { AnyEntity, Configuration, ConnectionOptions, EntityData, IsolationLevel, LoggingOptions, QueryResult, Transaction, TransactionEventBroadcaster } from '@mikro-orm/core';
|
|
3
3
|
import { Connection } from '@mikro-orm/core';
|
|
4
4
|
import type { AbstractSqlPlatform } from './AbstractSqlPlatform';
|
|
5
5
|
export declare abstract class AbstractSqlConnection extends Connection {
|
|
@@ -7,6 +7,9 @@ export declare abstract class AbstractSqlConnection extends Connection {
|
|
|
7
7
|
protected platform: AbstractSqlPlatform;
|
|
8
8
|
protected client: Knex;
|
|
9
9
|
constructor(config: Configuration, options?: ConnectionOptions, type?: 'read' | 'write');
|
|
10
|
+
abstract createKnex(): void;
|
|
11
|
+
/** @inheritDoc */
|
|
12
|
+
connect(): void | Promise<void>;
|
|
10
13
|
getKnex(): Knex;
|
|
11
14
|
close(force?: boolean): Promise<void>;
|
|
12
15
|
isConnected(): Promise<boolean>;
|
|
@@ -22,7 +25,7 @@ export declare abstract class AbstractSqlConnection extends Connection {
|
|
|
22
25
|
}): Promise<Knex.Transaction>;
|
|
23
26
|
commit(ctx: Knex.Transaction, eventBroadcaster?: TransactionEventBroadcaster): Promise<void>;
|
|
24
27
|
rollback(ctx: Knex.Transaction, eventBroadcaster?: TransactionEventBroadcaster): Promise<void>;
|
|
25
|
-
execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(queryOrKnex: string | Knex.QueryBuilder | Knex.Raw, params?: unknown[], method?: 'all' | 'get' | 'run', ctx?: Transaction): Promise<T>;
|
|
28
|
+
execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(queryOrKnex: string | Knex.QueryBuilder | Knex.Raw, params?: unknown[], method?: 'all' | 'get' | 'run', ctx?: Transaction, logging?: LoggingOptions): Promise<T>;
|
|
26
29
|
/**
|
|
27
30
|
* Execute raw SQL queries from file
|
|
28
31
|
*/
|
package/AbstractSqlConnection.js
CHANGED
|
@@ -14,16 +14,23 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
14
14
|
super(config, options, type);
|
|
15
15
|
this.patchKnexClient();
|
|
16
16
|
}
|
|
17
|
+
/** @inheritDoc */
|
|
18
|
+
connect() {
|
|
19
|
+
this.createKnex();
|
|
20
|
+
}
|
|
17
21
|
getKnex() {
|
|
22
|
+
if (!this.client) {
|
|
23
|
+
this.createKnex();
|
|
24
|
+
}
|
|
18
25
|
return this.client;
|
|
19
26
|
}
|
|
20
27
|
async close(force) {
|
|
21
28
|
await super.close(force);
|
|
22
|
-
await this.
|
|
29
|
+
await this.getKnex().destroy();
|
|
23
30
|
}
|
|
24
31
|
async isConnected() {
|
|
25
32
|
try {
|
|
26
|
-
await this.
|
|
33
|
+
await this.getKnex().raw('select 1');
|
|
27
34
|
return true;
|
|
28
35
|
}
|
|
29
36
|
catch {
|
|
@@ -46,7 +53,7 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
46
53
|
if (!options.ctx) {
|
|
47
54
|
await options.eventBroadcaster?.dispatchEvent(core_1.EventType.beforeTransactionStart);
|
|
48
55
|
}
|
|
49
|
-
const trx = await (options.ctx || this.
|
|
56
|
+
const trx = await (options.ctx || this.getKnex()).transaction(null, { isolationLevel: options.isolationLevel });
|
|
50
57
|
if (!options.ctx) {
|
|
51
58
|
await options.eventBroadcaster?.dispatchEvent(core_1.EventType.afterTransactionStart, trx);
|
|
52
59
|
}
|
|
@@ -76,30 +83,31 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
76
83
|
await eventBroadcaster?.dispatchEvent(core_1.EventType.afterTransactionRollback, ctx);
|
|
77
84
|
}
|
|
78
85
|
}
|
|
79
|
-
async execute(queryOrKnex, params = [], method = 'all', ctx) {
|
|
86
|
+
async execute(queryOrKnex, params = [], method = 'all', ctx, logging) {
|
|
87
|
+
await this.ensureConnection();
|
|
80
88
|
if (core_1.Utils.isObject(queryOrKnex)) {
|
|
81
|
-
ctx
|
|
89
|
+
ctx ??= (queryOrKnex.client.transacting ? queryOrKnex : null);
|
|
82
90
|
const q = queryOrKnex.toSQL();
|
|
83
91
|
queryOrKnex = q.sql;
|
|
84
92
|
params = q.bindings;
|
|
85
93
|
}
|
|
86
94
|
const formatted = this.platform.formatQuery(queryOrKnex, params);
|
|
87
|
-
const sql = this.getSql(queryOrKnex, formatted);
|
|
88
|
-
|
|
89
|
-
const query = this.
|
|
95
|
+
const sql = this.getSql(queryOrKnex, formatted, logging);
|
|
96
|
+
return this.executeQuery(sql, async () => {
|
|
97
|
+
const query = this.getKnex().raw(formatted);
|
|
90
98
|
if (ctx) {
|
|
91
99
|
query.transacting(ctx);
|
|
92
100
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
101
|
+
const res = await query;
|
|
102
|
+
return this.transformRawResult(res, method);
|
|
103
|
+
}, { query: queryOrKnex, params, ...logging });
|
|
96
104
|
}
|
|
97
105
|
/**
|
|
98
106
|
* Execute raw SQL queries from file
|
|
99
107
|
*/
|
|
100
108
|
async loadFile(path) {
|
|
101
109
|
const buf = await (0, fs_extra_1.readFile)(path);
|
|
102
|
-
await this.
|
|
110
|
+
await this.getKnex().raw(buf.toString());
|
|
103
111
|
}
|
|
104
112
|
createKnexClient(type) {
|
|
105
113
|
const driverOptions = this.config.get('driverOptions');
|
|
@@ -115,7 +123,7 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
115
123
|
});
|
|
116
124
|
}
|
|
117
125
|
getKnexOptions(type) {
|
|
118
|
-
const config = core_1.Utils.
|
|
126
|
+
const config = core_1.Utils.mergeConfig({
|
|
119
127
|
client: type,
|
|
120
128
|
connection: this.getConnectionOptions(),
|
|
121
129
|
pool: this.config.get('pool'),
|
|
@@ -138,15 +146,15 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
138
146
|
};
|
|
139
147
|
return config;
|
|
140
148
|
}
|
|
141
|
-
getSql(query, formatted) {
|
|
149
|
+
getSql(query, formatted, context) {
|
|
142
150
|
const logger = this.config.getLogger();
|
|
143
|
-
if (!logger.isEnabled('query')) {
|
|
151
|
+
if (!logger.isEnabled('query', context)) {
|
|
144
152
|
return query;
|
|
145
153
|
}
|
|
146
|
-
if (logger.isEnabled('query-params')) {
|
|
154
|
+
if (logger.isEnabled('query-params', context)) {
|
|
147
155
|
return formatted;
|
|
148
156
|
}
|
|
149
|
-
return this.
|
|
157
|
+
return this.getKnex().client.positionBindings(query);
|
|
150
158
|
}
|
|
151
159
|
/**
|
|
152
160
|
* do not call `positionBindings` when there are no bindings - it was messing up with
|
package/AbstractSqlDriver.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import type { AnyEntity, Collection, ConnectionType, Configuration, Constructor, CountOptions, DeleteOptions, Dictionary, DriverMethodOptions, EntityData, EntityDictionary, EntityField, EntityMetadata, EntityName, EntityProperty, FilterQuery, FindOneOptions, FindOptions, IDatabaseDriver, LockOptions, NativeInsertUpdateManyOptions, NativeInsertUpdateOptions, PopulateOptions, Primary, QueryOrderMap, QueryResult, Transaction } from '@mikro-orm/core';
|
|
2
|
+
import type { AnyEntity, Collection, ConnectionType, Configuration, Constructor, CountOptions, DeleteOptions, Dictionary, DriverMethodOptions, EntityData, EntityDictionary, EntityField, EntityMetadata, EntityName, EntityProperty, FilterQuery, FindOneOptions, FindOptions, IDatabaseDriver, LockOptions, NativeInsertUpdateManyOptions, NativeInsertUpdateOptions, PopulateOptions, Primary, QueryOrderMap, QueryResult, Transaction, OrderDefinition, LoggingOptions } from '@mikro-orm/core';
|
|
3
3
|
import { DatabaseDriver, EntityManagerType } from '@mikro-orm/core';
|
|
4
4
|
import type { AbstractSqlConnection } from './AbstractSqlConnection';
|
|
5
5
|
import type { AbstractSqlPlatform } from './AbstractSqlPlatform';
|
|
6
6
|
import { QueryBuilder, QueryType } from './query';
|
|
7
7
|
import { SqlEntityManager } from './SqlEntityManager';
|
|
8
8
|
import type { Field } from './typings';
|
|
9
|
-
export declare abstract class AbstractSqlDriver<
|
|
9
|
+
export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection = AbstractSqlConnection, Platform extends AbstractSqlPlatform = AbstractSqlPlatform> extends DatabaseDriver<Connection> {
|
|
10
10
|
[EntityManagerType]: SqlEntityManager<this>;
|
|
11
|
-
protected readonly connection:
|
|
12
|
-
protected readonly replicas:
|
|
13
|
-
protected readonly platform:
|
|
14
|
-
protected constructor(config: Configuration, platform:
|
|
15
|
-
getPlatform():
|
|
11
|
+
protected readonly connection: Connection;
|
|
12
|
+
protected readonly replicas: Connection[];
|
|
13
|
+
protected readonly platform: Platform;
|
|
14
|
+
protected constructor(config: Configuration, platform: Platform, connection: Constructor<Connection>, connector: string[]);
|
|
15
|
+
getPlatform(): Platform;
|
|
16
16
|
createEntityManager<D extends IDatabaseDriver = IDatabaseDriver>(useContext?: boolean): D[typeof EntityManagerType];
|
|
17
|
-
find<T extends object, P extends string = never>(entityName: string, where: FilterQuery<T>, options?: FindOptions<T, P>): Promise<EntityData<T>[]>;
|
|
18
|
-
findOne<T extends object, P extends string = never>(entityName: string, where: FilterQuery<T>, options?: FindOneOptions<T, P>): Promise<EntityData<T> | null>;
|
|
19
|
-
findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any>): Promise<EntityData<T>[]>;
|
|
17
|
+
find<T extends object, P extends string = never, F extends string = '*'>(entityName: string, where: FilterQuery<T>, options?: FindOptions<T, P, F>): Promise<EntityData<T>[]>;
|
|
18
|
+
findOne<T extends object, P extends string = never, F extends string = '*'>(entityName: string, where: FilterQuery<T>, options?: FindOneOptions<T, P, F>): Promise<EntityData<T> | null>;
|
|
19
|
+
findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any, any>): Promise<EntityData<T>[]>;
|
|
20
20
|
countVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: CountOptions<T>): Promise<number>;
|
|
21
|
-
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>, type: QueryType.COUNT): Promise<number>;
|
|
22
|
-
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>, type: QueryType.SELECT): Promise<T[]>;
|
|
23
|
-
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>): Promise<T[]>;
|
|
21
|
+
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any, any>, type: QueryType.COUNT): Promise<number>;
|
|
22
|
+
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any, any>, type: QueryType.SELECT): Promise<T[]>;
|
|
23
|
+
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any, any>): Promise<T[]>;
|
|
24
24
|
mapResult<T extends object>(result: EntityData<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[], qb?: QueryBuilder<T>, map?: Dictionary): EntityData<T> | null;
|
|
25
25
|
private mapJoinedProps;
|
|
26
26
|
private appendToCollection;
|
|
@@ -31,12 +31,13 @@ export declare abstract class AbstractSqlDriver<C extends AbstractSqlConnection
|
|
|
31
31
|
nativeUpdateMany<T extends object>(entityName: string, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
|
|
32
32
|
nativeDelete<T extends object>(entityName: string, where: FilterQuery<T> | string | any, options?: DeleteOptions<T>): Promise<QueryResult<T>>;
|
|
33
33
|
syncCollection<T extends object, O extends object>(coll: Collection<T, O>, options?: DriverMethodOptions): Promise<void>;
|
|
34
|
-
loadFromPivotTable<T extends object, O extends object>(prop: EntityProperty, owners: Primary<O>[][], where?: FilterQuery<any>, orderBy?:
|
|
34
|
+
loadFromPivotTable<T extends object, O extends object>(prop: EntityProperty, owners: Primary<O>[][], where?: FilterQuery<any>, orderBy?: OrderDefinition<T>, ctx?: Transaction, options?: FindOptions<T, any, any>): Promise<Dictionary<T[]>>;
|
|
35
|
+
private getPivotOrderBy;
|
|
35
36
|
execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(queryOrKnex: string | Knex.QueryBuilder | Knex.Raw, params?: any[], method?: 'all' | 'get' | 'run', ctx?: Transaction): Promise<T>;
|
|
36
37
|
/**
|
|
37
38
|
* 1:1 owner side needs to be marked for population so QB auto-joins the owner id
|
|
38
39
|
*/
|
|
39
|
-
protected autoJoinOneToOneOwner<T extends object, P extends string = never>(meta: EntityMetadata
|
|
40
|
+
protected autoJoinOneToOneOwner<T extends object, P extends string = never>(meta: EntityMetadata<T>, populate: PopulateOptions<T>[], fields?: readonly EntityField<T, P>[]): PopulateOptions<T>[];
|
|
40
41
|
protected joinedProps<T>(meta: EntityMetadata, populate: PopulateOptions<T>[]): PopulateOptions<T>[];
|
|
41
42
|
/**
|
|
42
43
|
* @internal
|
|
@@ -48,17 +49,17 @@ export declare abstract class AbstractSqlDriver<C extends AbstractSqlConnection
|
|
|
48
49
|
*/
|
|
49
50
|
mapPropToFieldNames<T extends object>(qb: QueryBuilder<T>, prop: EntityProperty<T>, tableAlias?: string): Field<T>[];
|
|
50
51
|
/** @internal */
|
|
51
|
-
createQueryBuilder<T extends object>(entityName: EntityName<T> | QueryBuilder<T>, ctx?: Transaction<Knex.Transaction>, preferredConnectionType?: ConnectionType, convertCustomTypes?: boolean): QueryBuilder<T>;
|
|
52
|
+
createQueryBuilder<T extends object>(entityName: EntityName<T> | QueryBuilder<T>, ctx?: Transaction<Knex.Transaction>, preferredConnectionType?: ConnectionType, convertCustomTypes?: boolean, logging?: LoggingOptions): QueryBuilder<T>;
|
|
52
53
|
protected resolveConnectionType(args: {
|
|
53
54
|
ctx?: Transaction<Knex.Transaction>;
|
|
54
55
|
connectionType?: ConnectionType;
|
|
55
56
|
}): ConnectionType;
|
|
56
57
|
protected extractManyToMany<T>(entityName: string, data: EntityDictionary<T>): EntityData<T>;
|
|
57
58
|
protected processManyToMany<T extends object>(meta: EntityMetadata<T> | undefined, pks: Primary<T>[], collections: EntityData<T>, clear: boolean, options?: DriverMethodOptions): Promise<void>;
|
|
58
|
-
protected updateCollectionDiff<T extends object, O extends object>(meta: EntityMetadata<O>, prop: EntityProperty<
|
|
59
|
+
protected updateCollectionDiff<T extends object, O extends object>(meta: EntityMetadata<O>, prop: EntityProperty<O>, pks: Primary<O>[], deleteDiff: Primary<T>[][] | boolean, insertDiff: Primary<T>[][], options?: DriverMethodOptions & {
|
|
59
60
|
ownerSchema?: string;
|
|
60
61
|
}): Promise<void>;
|
|
61
|
-
lockPessimistic<T>(entity: T, options: LockOptions): Promise<void>;
|
|
62
|
+
lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
|
|
62
63
|
protected buildJoinedPropsOrderBy<T extends object>(entityName: string, qb: QueryBuilder<T>, meta: EntityMetadata<T>, populate: PopulateOptions<T>[], parentPath?: string): QueryOrderMap<T>[];
|
|
63
64
|
protected normalizeFields<T extends object>(fields: Field<T>[], prefix?: string): string[];
|
|
64
65
|
protected processField<T extends object>(meta: EntityMetadata<T>, prop: EntityProperty<T> | undefined, field: string, ret: Field<T>[], populate: PopulateOptions<T>[], joinedProps: PopulateOptions<T>[], qb: QueryBuilder<T>): void;
|