@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.
Files changed (60) hide show
  1. package/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +8 -4
  2. package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +35 -18
  3. package/AbstractSqlDriver.d.ts +66 -0
  4. package/{dist/AbstractSqlDriver.js → AbstractSqlDriver.js} +422 -129
  5. package/AbstractSqlPlatform.d.ts +22 -0
  6. package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +46 -19
  7. package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +1 -0
  8. package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +3 -0
  9. package/{dist/README.md → README.md} +66 -109
  10. package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +6 -10
  11. package/{dist/SqlEntityManager.js → SqlEntityManager.js} +5 -10
  12. package/SqlEntityRepository.d.ts +24 -0
  13. package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +11 -8
  14. package/{dist/index.d.ts → index.d.ts} +1 -0
  15. package/{dist/index.js → index.js} +6 -1
  16. package/index.mjs +208 -0
  17. package/package.json +20 -49
  18. package/query/ArrayCriteriaNode.d.ts +9 -0
  19. package/{dist/query → query}/CriteriaNode.d.ts +8 -8
  20. package/{dist/query → query}/CriteriaNode.js +23 -13
  21. package/query/CriteriaNodeFactory.d.ts +12 -0
  22. package/{dist/query → query}/CriteriaNodeFactory.js +10 -13
  23. package/{dist/query → query}/ObjectCriteriaNode.d.ts +3 -3
  24. package/{dist/query → query}/ObjectCriteriaNode.js +26 -9
  25. package/{dist/query → query}/QueryBuilder.d.ts +90 -34
  26. package/query/QueryBuilder.js +1098 -0
  27. package/{dist/query → query}/QueryBuilderHelper.d.ts +23 -19
  28. package/{dist/query → query}/QueryBuilderHelper.js +300 -176
  29. package/query/ScalarCriteriaNode.d.ts +10 -0
  30. package/{dist/query → query}/ScalarCriteriaNode.js +9 -6
  31. package/{dist/query → query}/enums.js +1 -1
  32. package/{dist/query → query}/index.js +5 -1
  33. package/{dist/schema → schema}/DatabaseSchema.d.ts +6 -3
  34. package/{dist/schema → schema}/DatabaseSchema.js +33 -23
  35. package/{dist/schema → schema}/DatabaseTable.d.ts +12 -10
  36. package/{dist/schema → schema}/DatabaseTable.js +55 -29
  37. package/{dist/schema → schema}/SchemaComparator.d.ts +7 -5
  38. package/{dist/schema → schema}/SchemaComparator.js +68 -10
  39. package/{dist/schema → schema}/SchemaHelper.d.ts +20 -13
  40. package/{dist/schema → schema}/SchemaHelper.js +56 -11
  41. package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +11 -14
  42. package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +139 -55
  43. package/{dist/schema → schema}/index.d.ts +1 -1
  44. package/{dist/schema → schema}/index.js +6 -2
  45. package/{dist/typings.d.ts → typings.d.ts} +27 -18
  46. package/CHANGELOG.md +0 -661
  47. package/dist/AbstractSqlDriver.d.ts +0 -55
  48. package/dist/AbstractSqlPlatform.d.ts +0 -21
  49. package/dist/LICENSE +0 -21
  50. package/dist/SqlEntityRepository.d.ts +0 -23
  51. package/dist/index.mjs +0 -26
  52. package/dist/package.json +0 -99
  53. package/dist/query/ArrayCriteriaNode.d.ts +0 -9
  54. package/dist/query/CriteriaNodeFactory.d.ts +0 -12
  55. package/dist/query/QueryBuilder.js +0 -688
  56. package/dist/query/ScalarCriteriaNode.d.ts +0 -9
  57. /package/{dist/query → query}/ArrayCriteriaNode.js +0 -0
  58. /package/{dist/query → query}/enums.d.ts +0 -0
  59. /package/{dist/query → query}/index.d.ts +0 -0
  60. /package/{dist/typings.js → typings.js} +0 -0
@@ -1,10 +1,13 @@
1
+ /// <reference types="node" />
2
+ import { inspect } from 'util';
1
3
  import type { Knex } from 'knex';
2
- import type { AnyEntity, Dictionary, EntityData, EntityProperty, RequiredEntityData, GroupOperator, MetadataStorage, PopulateOptions, QBFilterQuery, QueryOrderMap, QueryResult, FlushMode } from '@mikro-orm/core';
3
- import { LockMode, QueryFlag } from '@mikro-orm/core';
4
+ import { LockMode, PopulateHint, QueryFlag, type AnyEntity, type ConnectionType, type Dictionary, type EntityData, type EntityName, type EntityProperty, type FlushMode, type GroupOperator, type MetadataStorage, type ObjectQuery, type PopulateOptions, type QBFilterQuery, type QBQueryOrderMap, type QueryResult, type RequiredEntityData, type LoggingOptions } from '@mikro-orm/core';
4
5
  import { QueryType } from './enums';
