@mikro-orm/core 7.0.2-dev.14 → 7.0.2-dev.16

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.
@@ -5,6 +5,7 @@ import { Reference } from './Reference.js';
5
5
  import { helper } from './wrap.js';
6
6
  import { EntityHelper } from './EntityHelper.js';
7
7
  import { JsonType } from '../types/JsonType.js';
8
+ import { isRaw } from '../utils/RawQueryFragment.js';
8
9
  /** @internal Factory responsible for creating, merging, and hydrating entity instances. */
9
10
  export class EntityFactory {
10
11
  #driver;
@@ -275,9 +276,21 @@ export class EntityFactory {
275
276
  }
276
277
  assignDefaultValues(entity, meta) {
277
278
  for (const prop of meta.props) {
279
+ if (prop.embedded || [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
280
+ continue;
281
+ }
278
282
  if (prop.onCreate) {
279
283
  entity[prop.name] ??= prop.onCreate(entity, this.#em);
280
284
  }
285
+ else if (prop.default != null && !isRaw(prop.default) && entity[prop.name] === undefined) {
286
+ entity[prop.name] = prop.default;
287
+ }
288
+ if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
289
+ const items = prop.array ? entity[prop.name] : [entity[prop.name]];
290
+ for (const item of items) {
291
+ this.assignDefaultValues(item, prop.targetMeta);
292
+ }
293
+ }
281
294
  }
282
295
  }
283
296
  hydrate(entity, meta, data, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.0.2-dev.14",
3
+ "version": "7.0.2-dev.16",
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",
@@ -6,6 +6,7 @@ import { validateEntity } from '../entity/validators.js';
6
6
  import { Reference } from '../entity/Reference.js';
7
7
  import { PolymorphicRef } from '../entity/PolymorphicRef.js';
8
8
  import { ReferenceKind } from '../enums.js';
9
+ import { isRaw } from '../utils/RawQueryFragment.js';
9
10
  /** @internal Computes change sets by comparing entity state against original snapshots. */
10
11
  export class ChangeSetComputer {
11
12
  #comparator;
@@ -83,14 +84,24 @@ export class ChangeSetComputer {
83
84
  (Utils.isScalarReference(entity[prop.name]) && entity[prop.name].unwrap() == null))) {
84
85
  entity[prop.name] = prop.onCreate(entity, this.#em);
85
86
  }
87
+ else if (prop.default != null &&
88
+ !isRaw(prop.default) &&
89
+ ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
90
+ type === ChangeSetType.CREATE &&
91
+ entity[prop.name] === undefined) {
92
+ entity[prop.name] = prop.default;
93
+ }
86
94
  if (prop.onUpdate && type === ChangeSetType.UPDATE) {
87
95
  const pairs = map.get(entity) ?? [];
88
96
  pairs.push([prop.name, prop.onUpdate(entity, this.#em)]);
89
97
  map.set(entity, pairs);
90
98
  }
91
99
  if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
92
- for (const embeddedProp of prop.targetMeta.hydrateProps) {
93
- this.processPropertyInitializers(entity[prop.name], embeddedProp, type, map, nested || prop.object);
100
+ const items = prop.array ? entity[prop.name] : [entity[prop.name]];
101
+ for (const item of items) {
102
+ for (const embeddedProp of prop.targetMeta.hydrateProps) {
103
+ this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
104
+ }
94
105
  }
95
106
  }
96
107
  }
package/utils/Utils.js CHANGED
@@ -129,7 +129,7 @@ export function parseJsonSafe(value) {
129
129
  /** Collection of general-purpose utility methods used throughout the ORM. */
130
130
  export class Utils {
131
131
  static PK_SEPARATOR = '~~~';
132
- static #ORM_VERSION = '7.0.2-dev.14';
132
+ static #ORM_VERSION = '7.0.2-dev.16';
133
133
  /**
134
134
  * Checks if the argument is instance of `Object`. Returns false for arrays.
135
135
  */