@mikro-orm/core 7.1.0-dev.4 → 7.1.0-dev.5

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.
@@ -316,11 +316,30 @@ export class EntityFactory {
316
316
  else if (!onCreateOnly && prop.default != null && !isRaw(prop.default) && entity[prop.name] === undefined) {
317
317
  entity[prop.name] = prop.default;
318
318
  }
319
+ else if (!onCreateOnly &&
320
+ this.#config.get('initNullableProperties') &&
321
+ prop.nullable &&
322
+ prop.default == null &&
323
+ !prop.defaultRaw &&
324
+ entity[prop.name] === undefined) {
325
+ entity[prop.name] = (this.#config.get('forceUndefined') ? undefined : null);
326
+ }
319
327
  if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
320
328
  const items = prop.array ? entity[prop.name] : [entity[prop.name]];
321
329
  for (const item of items) {
322
330
  // Embedded sub-properties need all defaults since the DB can't apply them within JSON columns.
323
- this.assignDefaultValues(item, prop.targetMeta);
331
+ // For polymorphic embeddables, resolve the actual subtype to avoid setting
332
+ // properties from other subtypes (e.g. Cat's canMeow on a Dog instance).
333
+ let targetMeta = prop.targetMeta;
334
+ if (targetMeta.polymorphs && targetMeta.discriminatorColumn) {
335
+ const discValue = item[targetMeta.discriminatorColumn];
336
+ // eslint-disable-next-line eqeqeq
337
+ const resolved = targetMeta.polymorphs.find(m => m.discriminatorValue == discValue);
338
+ if (resolved) {
339
+ targetMeta = resolved;
340
+ }
341
+ }
342
+ this.assignDefaultValues(item, targetMeta);
324
343
  }
325
344
  }
326
345
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.1.0-dev.4",
3
+ "version": "7.1.0-dev.5",
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
  "keywords": [
6
6
  "data-mapper",
package/typings.js CHANGED
@@ -4,7 +4,6 @@ import { EntityHelper } from './entity/EntityHelper.js';
4
4
  import { helper } from './entity/wrap.js';
5
5
  import { Utils } from './utils/Utils.js';
6
6
  import { EntityComparator } from './utils/EntityComparator.js';
7
- import { BaseEntity } from './entity/BaseEntity.js';
8
7
  /** Symbol used to declare a custom repository type on an entity class (e.g., `[EntityRepositoryType]?: BookRepository`). */
9
8
  export const EntityRepositoryType = Symbol('EntityRepositoryType');
10
9
  /** Symbol used to declare the primary key property name(s) on an entity (e.g., `[PrimaryKeyProp]?: 'id'`). */
@@ -44,7 +43,14 @@ export class EntityMetadata {
44
43
  Object.assign(this, meta);
45
44
  const name = meta.className ?? meta.name;
46
45
  if (!this.class && name) {
47
- const Class = this.extends === BaseEntity ? { [name]: class extends BaseEntity {
46
+ let Parent;
47
+ if (typeof this.extends === 'function') {
48
+ Parent = this.extends;
49
+ }
50
+ else if (this.extends != null && typeof this.extends.class === 'function') {
51
+ Parent = this.extends.class;
52
+ }
53
+ const Class = Parent ? { [name]: class extends Parent {
48
54
  } }[name] : { [name]: class {
49
55
  } }[name];
50
56
  this.class = Class;
@@ -529,6 +529,13 @@ export interface Options<Driver extends IDatabaseDriver = IDatabaseDriver, EM ex
529
529
  * @default false
530
530
  */
531
531
  forceUndefined: boolean;
532
+ /**
533
+ * Initialize nullable properties to `null` (or `undefined` when `forceUndefined` is set)
534
+ * during `em.create()` when they are not provided in the data. Without this option,
535
+ * nullable properties remain `undefined` until the entity is loaded from the database.
536
+ * @default false
537
+ */
538
+ initNullableProperties: boolean;
532
539
  /**
533
540
  * Property `onCreate` hooks are normally executed during `flush` operation.
534
541
  * With this option, they will be processed early inside `em.create()` method.
@@ -62,6 +62,7 @@ const DEFAULTS = {
62
62
  upsertManaged: true,
63
63
  forceEntityConstructor: false,
64
64
  forceUndefined: false,
65
+ initNullableProperties: false,
65
66
  forceUtcTimezone: true,
66
67
  processOnCreateHooksEarly: true,
67
68
  ensureDatabase: true,
package/utils/Utils.js CHANGED
@@ -141,7 +141,7 @@ export function parseJsonSafe(value) {
141
141
  /** Collection of general-purpose utility methods used throughout the ORM. */
142
142
  export class Utils {
143
143
  static PK_SEPARATOR = '~~~';
144
- static #ORM_VERSION = '7.1.0-dev.4';
144
+ static #ORM_VERSION = '7.1.0-dev.5';
145
145
  /**
146
146
  * Checks if the argument is instance of `Object`. Returns false for arrays.
147
147
  */