@mikro-orm/knex 5.7.8-dev.3 → 5.7.8-dev.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "5.7.8-dev.3",
3
+ "version": "5.7.8-dev.4",
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.7.7"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~5.7.8-dev.3",
70
- "@mikro-orm/entity-generator": "~5.7.8-dev.3",
71
- "@mikro-orm/migrations": "~5.7.8-dev.3",
69
+ "@mikro-orm/core": "~5.7.8-dev.4",
70
+ "@mikro-orm/entity-generator": "~5.7.8-dev.4",
71
+ "@mikro-orm/migrations": "~5.7.8-dev.4",
72
72
  "better-sqlite3": "*",
73
73
  "mssql": "*",
74
74
  "mysql": "*",
@@ -58,6 +58,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
58
58
  private _orderBy;
59
59
  private _groupBy;
60
60
  private _having;
61
+ private _returning?;
61
62
  private _onConflict?;
62
63
  private _limit?;
63
64
  private _offset?;
@@ -107,6 +108,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
107
108
  onConflict(fields?: Field<T> | Field<T>[]): this;
108
109
  ignore(): this;
109
110
  merge(data?: EntityData<T> | Field<T>[]): this;
111
+ returning(fields?: Field<T> | Field<T>[]): this;
110
112
  /**
111
113
  * @internal
112
114
  */
@@ -266,6 +266,10 @@ class QueryBuilder {
266
266
  this._onConflict[this._onConflict.length - 1].merge = data;
267
267
  return this;
268
268
  }
269
+ returning(fields) {
270
+ this._returning = fields != null ? core_1.Utils.asArray(fields) : fields;
271
+ return this;
272
+ }
269
273
  /**
270
274
  * @internal
271
275
  */
@@ -377,7 +381,7 @@ class QueryBuilder {
377
381
  if (this.lockMode) {
378
382
  this.helper.getLockSQL(qb, this.lockMode, this.lockTables);
379
383
  }
380
- this.helper.finalize(this.type ?? enums_1.QueryType.SELECT, qb, this.mainAlias.metadata);
384
+ this.helper.finalize(this.type ?? enums_1.QueryType.SELECT, qb, this.mainAlias.metadata, this._data, this._returning);
381
385
  return qb;
382
386
  }
383
387
  /**
@@ -565,7 +569,7 @@ class QueryBuilder {
565
569
  // clone array/object properties
566
570
  const properties = [
567
571
  'flags', '_populate', '_populateWhere', '_populateMap', '_joins', '_joinedProps', '_cond', '_data', '_orderBy',
568
- '_schema', '_indexHint', '_cache', 'subQueries', 'lockMode', 'lockTables', '_groupBy', '_having',
572
+ '_schema', '_indexHint', '_cache', 'subQueries', 'lockMode', 'lockTables', '_groupBy', '_having', '_returning',
569
573
  ];
570
574
  properties.forEach(prop => qb[prop] = core_1.Utils.copy(this[prop]));
571
575
  /* istanbul ignore else */
@@ -48,7 +48,7 @@ export declare class QueryBuilderHelper {
48
48
  private getOperatorReplacement;
49
49
  getQueryOrder(type: QueryType, orderBy: FlatQueryOrderMap | FlatQueryOrderMap[], populate: Dictionary<string>): string;
50
50
  getQueryOrderFromObject(type: QueryType, orderBy: FlatQueryOrderMap, populate: Dictionary<string>): string;
51
- finalize(type: QueryType, qb: Knex.QueryBuilder, meta?: EntityMetadata): void;
51
+ finalize(type: QueryType, qb: Knex.QueryBuilder, meta?: EntityMetadata, data?: Dictionary, returning?: Field<any>[]): void;
52
52
  splitField(field: string, greedyAlias?: boolean): [string, string];
53
53
  getLockSQL(qb: Knex.QueryBuilder, lockMode: LockMode, lockTables?: string[]): void;
54
54
  updateVersionProperty(qb: Knex.QueryBuilder, data: Dictionary): void;
@@ -460,11 +460,22 @@ class QueryBuilderHelper {
460
460
  });
461
461
  return ret.join(', ');
462
462
  }
463
- finalize(type, qb, meta) {
464
- const useReturningStatement = type === enums_1.QueryType.INSERT && this.platform.usesReturningStatement() && meta && !meta.compositePK;
465
- if (useReturningStatement) {
466
- const returningProps = meta.hydrateProps.filter(prop => prop.persist !== false && (prop.primary || prop.defaultRaw));
467
- qb.returning(core_1.Utils.flatten(returningProps.map(prop => prop.fieldNames)));
463
+ finalize(type, qb, meta, data, returning) {
464
+ if (!meta || !data || !this.platform.usesReturningStatement()) {
465
+ return;
466
+ }
467
+ // always respect explicit returning hint
468
+ if (returning && returning.length > 0) {
469
+ qb.returning(returning.map(field => this.mapper(field, type)));
470
+ return;
471
+ }
472
+ if (type === enums_1.QueryType.INSERT) {
473
+ const returningProps = meta.hydrateProps
474
+ .filter(prop => prop.returning || (prop.persist !== false && ((prop.primary && prop.autoincrement) || prop.defaultRaw)))
475
+ .filter(prop => !(prop.name in data));
476
+ if (returningProps.length > 0) {
477
+ qb.returning(core_1.Utils.flatten(returningProps.map(prop => prop.fieldNames)));
478
+ }
468
479
  }
469
480
  }
470
481
  splitField(field, greedyAlias = false) {