@mikro-orm/knex 6.0.0-dev.102 → 6.0.0-dev.103

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.
@@ -16,10 +16,9 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
16
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
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
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[]>;
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>;
23
22
  mapResult<T extends object>(result: EntityData<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[], qb?: QueryBuilder<T>, map?: Dictionary): EntityData<T> | null;
24
23
  private mapJoinedProps;
25
24
  private appendToCollection;
@@ -86,42 +86,38 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
86
86
  return res[0] || null;
87
87
  }
88
88
  async findVirtual(entityName, where, options) {
89
- const meta = this.metadata.get(entityName);
90
- /* istanbul ignore next */
91
- if (!meta.expression) {
92
- return [];
93
- }
94
- if (typeof meta.expression === 'string') {
95
- return this.wrapVirtualExpressionInSubquery(meta, meta.expression, where, options);
96
- }
97
- const em = this.createEntityManager(false);
98
- em.setTransactionContext(options.ctx);
99
- const res = meta.expression(em, where, options);
100
- if (res instanceof query_1.QueryBuilder) {
101
- return this.wrapVirtualExpressionInSubquery(meta, res.getFormattedQuery(), where, options);
102
- }
103
- /* istanbul ignore next */
104
- return res;
89
+ return this.findFromVirtual(entityName, where, options, query_1.QueryType.SELECT);
105
90
  }
106
91
  async countVirtual(entityName, where, options) {
92
+ return this.findFromVirtual(entityName, where, options, query_1.QueryType.COUNT);
93
+ }
94
+ async findFromVirtual(entityName, where, options, type) {
107
95
  const meta = this.metadata.get(entityName);
108
96
  /* istanbul ignore next */
109
97
  if (!meta.expression) {
110
- return 0;
98
+ return type === query_1.QueryType.SELECT ? [] : 0;
111
99
  }
112
100
  if (typeof meta.expression === 'string') {
113
- return this.wrapVirtualExpressionInSubquery(meta, meta.expression, where, options, query_1.QueryType.COUNT);
101
+ return this.wrapVirtualExpressionInSubquery(meta, meta.expression, where, options, type);
114
102
  }
115
103
  const em = this.createEntityManager(false);
116
104
  em.setTransactionContext(options.ctx);
117
105
  const res = meta.expression(em, where, options);
106
+ if (typeof res === 'string') {
107
+ return this.wrapVirtualExpressionInSubquery(meta, res, where, options, type);
108
+ }
118
109
  if (res instanceof query_1.QueryBuilder) {
119
- return this.wrapVirtualExpressionInSubquery(meta, res.getFormattedQuery(), where, options, query_1.QueryType.COUNT);
110
+ return this.wrapVirtualExpressionInSubquery(meta, res.getFormattedQuery(), where, options, type);
111
+ }
112
+ if (core_1.Utils.isObject(res)) {
113
+ const { sql, bindings } = res.toSQL();
114
+ const query = this.platform.formatQuery(sql, bindings);
115
+ return this.wrapVirtualExpressionInSubquery(meta, query, where, options, type);
120
116
  }
121
117
  /* istanbul ignore next */
122
118
  return res;
123
119
  }
124
- async wrapVirtualExpressionInSubquery(meta, expression, where, options, type = query_1.QueryType.SELECT) {
120
+ async wrapVirtualExpressionInSubquery(meta, expression, where, options, type) {
125
121
  const qb = this.createQueryBuilder(meta.className, options?.ctx, options.connectionType, options.convertCustomTypes)
126
122
  .limit(options?.limit, options?.offset)
127
123
  .indexHint(options.indexHint)
@@ -174,10 +170,12 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
174
170
  return root[`${relationAlias}__${name}`] != null;
175
171
  }));
176
172
  if (!hasPK) {
177
- // initialize empty collections
178
173
  if ([core_1.ReferenceKind.MANY_TO_MANY, core_1.ReferenceKind.ONE_TO_MANY].includes(relation.kind)) {
179
174
  result[relation.name] ??= [];
180
175
  }
176
+ if ([core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(relation.kind)) {
177
+ result[relation.name] = null;
178
+ }
181
179
  return;
182
180
  }
183
181
  const tz = this.platform.getTimezone();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.102",
3
+ "version": "6.0.0-dev.103",
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",
@@ -63,9 +63,9 @@
63
63
  "sqlstring": "2.3.3"
64
64
  },
65
65
  "devDependencies": {
66
- "@mikro-orm/core": "^5.8.1"
66
+ "@mikro-orm/core": "^5.8.3"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~6.0.0-dev.102"
69
+ "@mikro-orm/core": "~6.0.0-dev.103"
70
70
  }
71
71
  }
@@ -734,7 +734,8 @@ class QueryBuilder {
734
734
  return;
735
735
  }
736
736
  if (prop?.embedded) {
737
- const fieldName = this.helper.mapper(prop.fieldNames[0], this.type);
737
+ const name = this._aliases[a] ? `${a}.${prop.fieldNames[0]}` : prop.fieldNames[0];
738
+ const fieldName = this.helper.mapper(name, this.type);
738
739
  ret.push(fieldName);
739
740
  return;
740
741
  }