@mikro-orm/core 6.4.1-dev.17 → 6.4.1-dev.19

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.
@@ -283,18 +283,21 @@ class EntityFactory {
283
283
  extractConstructorParams(meta, data, options) {
284
284
  return meta.constructorParams.map(k => {
285
285
  if (meta.properties[k] && [enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(meta.properties[k].kind) && data[k]) {
286
- const entity = this.unitOfWork.getById(meta.properties[k].type, data[k], options.schema);
286
+ const pk = Reference_1.Reference.unwrapReference(data[k]);
287
+ const entity = this.unitOfWork.getById(meta.properties[k].type, pk, options.schema);
287
288
  if (entity) {
288
289
  return entity;
289
290
  }
290
291
  if (Utils_1.Utils.isEntity(data[k])) {
291
292
  return data[k];
292
293
  }
293
- if (Utils_1.Utils.isObject(data[k]) && !Utils_1.Utils.extractPK(data[k], meta.properties[k].targetMeta, true)) {
294
+ const nakedPk = Utils_1.Utils.extractPK(data[k], meta.properties[k].targetMeta, true);
295
+ if (Utils_1.Utils.isObject(data[k]) && !nakedPk) {
294
296
  return this.create(meta.properties[k].type, data[k], options);
295
297
  }
296
298
  const { newEntity, initialized, ...rest } = options;
297
- return this.createReference(meta.properties[k].type, data[k], rest);
299
+ const target = this.createReference(meta.properties[k].type, nakedPk, rest);
300
+ return Reference_1.Reference.wrapReference(target, meta.properties[k]);
298
301
  }
299
302
  if (meta.properties[k]?.kind === enums_1.ReferenceKind.EMBEDDED && data[k]) {
300
303
  /* istanbul ignore next */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.4.1-dev.17",
3
+ "version": "6.4.1-dev.19",
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",
@@ -64,7 +64,7 @@
64
64
  "esprima": "4.0.1",
65
65
  "fs-extra": "11.2.0",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.4.1-dev.17",
67
+ "mikro-orm": "6.4.1-dev.19",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }
package/typings.d.ts CHANGED
@@ -388,6 +388,7 @@ export declare class EntityMetadata<T = any> {
388
388
  addProperty(prop: Partial<EntityProperty<T>>, sync?: boolean): void;
389
389
  removeProperty(name: string, sync?: boolean): void;
390
390
  getPrimaryProps(): EntityProperty<T>[];
391
+ getPrimaryProp(): EntityProperty<T>;
391
392
  get tableName(): string;
392
393
  set tableName(name: string);
393
394
  sync(initIndexes?: boolean): void;
package/typings.js CHANGED
@@ -50,6 +50,9 @@ class EntityMetadata {
50
50
  getPrimaryProps() {
51
51
  return this.primaryKeys.map(pk => this.properties[pk]);
52
52
  }
53
+ getPrimaryProp() {
54
+ return this.properties[this.primaryKeys[0]];
55
+ }
53
56
  get tableName() {
54
57
  return this.collection;
55
58
  }
@@ -31,6 +31,14 @@ class ChangeSet {
31
31
  else {
32
32
  this.primaryKey = this.originalEntity[this.meta.primaryKeys[0]];
33
33
  }
34
+ if (!this.meta.compositePK
35
+ && this.meta.getPrimaryProp().targetMeta?.compositePK
36
+ && typeof this.primaryKey === 'object'
37
+ && this.primaryKey !== null) {
38
+ this.primaryKey = this.meta.getPrimaryProp().targetMeta.primaryKeys.map(childPK => {
39
+ return this.primaryKey[childPK];
40
+ });
41
+ }
34
42
  if (object && this.primaryKey != null) {
35
43
  return Utils_1.Utils.primaryKeyToObject(this.meta, this.primaryKey);
36
44
  }
@@ -774,7 +774,7 @@ class UnitOfWork {
774
774
  findExtraUpdates(changeSet, props) {
775
775
  for (const prop of props) {
776
776
  const ref = changeSet.entity[prop.name];
777
- if (!ref) {
777
+ if (!ref || prop.deferMode === enums_1.DeferMode.INITIALLY_DEFERRED) {
778
778
  continue;
779
779
  }
780
780
  if (Utils_1.Utils.isCollection(ref)) {
@@ -74,7 +74,19 @@ class EntityComparator {
74
74
  else {
75
75
  const pk = meta.primaryKeys[0];
76
76
  if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
77
- lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getPrimaryKey();`);
77
+ lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) {`);
78
+ lines.push(` const pk = entity${this.wrap(pk)}.__helper.getPrimaryKey();`);
79
+ if (meta.properties[pk].targetMeta.compositePK) {
80
+ lines.push(` if (typeof pk === 'object') {`);
81
+ lines.push(` return [`);
82
+ for (const childPK of meta.properties[pk].targetMeta.primaryKeys) {
83
+ lines.push(` pk${this.wrap(childPK)},`);
84
+ }
85
+ lines.push(` ];`);
86
+ lines.push(` }`);
87
+ }
88
+ lines.push(` return pk;`);
89
+ lines.push(` }`);
78
90
  }
79
91
  lines.push(` return entity${this.wrap(pk)};`);
80
92
  }