@mikro-orm/knex 6.0.0-dev.80 → 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.
@@ -171,6 +171,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
171
171
  }
172
172
  return;
173
173
  }
174
+ const tz = this.platform.getTimezone();
174
175
  meta2.props
175
176
  .filter(prop => this.platform.shouldHaveColumn(prop, p.children || []))
176
177
  .forEach(prop => {
@@ -178,6 +179,11 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
178
179
  relationPojo[prop.name] = prop.fieldNames.map(name => root[`${relationAlias}__${name}`]);
179
180
  prop.fieldNames.map(name => delete root[`${relationAlias}__${name}`]);
180
181
  }
182
+ else if (prop.runtimeType === 'Date') {
183
+ const alias = `${relationAlias}__${prop.fieldNames[0]}`;
184
+ relationPojo[prop.name] = (typeof root[alias] === 'string' ? new Date(root[alias]) : root[alias]);
185
+ delete root[alias];
186
+ }
181
187
  else {
182
188
  const alias = `${relationAlias}__${prop.fieldNames[0]}`;
183
189
  relationPojo[prop.name] = root[alias];
@@ -415,7 +421,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
415
421
  const versionProperty = meta.properties[meta.versionProperty];
416
422
  const quotedFieldName = this.platform.quoteIdentifier(versionProperty.fieldNames[0]);
417
423
  sql += `${quotedFieldName} = `;
418
- if (versionProperty.type.toLowerCase() === 'date') {
424
+ if (versionProperty.runtimeType === 'Date') {
419
425
  sql += this.platform.getCurrentTimestampSQL(versionProperty.length);
420
426
  }
421
427
  else {
@@ -637,17 +643,18 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
637
643
  * @internal
638
644
  */
639
645
  mapPropToFieldNames(qb, prop, tableAlias) {
640
- 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();
641
648
  if (prop.customType?.convertToJSValueSQL && tableAlias) {
642
- const prefixed = qb.ref(prop.fieldNames[0]).withSchema(tableAlias).toString();
649
+ const prefixed = knex.ref(prop.fieldNames[0]).withSchema(tableAlias).toString();
643
650
  return [(0, core_1.raw)(`${prop.customType.convertToJSValueSQL(prefixed, this.platform)} as ${aliased}`)];
644
651
  }
645
652
  if (prop.formula) {
646
- const alias = qb.ref(tableAlias ?? qb.alias).toString();
653
+ const alias = knex.ref(tableAlias ?? qb.alias).toString();
647
654
  return [(0, core_1.raw)(`${prop.formula(alias)} as ${aliased}`)];
648
655
  }
649
656
  if (tableAlias) {
650
- 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}`));
651
658
  }
652
659
  return prop.fieldNames;
653
660
  }
@@ -847,8 +854,8 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
847
854
  meta.props
848
855
  .filter(prop => prop.formula && !lazyProps.includes(prop))
849
856
  .forEach(prop => {
850
- const alias = qb.ref(qb.alias).toString();
851
- 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();
852
859
  ret.push((0, core_1.raw)(`${prop.formula(alias)} as ${aliased}`));
853
860
  });
854
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.80",
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.80"
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
  });
@@ -515,7 +521,7 @@ class QueryBuilderHelper {
515
521
  }
516
522
  const versionProperty = meta.properties[meta.versionProperty];
517
523
  let sql = this.platform.quoteIdentifier(versionProperty.fieldNames[0]) + ' + 1';
518
- if (versionProperty.type.toLowerCase() === 'date') {
524
+ if (versionProperty.runtimeType === 'Date') {
519
525
  sql = this.platform.getCurrentTimestampSQL(versionProperty.length);
520
526
  }
521
527
  qb.update(versionProperty.fieldNames[0], this.knex.raw(sql));
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;