@mikro-orm/core 7.1.0-dev.3 → 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
  }
@@ -127,9 +127,13 @@ export interface PropertyChain<Value, Options> {
127
127
  pivotEntity(pivotEntity: () => EntityName): HasKind<Options, 'm:n'> extends true ? PropertyChain<Value, Options> : never;
128
128
  fixedOrder(fixedOrder?: boolean): HasKind<Options, 'm:n'> extends true ? PropertyChain<Value, Options> : never;
129
129
  fixedOrderColumn(fixedOrderColumn: string): HasKind<Options, 'm:n'> extends true ? PropertyChain<Value, Options> : never;
130
- array(): HasKind<Options, 'embedded' | 'enum'> extends true ? PropertyChain<Value, Omit<Options, 'array'> & {
130
+ array(): Options extends {
131
+ kind: infer K extends string;
132
+ } ? K extends 'embedded' | 'enum' ? PropertyChain<Value, Omit<Options, 'array'> & {
131
133
  array: true;
132
- }> : never;
134
+ }> : never : PropertyChain<Value, Omit<Options, 'array'> & {
135
+ array: true;
136
+ }>;
133
137
  prefix(prefix: string | boolean): HasKind<Options, 'embedded'> extends true ? PropertyChain<Value, Options> : never;
134
138
  prefixMode(prefixMode: EmbeddedPrefixMode): HasKind<Options, 'embedded'> extends true ? PropertyChain<Value, Options> : never;
135
139
  object(object?: boolean): HasKind<Options, 'embedded'> extends true ? PropertyChain<Value, Options> : never;
@@ -1646,7 +1646,14 @@ export class MetadataDiscovery {
1646
1646
  return (!Type.getType(t[type]).ensureComparable(meta, prop) && (prop.type === t[type] || brand === type));
1647
1647
  });
1648
1648
  if (type) {
1649
- prop.type = type === 'datetime' ? 'Date' : type;
1649
+ if (prop.array) {
1650
+ // built-in type + array: true — force-create instance for ArrayType wrapping
1651
+ prop.customType = new prop.type();
1652
+ prop.type = prop.customType.constructor.name;
1653
+ }
1654
+ else {
1655
+ prop.type = type === 'datetime' ? 'Date' : type;
1656
+ }
1650
1657
  }
1651
1658
  else {
1652
1659
  prop.customType = new prop.type();
@@ -1656,6 +1663,30 @@ export class MetadataDiscovery {
1656
1663
  if (simple) {
1657
1664
  return;
1658
1665
  }
1666
+ if (prop.array &&
1667
+ prop.customType &&
1668
+ !(prop.customType instanceof t.array) &&
1669
+ !(prop.customType instanceof t.enumArray) &&
1670
+ prop.kind === ReferenceKind.SCALAR) {
1671
+ const innerType = prop.customType;
1672
+ innerType.platform = this.#platform;
1673
+ innerType.meta = meta;
1674
+ innerType.prop = prop;
1675
+ if (!prop.columnTypes) {
1676
+ const arrayDecl = this.#platform.getArrayDeclarationSQL();
1677
+ if (arrayDecl.endsWith('[]')) {
1678
+ // native array support (e.g. postgres) — use inner type's column type with [] suffix
1679
+ prop.columnTypes = [innerType.getColumnType(prop, this.#platform) + '[]'];
1680
+ }
1681
+ else {
1682
+ // non-native (e.g. mysql, sqlite) — store as text/clob
1683
+ prop.columnTypes = [arrayDecl];
1684
+ }
1685
+ }
1686
+ const platform = this.#platform;
1687
+ prop.customType = new t.array(v => innerType.convertToJSValue(v, platform), v => innerType.convertToDatabaseValue(v, platform));
1688
+ prop.type = prop.customType.constructor.name;
1689
+ }
1659
1690
  if (!prop.customType && ['json', 'jsonb'].includes(prop.type?.toLowerCase())) {
1660
1691
  prop.customType = new t.json();
1661
1692
  }
@@ -202,6 +202,12 @@ export interface PropertyOptions<Owner> {
202
202
  * @see https://mikro-orm.io/docs/defining-entities#lazy-scalar-properties
203
203
  */
204
204
  lazy?: boolean;
205
+ /**
206
+ * Marks this property as an array. When used with a custom type, the type is automatically
207
+ * wrapped in an `ArrayType` that delegates element conversion to the inner type.
208
+ * The column type is inferred as `innerType[]` (e.g. `int[]`, `time[]`).
209
+ */
210
+ array?: boolean;
205
211
  /**
206
212
  * Set true to define entity's unique primary key identifier.
207
213
  * Alias for `@PrimaryKey()` decorator
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.1.0-dev.3",
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.3';
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
  */