@mikro-orm/knex 5.8.3-dev.5 → 5.8.3-dev.6

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>(entityName: string, where: FilterQuery<T>, options?: FindOptions<T, P>): Promise<EntityData<T>[]>;
17
17
  findOne<T extends object, P extends string = never>(entityName: string, where: FilterQuery<T>, options?: FindOneOptions<T, P>): Promise<EntityData<T> | null>;
18
18
  findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, 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>, type: QueryType.COUNT): Promise<number>;
21
- protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>, type: QueryType.SELECT): Promise<T[]>;
22
- protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>): Promise<T[]>;
19
+ countVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: CountOptions<T, any>): Promise<number>;
20
+ private findFromVirtual;
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;
@@ -71,42 +71,38 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
71
71
  return res[0] || null;
72
72
  }
73
73
  async findVirtual(entityName, where, options) {
74
- const meta = this.metadata.get(entityName);
75
- /* istanbul ignore next */
76
- if (!meta.expression) {
77
- return [];
78
- }
79
- if (typeof meta.expression === 'string') {
80
- return this.wrapVirtualExpressionInSubquery(meta, meta.expression, where, options);
81
- }
82
- const em = this.createEntityManager(false);
83
- em.setTransactionContext(options.ctx);
84
- const res = meta.expression(em, where, options);
85
- if (res instanceof query_1.QueryBuilder) {
86
- return this.wrapVirtualExpressionInSubquery(meta, res.getFormattedQuery(), where, options);
87
- }
88
- /* istanbul ignore next */
89
- return res;
74
+ return this.findFromVirtual(entityName, where, options, query_1.QueryType.SELECT);
90
75
  }
91
76
  async countVirtual(entityName, where, options) {
77
+ return this.findFromVirtual(entityName, where, options, query_1.QueryType.COUNT);
78
+ }
79
+ async findFromVirtual(entityName, where, options, type) {
92
80
  const meta = this.metadata.get(entityName);
93
81
  /* istanbul ignore next */
94
82
  if (!meta.expression) {
95
- return 0;
83
+ return type === query_1.QueryType.SELECT ? [] : 0;
96
84
  }
97
85
  if (typeof meta.expression === 'string') {
98
- return this.wrapVirtualExpressionInSubquery(meta, meta.expression, where, options, query_1.QueryType.COUNT);
86
+ return this.wrapVirtualExpressionInSubquery(meta, meta.expression, where, options, type);
99
87
  }
100
88
  const em = this.createEntityManager(false);
101
89
  em.setTransactionContext(options.ctx);
102
90
  const res = meta.expression(em, where, options);
91
+ if (typeof res === 'string') {
92
+ return this.wrapVirtualExpressionInSubquery(meta, res, where, options, type);
93
+ }
103
94
  if (res instanceof query_1.QueryBuilder) {
104
- return this.wrapVirtualExpressionInSubquery(meta, res.getFormattedQuery(), where, options, query_1.QueryType.COUNT);
95
+ return this.wrapVirtualExpressionInSubquery(meta, res.getFormattedQuery(), where, options, type);
96
+ }
97
+ if (core_1.Utils.isObject(res)) {
98
+ const { sql, bindings } = res.toSQL();
99
+ const query = this.platform.formatQuery(sql, bindings);
100
+ return this.wrapVirtualExpressionInSubquery(meta, query, where, options, type);
105
101
  }
106
102
  /* istanbul ignore next */
107
103
  return res;
108
104
  }
109
- async wrapVirtualExpressionInSubquery(meta, expression, where, options, type = query_1.QueryType.SELECT) {
105
+ async wrapVirtualExpressionInSubquery(meta, expression, where, options, type) {
110
106
  const qb = this.createQueryBuilder(meta.className, options?.ctx, options.connectionType, options.convertCustomTypes)
111
107
  .limit(options?.limit, options?.offset)
112
108
  .indexHint(options.indexHint)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "5.8.3-dev.5",
3
+ "version": "5.8.3-dev.6",
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,9 +66,9 @@
66
66
  "@mikro-orm/core": "^5.8.2"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~5.8.3-dev.5",
70
- "@mikro-orm/entity-generator": "~5.8.3-dev.5",
71
- "@mikro-orm/migrations": "~5.8.3-dev.5",
69
+ "@mikro-orm/core": "~5.8.3-dev.6",
70
+ "@mikro-orm/entity-generator": "~5.8.3-dev.6",
71
+ "@mikro-orm/migrations": "~5.8.3-dev.6",
72
72
  "better-sqlite3": "*",
73
73
  "mssql": "*",
74
74
  "mysql": "*",