@mikro-orm/knex 6.0.0-dev.313 → 6.0.0-dev.315

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.
@@ -141,7 +141,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
141
141
  }
142
142
  }
143
143
  qb.where(where);
144
- const kqb = qb.getKnexQuery().clear('select');
144
+ const kqb = qb.getKnexQuery(false).clear('select');
145
145
  if (type === query_1.QueryType.COUNT) {
146
146
  kqb.select(this.connection.getKnex().raw('count(*) as count'));
147
147
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.313",
3
+ "version": "6.0.0-dev.315",
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
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -66,6 +66,6 @@
66
66
  "@mikro-orm/core": "^5.0.0"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.0.0-dev.313"
69
+ "@mikro-orm/core": "6.0.0-dev.315"
70
70
  }
71
71
  }
@@ -116,10 +116,10 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
116
116
  andWhere(cond: string, params?: any[]): this;
117
117
  orWhere(cond: QBFilterQuery<T>): this;
118
118
  orWhere(cond: string, params?: any[]): this;
119
- orderBy(orderBy: QBQueryOrderMap<T> | QBQueryOrderMap<T>[]): this;
120
- groupBy(fields: (string | keyof T) | readonly (string | keyof T)[]): this;
121
- having(cond?: QBFilterQuery | string, params?: any[]): this;
122
- onConflict(fields?: Field<T> | Field<T>[]): this;
119
+ orderBy(orderBy: QBQueryOrderMap<T> | QBQueryOrderMap<T>[]): SelectQueryBuilder<T>;
120
+ groupBy(fields: (string | keyof T) | readonly (string | keyof T)[]): SelectQueryBuilder<T>;
121
+ having(cond?: QBFilterQuery | string, params?: any[]): SelectQueryBuilder<T>;
122
+ onConflict(fields?: Field<T> | Field<T>[]): InsertQueryBuilder<T>;
123
123
  ignore(): this;
124
124
  merge(data?: EntityData<T> | Field<T>[]): this;
125
125
  returning(fields?: Field<T> | Field<T>[]): this;
@@ -127,8 +127,8 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
127
127
  * @internal
128
128
  */
129
129
  populate(populate: PopulateOptions<T>[], populateWhere?: ObjectQuery<T> | PopulateHint | `${PopulateHint}`): this;
130
- limit(limit?: number, offset?: number): this;
131
- offset(offset?: number): this;
130
+ limit(limit?: number, offset?: number): SelectQueryBuilder<T>;
131
+ offset(offset?: number): SelectQueryBuilder<T>;
132
132
  withSchema(schema?: string): this;
133
133
  setLockMode(mode?: LockMode, tables?: string[]): this;
134
134
  setFlushMode(flushMode?: FlushMode): this;
@@ -154,9 +154,9 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
154
154
  * Specifies FROM which entity's table select/update/delete will be executed, removing all previously set FROM-s.
155
155
  * Allows setting a main string alias of the selection data.
156
156
  */
157
- from<T extends AnyEntity<T> = AnyEntity>(target: QueryBuilder<T>, aliasName?: string): QueryBuilder<T>;
158
- from<T extends AnyEntity<T> = AnyEntity>(target: EntityName<T>): QueryBuilder<T>;
159
- getKnexQuery(): Knex.QueryBuilder;
157
+ from<T extends AnyEntity<T> = AnyEntity>(target: QueryBuilder<T>, aliasName?: string): SelectQueryBuilder<T>;
158
+ from<T extends AnyEntity<T> = AnyEntity>(target: EntityName<T>): SelectQueryBuilder<T>;
159
+ getKnexQuery(processVirtualEntity?: boolean): Knex.QueryBuilder;
160
160
  /**
161
161
  * Returns the query with parameters as wildcards.
162
162
  */
@@ -225,7 +225,8 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
225
225
  */
226
226
  as(alias: string): Knex.QueryBuilder;
