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