@mikro-orm/knex 6.0.0-dev.81 → 6.0.0-dev.82

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.
@@ -643,17 +643,18 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
643
643
  * @internal
644
644
  */
645
645
  mapPropToFieldNames(qb, prop, tableAlias) {
646
- const aliased = qb.ref(tableAlias ? `${tableAlias}__${prop.fieldNames[0]}` : prop.fieldNames[0]).toString();
646
+ const knex = this.connection.getKnex();
647
+ const aliased = knex.ref(tableAlias ? `${tableAlias}__${prop.fieldNames[0]}` : prop.fieldNames[0]).toString();
647
648
  if (prop.customType?.convertToJSValueSQL && tableAlias) {
648
- const prefixed = qb.ref(prop.fieldNames[0]).withSchema(tableAlias).toString();
649
+ const prefixed = knex.ref(prop.fieldNames[0]).withSchema(tableAlias).toString();
649
650
  return [(0, core_1.raw)(`${prop.customType.convertToJSValueSQL(prefixed, this.platform)} as ${aliased}`)];
650
651
  }
651
652
  if (prop.formula) {
652
- const alias = qb.ref(tableAlias ?? qb.alias).toString();
653
+ const alias = knex.ref(tableAlias ?? qb.alias).toString();
653
654
  return [(0, core_1.raw)(`${prop.formula(alias)} as ${aliased}`)];
654
655
  }
655
656
  if (tableAlias) {
656
- return prop.fieldNames.map(fieldName => qb.ref(fieldName).withSchema(tableAlias).as(`${tableAlias}__${fieldName}`));
657
+ return prop.fieldNames.map(fieldName => knex.ref(fieldName).withSchema(tableAlias).as(`${tableAlias}__${fieldName}`));
657
658
  }
658
659
  return prop.fieldNames;
659
660
  }
@@ -853,8 +854,8 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
853
854
  meta.props
854
855
  .filter(prop => prop.formula && !lazyProps.includes(prop))
855
856
  .forEach(prop => {
856
- const alias = qb.ref(qb.alias).toString();
857
- const aliased = qb.ref(prop.fieldNames[0]).toString();
857
+ const alias = this.connection.getKnex().ref(qb.alias).toString();
858
+ const aliased = this.connection.getKnex().ref(prop.fieldNames[0]).toString();
858
859
  ret.push((0, core_1.raw)(`${prop.formula(alias)} as ${aliased}`));
859
860
  });
860
861
  meta.props
@@ -48,9 +48,15 @@ class AbstractSqlPlatform extends core_1.Platform {
48
48
  let j = 0;
49
49
  let pos = 0;
50
50
  let ret = '';
51
- if (sql[0] === '?' && sql[1] !== '?') {
52
- ret += this.quoteValue(params[j++]);
53
- pos = 1;
51
+ if (sql[0] === '?') {
52
+ if (sql[1] === '?') {
53
+ ret += this.quoteIdentifier(params[j++]);
54
+ pos = 2;
55
+ }
56
+ else {
57
+ ret += this.quoteValue(params[j++]);
58
+ pos = 1;
59
+ }
54
60
  }
55
61
  while (pos < sql.length) {
56
62
  const idx = sql.indexOf('?', pos + 1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.81",
3
+ "version": "6.0.0-dev.82",
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.7.10"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~6.0.0-dev.81"
69
+ "@mikro-orm/core": "~6.0.0-dev.82"
70
70
  }
71
71
  }
@@ -115,12 +115,6 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
115
115
  * @internal
116
116
  */
117
117
  populate(populate: PopulateOptions<T>[], populateWhere?: ObjectQuery<T> | PopulateHint): this;
118
- /**
119
- * @internal
120
- */
121
- ref(field: string): Knex.Ref<string, {
122
- [x: string]: string;
123
- }>;
124
118
  limit(limit?: number, offset?: number): this;
125
119
  offset(offset?: number): this;
126
120
  withSchema(schema?: string): this;
@@ -299,12 +299,6 @@ class QueryBuilder {
299
299
  this._populateWhere = populateWhere;
300
300
  return this;
301
301
  }
302
- /**
303
- * @internal
304
- */
305
- ref(field) {
306
- return this.knex.ref(field);
307
- }
308
302
  limit(limit, offset = 0) {
309
303
  this.ensureNotFinalized();
310
304
  this._limit = limit;
@@ -947,7 +941,7 @@ class QueryBuilder {
947
941
  if (!prop?.persist && !prop?.formula) {
948
942
  addToSelect.push(fieldName);
949
943
  }
950
- orderBy.push({ [(0, core_1.raw)(`min(${this.ref(fieldName)}${type})`)]: direction });
944
+ orderBy.push({ [(0, core_1.raw)(`min(${this.knex.ref(fieldName)}${type})`)]: direction });
951
945
  }
952
946
  }
953
947
  subQuery.orderBy(orderBy);
@@ -344,6 +344,9 @@ class QueryBuilderHelper {
344
344
  }
345
345
  appendQuerySubCondition(qb, type, method, cond, key, operator) {
346
346
  const m = operator === '$or' ? 'orWhere' : method;
347
+ if (cond[key] instanceof core_1.RawQueryFragment) {
348
+ cond[key] = this.knex.raw(cond[key].sql, cond[key].params);
349
+ }
347
350
  if (this.isSimpleRegExp(cond[key])) {
348
351
  return void qb[m](this.mapper(key, type), 'like', this.getRegExpParam(cond[key]));
349
352
  }
@@ -447,9 +450,12 @@ class QueryBuilderHelper {
447
450
  /* istanbul ignore next */
448
451
  const rawColumn = core_1.Utils.isString(column) ? column.split('.').map(e => this.knex.ref(e)).join('.') : column;
449
452
  const customOrder = prop?.customOrder;
450
- const colPart = customOrder
453
+ let colPart = customOrder
451
454
  ? this.platform.generateCustomOrder(rawColumn, customOrder)
452
455
  : rawColumn;
456
+ if (core_1.Utils.isRawSql(colPart)) {
457
+ colPart = this.platform.formatQuery(colPart.sql, colPart.params);
458
+ }
453
459
  ret.push(`${colPart} ${order.toLowerCase()}`);
454
460
  });
455
461
  });
package/typings.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Knex } from 'knex';
2
- import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, QBFilterQuery, QueryOrderMap, Type } from '@mikro-orm/core';
2
+ import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, RawQueryFragment, QBFilterQuery, QueryOrderMap, Type } from '@mikro-orm/core';
3
3
  import type { QueryType } from './query/enums';
4
4
  import type { DatabaseSchema, DatabaseTable } from './schema';
5
5
  export interface Table {
@@ -11,7 +11,7 @@ export type KnexStringRef = Knex.Ref<string, {
11
11
  [alias: string]: string;
12
12
  }>;
13
13
  type AnyString = string & {};
14
- export type Field<T> = AnyString | keyof T | KnexStringRef | Knex.QueryBuilder;
14
+ export type Field<T> = AnyString | keyof T | RawQueryFragment | KnexStringRef | Knex.QueryBuilder;
15
15
  export interface JoinOptions {
16
16
  table: string;
17
17
  schema?: string;