227
227
  clone(reset?: boolean): QueryBuilder<T>;
228
- getKnex(): Knex.QueryBuilder;
228
+ getKnex(processVirtualEntity?: boolean): Knex.QueryBuilder;
229
+ private fromVirtual;
229
230
  private joinReference;
230
231
  private prepareFields;
231
232
  private init;
@@ -456,9 +456,9 @@ class QueryBuilder {
456
456
  }
457
457
  return this;
458
458
  }
459
- getKnexQuery() {
459
+ getKnexQuery(processVirtualEntity = true) {
460
460
  this.finalize();
461
- const qb = this.getQueryBase();
461
+ const qb = this.getQueryBase(processVirtualEntity);
462
462
  const type = this.type ?? enums_1.QueryType.SELECT;
463
463
  qb.__raw = true; // tag it as there is now way to check via `instanceof`
464
464
  core_1.Utils.runIfNotEmpty(() => this.helper.appendQueryCondition(type, this._cond, qb), this._cond && !this._onConflict);
@@ -738,9 +738,9 @@ class QueryBuilder {
738
738
  qb.finalized = false;
739
739
  return qb;
740
740
  }
741
- getKnex() {
741
+ getKnex(processVirtualEntity = true) {
742
742
  const qb = this.knex.queryBuilder();
743
- const { subQuery, aliasName, entityName } = this.mainAlias;
743
+ const { subQuery, aliasName, entityName, metadata } = this.mainAlias;
744
744
  const ref = subQuery ? subQuery : this.knex.ref(this.helper.getTableName(entityName));
745
745
  if (this.finalized && (this._explicitAlias || this.helper.isTableNameAliasRequired(this.type))) {
746
746
  ref.as(aliasName);
@@ -749,12 +749,36 @@ class QueryBuilder {
749
749
  if (schema) {
750
750
  ref.withSchema(schema);
751
751
  }
752
- qb.from(ref);
752
+ if (metadata?.virtual && processVirtualEntity) {
753
+ qb.fromRaw(this.fromVirtual(metadata));
754
+ }
755
+ else {
756
+ qb.from(ref);
757
+ }
753
758
  if (this.context) {
754
759
  qb.transacting(this.context);
755
760
  }
756
761
  return qb;
757
762
  }
763
+ fromVirtual(meta) {
764
+ if (typeof meta.expression === 'string') {
765
+ return `(${meta.expression}) as ${this.platform.quoteIdentifier(this.alias)}`;
766
+ }
767
+ const res = meta.expression(this.em, this._cond, {});
768
+ if (typeof res === 'string') {
769
+ return `(${res}) as ${this.platform.quoteIdentifier(this.alias)}`;
770
+ }
771
+ if (res instanceof QueryBuilder) {
772
+ return `(${res.getFormattedQuery()}) as ${this.platform.quoteIdentifier(this.alias)}`;
773
+ }
774
+ if (core_1.Utils.isObject(res)) {
775
+ const { sql, bindings } = res.toSQL();
776
+ const query = this.platform.formatQuery(sql, bindings);
777
+ return `(${query}) as ${this.platform.quoteIdentifier(this.alias)}`;
778
+ }
779
+ /* istanbul ignore next */
780
+ return res;
781
+ }
758
782
  joinReference(field, alias, cond, type, path, schema, subquery) {
759
783
  this.ensureNotFinalized();
760
784
  if (typeof field === 'object') {
@@ -923,8 +947,8 @@ class QueryBuilder {
923
947
  }
924
948
  return this;
925
949
  }
926
- getQueryBase() {
927
- const qb = this.getKnex();
950
+ getQueryBase(processVirtualEntity) {
951
+ const qb = this.getKnex(processVirtualEntity);
928
952
  const schema = this.getSchema(this.mainAlias);
929
953
  // Joined tables doesn't need to belong to the same schema as the main table
930
954
  const joinSchema = this._schema ?? this.em?.schema ?? schema;