5
6
  import type { AbstractSqlDriver } from '../AbstractSqlDriver';
7
+ import { QueryBuilderHelper, type Alias } from './QueryBuilderHelper';
6
8
  import type { SqlEntityManager } from '../SqlEntityManager';
7
9
  import type { Field } from '../typings';
10
+ import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
8
11
  /**
9
12
  * SQL query builder with fluent interface.
10
13
  *
@@ -24,63 +27,77 @@ import type { Field } from '../typings';
24
27
  * const publisher = await qb.getSingleResult();
25
28
  * ```
26
29
  */
27
- export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
28
- private readonly entityName;
30
+ export declare class QueryBuilder<T extends object = AnyEntity> {
31
+ #private;
29
32
  private readonly metadata;
30
33
  private readonly driver;
31
34
  private readonly context?;
32
35
  private connectionType?;
33
36
  private readonly em?;
34
- readonly alias: string;
37
+ private readonly logging?;
38
+ get mainAlias(): Alias<T>;
39
+ get alias(): string;
40
+ get helper(): QueryBuilderHelper;
35
41
  /** @internal */
36
- type: QueryType;
42
+ type?: QueryType;
37
43
  /** @internal */
38
44
  _fields?: Field<T>[];
39
45
  /** @internal */
40
46
  _populate: PopulateOptions<T>[];
41
47
  /** @internal */
48
+ _populateWhere?: ObjectQuery<T> | PopulateHint;
49
+ /** @internal */
42
50
  _populateMap: Dictionary<string>;
43
51
  private aliasCounter;
44
52
  private flags;
45
53
  private finalized;
46
54
  private _joins;
47
- private _aliasMap;
55
+ private _explicitAlias;
48
56
  private _schema?;
49
57
  private _cond;
50
58
  private _data;
51
59
  private _orderBy;
52
60
  private _groupBy;
53
61
  private _having;
62
+ private _returning?;
54
63
  private _onConflict?;
55
64
  private _limit?;
56
65
  private _offset?;
66
+ private _distinctOn?;
57
67
  private _joinedProps;
58
68
  private _cache?;
59
69
  private _indexHint?;
70
+ private _comments;
71
+ private _hintComments;
60
72
  private flushMode?;
61
73
  private lockMode?;
62
74
  private lockTables?;
63
75
  private subQueries;
64
- private innerPromise?;
76
+ private _mainAlias?;
77
+ private _aliases;
78
+ private _helper?;
65
79
  private readonly platform;
66
80
  private readonly knex;
67
- private readonly helper;
68
81
  /**
69
82
  * @internal
70
83
  */
71
- constructor(entityName: string, metadata: MetadataStorage, driver: AbstractSqlDriver, context?: Knex.Transaction<any, any[]> | undefined, alias?: string, connectionType?: "write" | "read" | undefined, em?: SqlEntityManager<AbstractSqlDriver<import("..").AbstractSqlConnection>> | undefined);
84
+ constructor(entityName: EntityName<T> | QueryBuilder<T>, metadata: MetadataStorage, driver: AbstractSqlDriver, context?: Knex.Transaction<any, any[]> | undefined, alias?: string, connectionType?: ConnectionType | undefined, em?: SqlEntityManager<AbstractSqlDriver<import("..").AbstractSqlConnection, AbstractSqlPlatform>> | undefined, logging?: LoggingOptions | undefined);
72
85
  select(fields: Field<T> | Field<T>[], distinct?: boolean): SelectQueryBuilder<T>;
73
86
  addSelect(fields: Field<T> | Field<T>[]): SelectQueryBuilder<T>;
87
+ distinct(): SelectQueryBuilder<T>;
88
+ /** postgres only */
89
+ distinctOn(fields: string | string[]): SelectQueryBuilder<T>;
74
90
  insert(data: RequiredEntityData<T> | RequiredEntityData<T>[]): InsertQueryBuilder<T>;
75
91
  update(data: EntityData<T>): UpdateQueryBuilder<T>;
76
92
  delete(cond?: QBFilterQuery): DeleteQueryBuilder<T>;
77
93
  truncate(): TruncateQueryBuilder<T>;
78
94
  count(field?: string | string[], distinct?: boolean): CountQueryBuilder<T>;
79
- join(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): this;
80
- leftJoin(field: string, alias: string, cond?: QBFilterQuery): this;
81
- joinAndSelect(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): this;
82
- leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this;
83
- protected getFieldsForJoinedLoad<U extends AnyEntity<U>>(prop: EntityProperty<U>, alias: string): Field<U>[];
95
+ join(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string, schema?: string): this;
96
+ leftJoin(field: string, alias: string, cond?: QBFilterQuery, schema?: string): this;
97
+ joinAndSelect(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string, fields?: string[], schema?: string): SelectQueryBuilder<T>;
98
+ leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<T>;
99
+ innerJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<T>;
100
+ protected getFieldsForJoinedLoad(prop: EntityProperty<T>, alias: string, explicitFields?: string[]): Field<T>[];
84
101
  withSubQuery(subQuery: Knex.QueryBuilder, alias: string): this;
85
102
  where(cond: QBFilterQuery<T>, operator?: keyof typeof GroupOperator): this;
86
103
  where(cond: string, params?: any[], operator?: keyof typeof GroupOperator): this;
@@ -88,23 +105,17 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
88
105
  andWhere(cond: string, params?: any[]): this;
89
106
  orWhere(cond: QBFilterQuery<T>): this;
90
107
  orWhere(cond: string, params?: any[]): this;
91
- orderBy(orderBy: QueryOrderMap<unknown> | QueryOrderMap<unknown>[]): this;
108
+ orderBy(orderBy: QBQueryOrderMap<T> | QBQueryOrderMap<T>[]): this;
92
109
  groupBy(fields: (string | keyof T) | readonly (string | keyof T)[]): this;
93
110
  having(cond?: QBFilterQuery | string, params?: any[]): this;
94
- onConflict(fields?: string | string[]): this;
111
+ onConflict(fields?: Field<T> | Field<T>[]): this;
95
112
  ignore(): this;
96
113
  merge(data?: EntityData<T> | Field<T>[]): this;
114
+ returning(fields?: Field<T> | Field<T>[]): this;
97
115
  /**
98
116
  * @internal
99
117
  */
100
- populate(populate: PopulateOptions<T>[]): this;
101
- /**
102
- * @internal
103
- */
104
- ref(field: string): Knex.Ref<string, {
105
- [x: string]: string;
106
- }>;
107
- raw<R = Knex.Raw>(sql: string, bindings?: Knex.RawBinding[] | Knex.ValueDict): R;
118
+ populate(populate: PopulateOptions<T>[], populateWhere?: ObjectQuery<T> | PopulateHint): this;
108
119
  limit(limit?: number, offset?: number): this;
109
120
  offset(offset?: number): this;
110
121
  withSchema(schema?: string): this;
@@ -117,11 +128,32 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
117
128
  * Adds index hint to the FROM clause.
118
129
  */
119
130
  indexHint(sql: string): this;
131
+ /**
132
+ * Prepend comment to the sql query using the syntax `/* ... *&#8205;/`. Some characters are forbidden such as `/*, *&#8205;/` and `?`.
133
+ */
134
+ comment(comment: string | string[]): this;
135
+ /**
136
+ * Add hints to the query using comment-like syntax `/*+ ... *&#8205;/`. MySQL and Oracle use this syntax for optimizer hints.
137
+ * Also various DB proxies and routers use this syntax to pass hints to alter their behavior. In other dialects the hints
138
+ * are ignored as simple comments.
139
+ */
140
+ hintComment(comment: string | string[]): this;
141
+ /**
142
+ * Specifies FROM which entity's table select/update/delete will be executed, removing all previously set FROM-s.
143
+ * Allows setting a main string alias of the selection data.
144
+ */
145
+ from<T extends AnyEntity<T> = AnyEntity>(target: QueryBuilder<T>, aliasName?: string): QueryBuilder<T>;
146
+ from<T extends AnyEntity<T> = AnyEntity>(target: EntityName<T>): QueryBuilder<T>;
120
147
  getKnexQuery(): Knex.QueryBuilder;
121
148
  /**
122
149
  * Returns the query with parameters as wildcards.
123
150
  */
124
151
  getQuery(): string;
152
+ toQuery(): {
153
+ sql: string;
154
+ _sql: Knex.Sql;
155
+ params: readonly unknown[];
156
+ };
125
157
  /**
126
158
  * Returns the list of all parameters for this query.
127
159
  */
@@ -130,8 +162,18 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
130
162
  * Returns raw interpolated query string with all the parameters inlined.
131
163
  */
132
164
  getFormattedQuery(): string;
165
+ /**
166
+ * @internal
167
+ */
133
168
  getAliasForJoinPath(path?: string): string | undefined;
169
+ /**
170
+ * @internal
171
+ */
134
172
  getNextAlias(entityName?: string): string;
173
+ /**
174
+ * @internal
175
+ */
176
+ getAliasMap(): Dictionary<string>;
135
177
  /**
136
178
  * Executes this QB and returns the raw results, mapped to the property names (unless disabled via last parameter).
137
179
  * Use `method` to specify what kind of result you want to get (array/single/meta).
@@ -153,11 +195,14 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
153
195
  * Executes count query (without offset and limit), returning total count of results
154
196
  */
155
197
  getCount(field?: string | string[], distinct?: boolean): Promise<number>;
198
+ /**
199
+ * Executes the query, returning both array of results and total count query (without offset and limit).
200
+ */
201
+ getResultAndCount(): Promise<[T[], number]>;
156
202
  /**
157
203
  * Provides promise-like interface so we can await the QB instance.
158
204
  */
159
205
  then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<T[] | number | QueryResult<T>>;
160
- private getInnerPromise;
161
206
  /**
162
207
  * Returns knex instance with sub-query aliased with given alias.
163
208
  * You can provide `EntityName.propName` as alias, then the field name will be used based on the metadata
@@ -169,24 +214,35 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
169
214
  private prepareFields;
170
215
  private init;
171
216
  private getQueryBase;
217
+ private applyDiscriminatorCondition;
172
218
  private finalize;
219
+ private processPopulateWhere;
220
+ private hasToManyJoins;
173
221
  private wrapPaginateSubQuery;
174
222
  private wrapModifySubQuery;
175
- private autoJoinPivotTable;
223
+ private getSchema;
224
+ private createAlias;
225
+ private createMainAlias;
226
+ private fromSubQuery;
227
+ private fromEntityName;
228
+ private createQueryBuilderHelper;
229
+ private ensureFromClause;
230
+ private ensureNotFinalized;
231
+ [inspect.custom](depth: number): string;
176
232
  }
177
- export interface RunQueryBuilder<T> extends Omit<QueryBuilder<T>, 'getResult' | 'getSingleResult' | 'getResultList' | 'where'> {
233
+ export interface RunQueryBuilder<T extends object> extends Omit<QueryBuilder<T>, 'getResult' | 'getSingleResult' | 'getResultList' | 'where'> {
178
234
  where(cond: QBFilterQuery<T> | string, params?: keyof typeof GroupOperator | any[], operator?: keyof typeof GroupOperator): this;
179
235
  execute<U = QueryResult<T>>(method?: 'all' | 'get' | 'run', mapResults?: boolean): Promise<U>;
180
236
  then<TResult1 = QueryResult<T>, TResult2 = never>(onfulfilled?: ((value: QueryResult<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<QueryResult<T>>;
181
237
  }
182
- export interface SelectQueryBuilder<T> extends QueryBuilder<T> {
238
+ export interface SelectQueryBuilder<T extends object> extends QueryBuilder<T> {
183
239
  execute<U = T[]>(method?: 'all' | 'get' | 'run', mapResults?: boolean): Promise<U>;
184
240
  execute<U = T[]>(method: 'all', mapResults?: boolean): Promise<U>;
185
241
  execute<U = T>(method: 'get', mapResults?: boolean): Promise<U>;
186
242
  execute<U = QueryResult<T>>(method: 'run', mapResults?: boolean): Promise<U>;
187
243
  then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((value: T[]) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<T[]>;
188
244
  }
189
- export interface CountQueryBuilder<T> extends QueryBuilder<T> {
245
+ export interface CountQueryBuilder<T extends object> extends QueryBuilder<T> {
190
246
  execute<U = {
191
247
  count: number;
192
248
  }[]>(method?: 'all' | 'get' | 'run', mapResults?: boolean): Promise<U>;
@@ -201,11 +257,11 @@ export interface CountQueryBuilder<T> extends QueryBuilder<T> {
201
257
  }>>(method: 'run', mapResults?: boolean): Promise<U>;
202
258
  then<TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<number>;
203
259
  }
204
- export interface InsertQueryBuilder<T> extends RunQueryBuilder<T> {
260
+ export interface InsertQueryBuilder<T extends object> extends RunQueryBuilder<T> {
205
261
  }
206
- export interface UpdateQueryBuilder<T> extends RunQueryBuilder<T> {
262
+ export interface UpdateQueryBuilder<T extends object> extends RunQueryBuilder<T> {
207
263
  }
208
- export interface DeleteQueryBuilder<T> extends RunQueryBuilder<T> {
264
+ export interface DeleteQueryBuilder<T extends object> extends RunQueryBuilder<T> {
209
265
  }
210
- export interface TruncateQueryBuilder<T> extends RunQueryBuilder<T> {
266
+ export interface TruncateQueryBuilder<T extends object> extends RunQueryBuilder<T> {
211
267
  }