@mikro-orm/knex 6.0.0-dev.1 → 6.0.0-dev.100
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/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +8 -4
- package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +35 -18
- package/AbstractSqlDriver.d.ts +66 -0
- package/{dist/AbstractSqlDriver.js → AbstractSqlDriver.js} +422 -129
- package/AbstractSqlPlatform.d.ts +22 -0
- package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +46 -19
- package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +1 -0
- package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +3 -0
- package/{dist/README.md → README.md} +66 -109
- package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +6 -10
- package/{dist/SqlEntityManager.js → SqlEntityManager.js} +5 -10
- package/SqlEntityRepository.d.ts +24 -0
- package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +11 -8
- package/{dist/index.d.ts → index.d.ts} +1 -0
- package/{dist/index.js → index.js} +6 -1
- package/index.mjs +208 -0
- package/package.json +20 -49
- package/query/ArrayCriteriaNode.d.ts +9 -0
- package/{dist/query → query}/CriteriaNode.d.ts +8 -8
- package/{dist/query → query}/CriteriaNode.js +23 -13
- package/query/CriteriaNodeFactory.d.ts +12 -0
- package/{dist/query → query}/CriteriaNodeFactory.js +10 -13
- package/{dist/query → query}/ObjectCriteriaNode.d.ts +3 -3
- package/{dist/query → query}/ObjectCriteriaNode.js +26 -9
- package/{dist/query → query}/QueryBuilder.d.ts +90 -34
- package/query/QueryBuilder.js +1098 -0
- package/{dist/query → query}/QueryBuilderHelper.d.ts +23 -19
- package/{dist/query → query}/QueryBuilderHelper.js +300 -176
- package/query/ScalarCriteriaNode.d.ts +10 -0
- package/{dist/query → query}/ScalarCriteriaNode.js +9 -6
- package/{dist/query → query}/enums.js +1 -1
- package/{dist/query → query}/index.js +5 -1
- package/{dist/schema → schema}/DatabaseSchema.d.ts +6 -3
- package/{dist/schema → schema}/DatabaseSchema.js +33 -23
- package/{dist/schema → schema}/DatabaseTable.d.ts +12 -10
- package/{dist/schema → schema}/DatabaseTable.js +55 -29
- package/{dist/schema → schema}/SchemaComparator.d.ts +7 -5
- package/{dist/schema → schema}/SchemaComparator.js +68 -10
- package/{dist/schema → schema}/SchemaHelper.d.ts +20 -13
- package/{dist/schema → schema}/SchemaHelper.js +56 -11
- package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +11 -14
- package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +139 -55
- package/{dist/schema → schema}/index.d.ts +1 -1
- package/{dist/schema → schema}/index.js +6 -2
- package/{dist/typings.d.ts → typings.d.ts} +27 -18
- package/CHANGELOG.md +0 -661
- package/dist/AbstractSqlDriver.d.ts +0 -55
- package/dist/AbstractSqlPlatform.d.ts +0 -21
- package/dist/LICENSE +0 -21
- package/dist/SqlEntityRepository.d.ts +0 -23
- package/dist/index.mjs +0 -26
- package/dist/package.json +0 -99
- package/dist/query/ArrayCriteriaNode.d.ts +0 -9
- package/dist/query/CriteriaNodeFactory.d.ts +0 -12
- package/dist/query/QueryBuilder.js +0 -688
- package/dist/query/ScalarCriteriaNode.d.ts +0 -9
- /package/{dist/query → query}/ArrayCriteriaNode.js +0 -0
- /package/{dist/query → query}/enums.d.ts +0 -0
- /package/{dist/query → query}/index.d.ts +0 -0
- /package/{dist/typings.js → typings.js} +0 -0
|
@@ -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
|
*/
|
|
@@ -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,32 +88,38 @@ 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) {
|
|
118
|
+
const driverOptions = this.config.get('driverOptions');
|
|
119
|
+
if (driverOptions.context?.client instanceof knex_1.knex.Client) {
|
|
120
|
+
this.logger.log('info', 'Reusing knex client provided via `driverOptions`');
|
|
121
|
+
return driverOptions;
|
|
122
|
+
}
|
|
105
123
|
return (0, knex_1.knex)(this.getKnexOptions(type))
|
|
106
124
|
.on('query', data => {
|
|
107
125
|
if (!data.__knexQueryUid) {
|
|
@@ -110,7 +128,7 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
110
128
|
});
|
|
111
129
|
}
|
|
112
130
|
getKnexOptions(type) {
|
|
113
|
-
const config = core_1.Utils.
|
|
131
|
+
const config = core_1.Utils.mergeConfig({
|
|
114
132
|
client: type,
|
|
115
133
|
connection: this.getConnectionOptions(),
|
|
116
134
|
pool: this.config.get('pool'),
|
|
@@ -133,15 +151,15 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
133
151
|
};
|
|
134
152
|
return config;
|
|
135
153
|
}
|
|
136
|
-
getSql(query, formatted) {
|
|
154
|
+
getSql(query, formatted, context) {
|
|
137
155
|
const logger = this.config.getLogger();
|
|
138
|
-
if (!logger.isEnabled('query')) {
|
|
156
|
+
if (!logger.isEnabled('query', context)) {
|
|
139
157
|
return query;
|
|
140
158
|
}
|
|
141
|
-
if (logger.isEnabled('query-params')) {
|
|
159
|
+
if (logger.isEnabled('query-params', context)) {
|
|
142
160
|
return formatted;
|
|
143
161
|
}
|
|
144
|
-
return this.
|
|
162
|
+
return this.getKnex().client.positionBindings(query);
|
|
145
163
|
}
|
|
146
164
|
/**
|
|
147
165
|
* do not call `positionBindings` when there are no bindings - it was messing up with
|
|
@@ -173,4 +191,3 @@ class AbstractSqlConnection extends core_1.Connection {
|
|
|
173
191
|
}
|
|
174
192
|
}
|
|
175
193
|
exports.AbstractSqlConnection = AbstractSqlConnection;
|
|
176
|
-
AbstractSqlConnection.__patched = false;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
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 PopulateOptions, type Primary, type QueryOrderMap, type QueryResult, type Transaction, type UpsertManyOptions, type UpsertOptions, type OrderDefinition } from '@mikro-orm/core';
|
|
3
|
+
import type { AbstractSqlConnection } from './AbstractSqlConnection';
|
|
4
|
+
import type { AbstractSqlPlatform } from './AbstractSqlPlatform';
|
|
5
|
+
import { QueryBuilder, QueryType } from './query';
|
|
6
|
+
import { SqlEntityManager } from './SqlEntityManager';
|
|
7
|
+
import type { Field } from './typings';
|
|
8
|
+
export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection = AbstractSqlConnection, Platform extends AbstractSqlPlatform = AbstractSqlPlatform> extends DatabaseDriver<Connection> {
|
|
9
|
+
[EntityManagerType]: SqlEntityManager<this>;
|
|
10
|
+
protected readonly connection: Connection;
|
|
11
|
+
protected readonly replicas: Connection[];
|
|
12
|
+
protected readonly platform: Platform;
|
|
13
|
+
protected constructor(config: Configuration, platform: Platform, connection: Constructor<Connection>, connector: string[]);
|
|
14
|
+
getPlatform(): Platform;
|
|
15
|
+
createEntityManager<D extends IDatabaseDriver = IDatabaseDriver>(useContext?: boolean): D[typeof EntityManagerType];
|
|
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>): Promise<number>;
|
|
20
|
+
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any, any>, type: QueryType.COUNT): Promise<number>;
|
|
21
|
+
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any, any>, type: QueryType.SELECT): Promise<T[]>;
|
|
22
|
+
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any, any>): Promise<T[]>;
|
|
23
|
+
mapResult<T extends object>(result: EntityData<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[], qb?: QueryBuilder<T>, map?: Dictionary): EntityData<T> | null;
|
|
24
|
+
private mapJoinedProps;
|
|
25
|
+
private appendToCollection;
|
|
26
|
+
count<T extends object>(entityName: string, where: any, options?: CountOptions<T>): Promise<number>;
|
|
27
|
+
nativeInsert<T extends object>(entityName: string, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
28
|
+
nativeInsertMany<T extends object>(entityName: string, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
|
|
29
|
+
nativeUpdate<T extends object>(entityName: string, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T> & UpsertOptions<T>): Promise<QueryResult<T>>;
|
|
30
|
+
nativeUpdateMany<T extends object>(entityName: string, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T> & UpsertManyOptions<T>): Promise<QueryResult<T>>;
|
|
31
|
+
nativeDelete<T extends object>(entityName: string, where: FilterQuery<T> | string | any, options?: DeleteOptions<T>): Promise<QueryResult<T>>;
|
|
32
|
+
syncCollection<T extends object, O extends object>(coll: Collection<T, O>, options?: DriverMethodOptions): Promise<void>;
|
|
33
|
+
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[]>>;
|
|
34
|
+
private getPivotOrderBy;
|
|
35
|
+
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
|
+
* 1:1 owner side needs to be marked for population so QB auto-joins the owner id
|
|
38
|
+
*/
|
|
39
|
+
protected autoJoinOneToOneOwner<T extends object, P extends string = never>(meta: EntityMetadata<T>, populate: PopulateOptions<T>[], fields?: readonly EntityField<T, P>[]): PopulateOptions<T>[];
|
|
40
|
+
protected joinedProps<T>(meta: EntityMetadata, populate: PopulateOptions<T>[]): PopulateOptions<T>[];
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
mergeJoinedResult<T extends object>(rawResults: EntityData<T>[], meta: EntityMetadata<T>, joinedProps: PopulateOptions<T>[]): EntityData<T>[];
|
|
45
|
+
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
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
mapPropToFieldNames<T extends object>(qb: QueryBuilder<T>, prop: EntityProperty<T>, tableAlias?: string): Field<T>[];
|
|
50
|
+
/** @internal */
|
|
51
|
+
createQueryBuilder<T extends object>(entityName: EntityName<T> | QueryBuilder<T>, ctx?: Transaction<Knex.Transaction>, preferredConnectionType?: ConnectionType, convertCustomTypes?: boolean, logging?: LoggingOptions): QueryBuilder<T>;
|
|
52
|
+
protected resolveConnectionType(args: {
|
|
53
|
+
ctx?: Transaction<Knex.Transaction>;
|
|
54
|
+
connectionType?: ConnectionType;
|
|
55
|
+
}): ConnectionType;
|
|
56
|
+
protected extractManyToMany<T>(entityName: string, data: EntityDictionary<T>): EntityData<T>;
|
|
57
|
+
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<O>, pks: Primary<O>[], deleteDiff: Primary<T>[][] | boolean, insertDiff: Primary<T>[][], options?: DriverMethodOptions & {
|
|
59
|
+
ownerSchema?: string;
|
|
60
|
+
}): Promise<void>;
|
|
61
|
+
lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
|
|
62
|
+
protected buildJoinedPropsOrderBy<T extends object>(entityName: string, qb: QueryBuilder<T>, meta: EntityMetadata<T>, populate: PopulateOptions<T>[], parentPath?: string): QueryOrderMap<T>[];
|
|
63
|
+
protected normalizeFields<T extends object>(fields: Field<T>[], prefix?: string): string[];
|
|
64
|
+
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>[];
|
|
66
|
+
}
|