@mikro-orm/core 6.5.7-dev.9 → 6.5.7

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.
@@ -579,6 +579,8 @@ export interface MergeOptions {
579
579
  schema?: string;
580
580
  disableContextResolution?: boolean;
581
581
  keepIdentity?: boolean;
582
+ validate?: boolean;
583
+ cascade?: boolean; /** @default true */
582
584
  }
583
585
  export interface ForkOptions {
584
586
  /** do we want a clear identity map? defaults to true */
package/EntityManager.js CHANGED
@@ -1186,8 +1186,12 @@ class EntityManager {
1186
1186
  }
1187
1187
  const em = options.disableContextResolution ? this : this.getContext();
1188
1188
  options.schema ??= em._schema;
1189
+ options.validate ??= true;
1190
+ options.cascade ??= true;
1189
1191
  entityName = utils_1.Utils.className(entityName);
1190
- em.validator.validatePrimaryKey(data, em.metadata.get(entityName));
1192
+ if (options.validate) {
1193
+ em.validator.validatePrimaryKey(data, em.metadata.get(entityName));
1194
+ }
1191
1195
  let entity = em.unitOfWork.tryGetById(entityName, data, options.schema, false);
1192
1196
  if (entity && (0, entity_1.helper)(entity).__managed && (0, entity_1.helper)(entity).__initialized && !options.refresh) {
1193
1197
  return entity;
@@ -1196,12 +1200,16 @@ class EntityManager {
1196
1200
  const childMeta = em.metadata.getByDiscriminatorColumn(meta, data);
1197
1201
  const dataIsEntity = utils_1.Utils.isEntity(data);
1198
1202
  if (options.keepIdentity && entity && dataIsEntity && entity !== data) {
1199
- em.entityFactory.mergeData(meta, entity, (0, entity_1.helper)(data).__originalEntityData, { initialized: true, merge: true, ...options });
1203
+ (0, entity_1.helper)(entity).__data = (0, entity_1.helper)(data).__data;
1204
+ (0, entity_1.helper)(entity).__originalEntityData = (0, entity_1.helper)(data).__originalEntityData;
1200
1205
  return entity;
1201
1206
  }
1202
1207
  entity = dataIsEntity ? data : em.entityFactory.create(entityName, data, { merge: true, ...options });
1203
- em.validator.validate(entity, data, childMeta ?? meta);
1204
- em.unitOfWork.merge(entity);
1208
+ if (options.validate) {
1209
+ em.validator.validate(entity, data, childMeta ?? meta);
1210
+ }
1211
+ const visited = options.cascade ? undefined : new Set([entity]);
1212
+ em.unitOfWork.merge(entity, visited);
1205
1213
  return entity;
1206
1214
  }
1207
1215
  /**
@@ -14,7 +14,7 @@ class EntityValidator {
14
14
  }
15
15
  validate(entity, payload, meta) {
16
16
  meta.props.forEach(prop => {
17
- if (prop.inherited) {
17
+ if (prop.inherited || (prop.persist === false && prop.userDefined !== false)) {
18
18
  return;
19
19
  }
20
20
  if ([enums_1.ReferenceKind.ONE_TO_MANY, enums_1.ReferenceKind.MANY_TO_MANY].includes(prop.kind)) {
@@ -73,7 +73,7 @@ export declare class PropertyOptionsBuilder<Value> {
73
73
  /**
74
74
  * Explicitly specify the auto increment of the primary key.
75
75
  */
76
- autoincrement(autoincrement?: boolean): PropertyOptionsBuilder<Value>;
76
+ autoincrement<T extends boolean = true>(autoincrement?: T): PropertyOptionsBuilder<T extends true ? Opt<Value> : Value>;
77
77
  /**
78
78
  * Add the property to the `returning` statement.
79
79
  */
@@ -73,7 +73,14 @@ class ObjectHydrator extends Hydrator_1.Hydrator {
73
73
  ret.push(` const oldValue_${idx} = entity${entityKey};`);
74
74
  }
75
75
  ret.push(` if (data${dataKey} === null) {`);
76
- ret.push(` entity${entityKey} = ${nullVal};`);
76
+ if (prop.ref) {
77
+ ret.push(` entity${entityKey} = new ScalarReference();`);
78
+ ret.push(` entity${entityKey}.bind(entity, '${prop.name}');`);
79
+ ret.push(` entity${entityKey}.set(${nullVal});`);
80
+ }
81
+ else {
82
+ ret.push(` entity${entityKey} = ${nullVal};`);
83
+ }
77
84
  ret.push(` } else if (typeof data${dataKey} !== 'undefined') {`);
78
85
  if (prop.customType) {
79
86
  registerCustomType(prop, convertorKey, 'convertToJSValue', context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.5.7-dev.9",
3
+ "version": "6.5.7",
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.3.2",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.5.7-dev.9",
67
+ "mikro-orm": "6.5.7",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }
@@ -6,6 +6,7 @@ const events_1 = require("../events");
6
6
  const TransactionContext_1 = require("../utils/TransactionContext");
7
7
  const unit_of_work_1 = require("../unit-of-work");
8
8
  const errors_1 = require("../errors");
9
+ const wrap_1 = require("../entity/wrap");
9
10
  /**
10
11
  * Manages transaction lifecycle and propagation for EntityManager.
11
12
  */
@@ -152,8 +153,28 @@ class TransactionManager {
152
153
  * Merges entities from fork to parent EntityManager.
153
154
  */
154
155
  mergeEntitiesToParent(fork, parent) {
156
+ const parentUoW = parent.getUnitOfWork(false);
157
+ // perf: if parent is empty, we can just move all entities from the fork to skip the `em.merge` overhead
158
+ if (parentUoW.getIdentityMap().keys().length === 0) {
159
+ for (const entity of fork.getUnitOfWork(false).getIdentityMap()) {
160
+ parentUoW.getIdentityMap().store(entity);
161
+ (0, wrap_1.helper)(entity).__em = parent;
162
+ }
163
+ return;
164
+ }
155
165
  for (const entity of fork.getUnitOfWork(false).getIdentityMap()) {
156
- parent.merge(entity, { disableContextResolution: true, keepIdentity: true, refresh: true });
166
+ const wrapped = (0, wrap_1.helper)(entity);
167
+ const meta = wrapped.__meta;
168
+ // eslint-disable-next-line dot-notation
169
+ const parentEntity = parentUoW.getById(meta.className, wrapped.getPrimaryKey(), parent['_schema'], true);
170
+ if (parentEntity && parentEntity !== entity) {
171
+ const parentWrapped = (0, wrap_1.helper)(parentEntity);
172
+ parentWrapped.__data = (0, wrap_1.helper)(entity).__data;
173
+ parentWrapped.__originalEntityData = (0, wrap_1.helper)(entity).__originalEntityData;
174
+ }
175
+ else {
176
+ parentUoW.merge(entity, new Set([entity]));
177
+ }
157
178
  }
158
179
  }
159
180
  /**