From bc57ed0e2d0e5a2f92e4ddaa5cdd77ff88195a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Mon, 21 Feb 2022 21:17:35 +0100 Subject: [PATCH] fix(core): fix mapping default values of relation properties --- packages/core/src/EntityManager.ts | 8 +- packages/core/src/entity/EntityFactory.ts | 8 +- .../src/unit-of-work/ChangeSetPersister.ts | 14 +--- packages/core/src/utils/EntityComparator.ts | 2 +- ...entities-in-embeddables.mongo.test.ts.snap | 24 +++--- ...ities-in-embeddables.postgres.test.ts.snap | 24 +++--- tests/issues/GHx1.test.ts | 75 +++++++++++++++++++ 7 files changed, 112 insertions(+), 43 deletions(-) create mode 100644 tests/issues/GHx1.test.ts diff --git a/packages/core/src/EntityManager.ts b/packages/core/src/EntityManager.ts index 40f7f111c58e..a10cc506a97a 100644 --- a/packages/core/src/EntityManager.ts +++ b/packages/core/src/EntityManager.ts @@ -29,8 +29,8 @@ export class EntityManager { private readonly repositoryMap: Dictionary> = {}; private readonly entityLoader: EntityLoader = new EntityLoader(this); private readonly comparator = this.config.getComparator(this.metadata); + private readonly entityFactory: EntityFactory = new EntityFactory(this); private readonly unitOfWork: UnitOfWork = new UnitOfWork(this); - private readonly entityFactory: EntityFactory = new EntityFactory(this.unitOfWork, this); private readonly resultCache = this.config.getResultCacheAdapter(); private filters: Dictionary = {}; private filterParams: Dictionary = {}; @@ -835,7 +835,11 @@ export class EntityManager { /** * Gets the UnitOfWork used by the EntityManager to coordinate operations. */ - getUnitOfWork(): UnitOfWork { + getUnitOfWork(useContext = true): UnitOfWork { + if (!useContext) { + return this.unitOfWork; + } + return this.getContext().unitOfWork; } diff --git a/packages/core/src/entity/EntityFactory.ts b/packages/core/src/entity/EntityFactory.ts index da7788b377b8..2fff3b632687 100644 --- a/packages/core/src/entity/EntityFactory.ts +++ b/packages/core/src/entity/EntityFactory.ts @@ -1,6 +1,5 @@ import { Utils } from '../utils/Utils'; import type { AnyEntity, Dictionary, EntityData, EntityMetadata, EntityName, EntityProperty, New, Primary } from '../typings'; -import type { UnitOfWork } from '../unit-of-work'; import type { EntityManager } from '../EntityManager'; import { EventType, ReferenceType } from '../enums'; import { Reference } from './Reference'; @@ -24,8 +23,7 @@ export class EntityFactory { private readonly eventManager = this.em.getEventManager(); private readonly comparator = this.em.getComparator(); - constructor(private readonly unitOfWork: UnitOfWork, - private readonly em: EntityManager) { } + constructor(private readonly em: EntityManager) { } create, P extends string = string>(entityName: EntityName, data: EntityData, options: FactoryOptions = {}): New { data = Reference.unwrapReference(data); @@ -303,4 +301,8 @@ export class EntityFactory { }); } + private get unitOfWork() { + return this.em.getUnitOfWork(false); + } + } diff --git a/packages/core/src/unit-of-work/ChangeSetPersister.ts b/packages/core/src/unit-of-work/ChangeSetPersister.ts index 906bc6a80d02..9acaf1757216 100644 --- a/packages/core/src/unit-of-work/ChangeSetPersister.ts +++ b/packages/core/src/unit-of-work/ChangeSetPersister.ts @@ -340,19 +340,7 @@ export class ChangeSetPersister { for (const changeSet of changeSets) { const data = map.get(changeSet.entity.__helper!.getSerializedPrimaryKey()); - - for (const prop of reloadProps) { - const value = data![prop.name]; - - // needed for sqlite - if (prop.type.toLowerCase() === 'date') { - changeSet.entity[prop.name] = new Date(value) as unknown as T[keyof T & string]; - } else { - changeSet.entity[prop.name] = value; - } - - changeSet.payload![prop.name] = value; - } + this.hydrator.hydrate(changeSet.entity, meta, data as EntityData, this.factory, 'returning', false, true); } } diff --git a/packages/core/src/utils/EntityComparator.ts b/packages/core/src/utils/EntityComparator.ts index 3a5a07a68dfb..b26d111285eb 100644 --- a/packages/core/src/utils/EntityComparator.ts +++ b/packages/core/src/utils/EntityComparator.ts @@ -317,7 +317,7 @@ export class EntityComparator { } if (isRef) { - ret += ` && (entity${entityKey} == null || entity${entityKey}.__helper.hasPrimaryKey())`; + ret += ` && (entity${entityKey} == null || entity${entityKey}.__helper?.hasPrimaryKey())`; } return ret; diff --git a/tests/features/embeddables/__snapshots__/entities-in-embeddables.mongo.test.ts.snap b/tests/features/embeddables/__snapshots__/entities-in-embeddables.mongo.test.ts.snap index 78cfeb55204e..90e1b856c73c 100644 --- a/tests/features/embeddables/__snapshots__/entities-in-embeddables.mongo.test.ts.snap +++ b/tests/features/embeddables/__snapshots__/entities-in-embeddables.mongo.test.ts.snap @@ -20,7 +20,7 @@ exports[`embedded entities in mongo diffing 1`] = ` if (entity.profile1.identity.meta != null) { ret.profile1_identity_meta_foo = clone(entity.profile1.identity.meta.foo); ret.profile1_identity_meta_bar = clone(entity.profile1.identity.meta.bar); - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.meta !== 'undefined' && (entity.profile1.identity.meta.source == null || entity.profile1.identity.meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.meta !== 'undefined' && (entity.profile1.identity.meta.source == null || entity.profile1.identity.meta.source.__helper?.hasPrimaryKey())) { ret.profile1_identity_meta_source = getPrimaryKeyValues_profile1_identity_meta_source(entity.profile1.identity.meta.source); } @@ -39,7 +39,7 @@ exports[`embedded entities in mongo diffing 1`] = ` ret.profile1_identity_links[idx_0].meta = {}; ret.profile1_identity_links[idx_0].meta.foo = clone(entity.profile1.identity.links[idx_0].meta.foo); ret.profile1_identity_links[idx_0].meta.bar = clone(entity.profile1.identity.links[idx_0].meta.bar); - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].meta !== 'undefined' && (entity.profile1.identity.links[idx_0].meta.source == null || entity.profile1.identity.links[idx_0].meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].meta !== 'undefined' && (entity.profile1.identity.links[idx_0].meta.source == null || entity.profile1.identity.links[idx_0].meta.source.__helper?.hasPrimaryKey())) { ret.profile1_identity_links[idx_0].meta.source = getPrimaryKeyValues_profile1_identity_links_meta_source(entity.profile1.identity.links[idx_0].meta.source); } @@ -53,7 +53,7 @@ exports[`embedded entities in mongo diffing 1`] = ` ret.profile1_identity_links[idx_0].metas[idx_1] = {}; ret.profile1_identity_links[idx_0].metas[idx_1].foo = clone(entity.profile1.identity.links[idx_0].metas[idx_1].foo); ret.profile1_identity_links[idx_0].metas[idx_1].bar = clone(entity.profile1.identity.links[idx_0].metas[idx_1].bar); - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].metas !== 'undefined' && (entity.profile1.identity.links[idx_0].metas[idx_1].source == null || entity.profile1.identity.links[idx_0].metas[idx_1].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].metas !== 'undefined' && (entity.profile1.identity.links[idx_0].metas[idx_1].source == null || entity.profile1.identity.links[idx_0].metas[idx_1].source.__helper?.hasPrimaryKey())) { ret.profile1_identity_links[idx_0].metas[idx_1].source = getPrimaryKeyValues_profile1_identity_links_metas_source(entity.profile1.identity.links[idx_0].metas[idx_1].source); } @@ -61,7 +61,7 @@ exports[`embedded entities in mongo diffing 1`] = ` }); } - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && (entity.profile1.identity.links[idx_0].source == null || entity.profile1.identity.links[idx_0].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && (entity.profile1.identity.links[idx_0].source == null || entity.profile1.identity.links[idx_0].source.__helper?.hasPrimaryKey())) { ret.profile1_identity_links[idx_0].source = getPrimaryKeyValues_profile1_identity_links_source(entity.profile1.identity.links[idx_0].source); } @@ -70,13 +70,13 @@ exports[`embedded entities in mongo diffing 1`] = ` ret.profile1_identity_links = cloneEmbeddable(ret.profile1_identity_links); } - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && (entity.profile1.identity.source == null || entity.profile1.identity.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && (entity.profile1.identity.source == null || entity.profile1.identity.source.__helper?.hasPrimaryKey())) { ret.profile1_identity_source = getPrimaryKeyValues_profile1_identity_source(entity.profile1.identity.source); } } - if (typeof entity.profile1 !== 'undefined' && (entity.profile1.source == null || entity.profile1.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && (entity.profile1.source == null || entity.profile1.source.__helper?.hasPrimaryKey())) { ret.profile1_source = getPrimaryKeyValues_profile1_source(entity.profile1.source); } @@ -94,7 +94,7 @@ exports[`embedded entities in mongo diffing 1`] = ` ret.profile2.identity.meta = {}; ret.profile2.identity.meta.foo = clone(entity.profile2.identity.meta.foo); ret.profile2.identity.meta.bar = clone(entity.profile2.identity.meta.bar); - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.meta !== 'undefined' && (entity.profile2.identity.meta.source == null || entity.profile2.identity.meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.meta !== 'undefined' && (entity.profile2.identity.meta.source == null || entity.profile2.identity.meta.source.__helper?.hasPrimaryKey())) { ret.profile2.identity.meta.source = getPrimaryKeyValues_profile2_identity_meta_source(entity.profile2.identity.meta.source); } @@ -113,7 +113,7 @@ exports[`embedded entities in mongo diffing 1`] = ` ret.profile2.identity.links[idx_2].meta = {}; ret.profile2.identity.links[idx_2].meta.foo = clone(entity.profile2.identity.links[idx_2].meta.foo); ret.profile2.identity.links[idx_2].meta.bar = clone(entity.profile2.identity.links[idx_2].meta.bar); - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].meta !== 'undefined' && (entity.profile2.identity.links[idx_2].meta.source == null || entity.profile2.identity.links[idx_2].meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].meta !== 'undefined' && (entity.profile2.identity.links[idx_2].meta.source == null || entity.profile2.identity.links[idx_2].meta.source.__helper?.hasPrimaryKey())) { ret.profile2.identity.links[idx_2].meta.source = getPrimaryKeyValues_profile2_identity_links_meta_source(entity.profile2.identity.links[idx_2].meta.source); } @@ -127,7 +127,7 @@ exports[`embedded entities in mongo diffing 1`] = ` ret.profile2.identity.links[idx_2].metas[idx_3] = {}; ret.profile2.identity.links[idx_2].metas[idx_3].foo = clone(entity.profile2.identity.links[idx_2].metas[idx_3].foo); ret.profile2.identity.links[idx_2].metas[idx_3].bar = clone(entity.profile2.identity.links[idx_2].metas[idx_3].bar); - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].metas !== 'undefined' && (entity.profile2.identity.links[idx_2].metas[idx_3].source == null || entity.profile2.identity.links[idx_2].metas[idx_3].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].metas !== 'undefined' && (entity.profile2.identity.links[idx_2].metas[idx_3].source == null || entity.profile2.identity.links[idx_2].metas[idx_3].source.__helper?.hasPrimaryKey())) { ret.profile2.identity.links[idx_2].metas[idx_3].source = getPrimaryKeyValues_profile2_identity_links_metas_source(entity.profile2.identity.links[idx_2].metas[idx_3].source); } @@ -135,7 +135,7 @@ exports[`embedded entities in mongo diffing 1`] = ` }); } - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && (entity.profile2.identity.links[idx_2].source == null || entity.profile2.identity.links[idx_2].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && (entity.profile2.identity.links[idx_2].source == null || entity.profile2.identity.links[idx_2].source.__helper?.hasPrimaryKey())) { ret.profile2.identity.links[idx_2].source = getPrimaryKeyValues_profile2_identity_links_source(entity.profile2.identity.links[idx_2].source); } @@ -143,13 +143,13 @@ exports[`embedded entities in mongo diffing 1`] = ` }); } - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && (entity.profile2.identity.source == null || entity.profile2.identity.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && (entity.profile2.identity.source == null || entity.profile2.identity.source.__helper?.hasPrimaryKey())) { ret.profile2.identity.source = getPrimaryKeyValues_profile2_identity_source(entity.profile2.identity.source); } } - if (typeof entity.profile2 !== 'undefined' && (entity.profile2.source == null || entity.profile2.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && (entity.profile2.source == null || entity.profile2.source.__helper?.hasPrimaryKey())) { ret.profile2.source = getPrimaryKeyValues_profile2_source(entity.profile2.source); } diff --git a/tests/features/embeddables/__snapshots__/entities-in-embeddables.postgres.test.ts.snap b/tests/features/embeddables/__snapshots__/entities-in-embeddables.postgres.test.ts.snap index 2c514b56c6fd..94af6aa8fbfc 100644 --- a/tests/features/embeddables/__snapshots__/entities-in-embeddables.postgres.test.ts.snap +++ b/tests/features/embeddables/__snapshots__/entities-in-embeddables.postgres.test.ts.snap @@ -20,7 +20,7 @@ exports[`embedded entities in postgres diffing 1`] = ` if (entity.profile1.identity.meta != null) { ret.profile1_identity_meta_foo = clone(entity.profile1.identity.meta.foo); ret.profile1_identity_meta_bar = clone(entity.profile1.identity.meta.bar); - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.meta !== 'undefined' && (entity.profile1.identity.meta.source == null || entity.profile1.identity.meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.meta !== 'undefined' && (entity.profile1.identity.meta.source == null || entity.profile1.identity.meta.source.__helper?.hasPrimaryKey())) { ret.profile1_identity_meta_source = getPrimaryKeyValues_profile1_identity_meta_source(entity.profile1.identity.meta.source); } @@ -39,7 +39,7 @@ exports[`embedded entities in postgres diffing 1`] = ` ret.profile1_identity_links[idx_0].meta = {}; ret.profile1_identity_links[idx_0].meta.foo = clone(entity.profile1.identity.links[idx_0].meta.foo); ret.profile1_identity_links[idx_0].meta.bar = clone(entity.profile1.identity.links[idx_0].meta.bar); - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].meta !== 'undefined' && (entity.profile1.identity.links[idx_0].meta.source == null || entity.profile1.identity.links[idx_0].meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].meta !== 'undefined' && (entity.profile1.identity.links[idx_0].meta.source == null || entity.profile1.identity.links[idx_0].meta.source.__helper?.hasPrimaryKey())) { ret.profile1_identity_links[idx_0].meta.source = getPrimaryKeyValues_profile1_identity_links_meta_source(entity.profile1.identity.links[idx_0].meta.source); } @@ -53,7 +53,7 @@ exports[`embedded entities in postgres diffing 1`] = ` ret.profile1_identity_links[idx_0].metas[idx_1] = {}; ret.profile1_identity_links[idx_0].metas[idx_1].foo = clone(entity.profile1.identity.links[idx_0].metas[idx_1].foo); ret.profile1_identity_links[idx_0].metas[idx_1].bar = clone(entity.profile1.identity.links[idx_0].metas[idx_1].bar); - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].metas !== 'undefined' && (entity.profile1.identity.links[idx_0].metas[idx_1].source == null || entity.profile1.identity.links[idx_0].metas[idx_1].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && typeof entity.profile1.identity.links[idx_0].metas !== 'undefined' && (entity.profile1.identity.links[idx_0].metas[idx_1].source == null || entity.profile1.identity.links[idx_0].metas[idx_1].source.__helper?.hasPrimaryKey())) { ret.profile1_identity_links[idx_0].metas[idx_1].source = getPrimaryKeyValues_profile1_identity_links_metas_source(entity.profile1.identity.links[idx_0].metas[idx_1].source); } @@ -61,7 +61,7 @@ exports[`embedded entities in postgres diffing 1`] = ` }); } - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && (entity.profile1.identity.links[idx_0].source == null || entity.profile1.identity.links[idx_0].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && typeof entity.profile1.identity.links !== 'undefined' && (entity.profile1.identity.links[idx_0].source == null || entity.profile1.identity.links[idx_0].source.__helper?.hasPrimaryKey())) { ret.profile1_identity_links[idx_0].source = getPrimaryKeyValues_profile1_identity_links_source(entity.profile1.identity.links[idx_0].source); } @@ -70,13 +70,13 @@ exports[`embedded entities in postgres diffing 1`] = ` ret.profile1_identity_links = cloneEmbeddable(ret.profile1_identity_links); } - if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && (entity.profile1.identity.source == null || entity.profile1.identity.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && typeof entity.profile1.identity !== 'undefined' && (entity.profile1.identity.source == null || entity.profile1.identity.source.__helper?.hasPrimaryKey())) { ret.profile1_identity_source = getPrimaryKeyValues_profile1_identity_source(entity.profile1.identity.source); } } - if (typeof entity.profile1 !== 'undefined' && (entity.profile1.source == null || entity.profile1.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile1 !== 'undefined' && (entity.profile1.source == null || entity.profile1.source.__helper?.hasPrimaryKey())) { ret.profile1_source = getPrimaryKeyValues_profile1_source(entity.profile1.source); } @@ -94,7 +94,7 @@ exports[`embedded entities in postgres diffing 1`] = ` ret.profile2.identity.meta = {}; ret.profile2.identity.meta.foo = clone(entity.profile2.identity.meta.foo); ret.profile2.identity.meta.bar = clone(entity.profile2.identity.meta.bar); - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.meta !== 'undefined' && (entity.profile2.identity.meta.source == null || entity.profile2.identity.meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.meta !== 'undefined' && (entity.profile2.identity.meta.source == null || entity.profile2.identity.meta.source.__helper?.hasPrimaryKey())) { ret.profile2.identity.meta.source = getPrimaryKeyValues_profile2_identity_meta_source(entity.profile2.identity.meta.source); } @@ -113,7 +113,7 @@ exports[`embedded entities in postgres diffing 1`] = ` ret.profile2.identity.links[idx_2].meta = {}; ret.profile2.identity.links[idx_2].meta.foo = clone(entity.profile2.identity.links[idx_2].meta.foo); ret.profile2.identity.links[idx_2].meta.bar = clone(entity.profile2.identity.links[idx_2].meta.bar); - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].meta !== 'undefined' && (entity.profile2.identity.links[idx_2].meta.source == null || entity.profile2.identity.links[idx_2].meta.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].meta !== 'undefined' && (entity.profile2.identity.links[idx_2].meta.source == null || entity.profile2.identity.links[idx_2].meta.source.__helper?.hasPrimaryKey())) { ret.profile2.identity.links[idx_2].meta.source = getPrimaryKeyValues_profile2_identity_links_meta_source(entity.profile2.identity.links[idx_2].meta.source); } @@ -127,7 +127,7 @@ exports[`embedded entities in postgres diffing 1`] = ` ret.profile2.identity.links[idx_2].metas[idx_3] = {}; ret.profile2.identity.links[idx_2].metas[idx_3].foo = clone(entity.profile2.identity.links[idx_2].metas[idx_3].foo); ret.profile2.identity.links[idx_2].metas[idx_3].bar = clone(entity.profile2.identity.links[idx_2].metas[idx_3].bar); - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].metas !== 'undefined' && (entity.profile2.identity.links[idx_2].metas[idx_3].source == null || entity.profile2.identity.links[idx_2].metas[idx_3].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && typeof entity.profile2.identity.links[idx_2].metas !== 'undefined' && (entity.profile2.identity.links[idx_2].metas[idx_3].source == null || entity.profile2.identity.links[idx_2].metas[idx_3].source.__helper?.hasPrimaryKey())) { ret.profile2.identity.links[idx_2].metas[idx_3].source = getPrimaryKeyValues_profile2_identity_links_metas_source(entity.profile2.identity.links[idx_2].metas[idx_3].source); } @@ -135,7 +135,7 @@ exports[`embedded entities in postgres diffing 1`] = ` }); } - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && (entity.profile2.identity.links[idx_2].source == null || entity.profile2.identity.links[idx_2].source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && typeof entity.profile2.identity.links !== 'undefined' && (entity.profile2.identity.links[idx_2].source == null || entity.profile2.identity.links[idx_2].source.__helper?.hasPrimaryKey())) { ret.profile2.identity.links[idx_2].source = getPrimaryKeyValues_profile2_identity_links_source(entity.profile2.identity.links[idx_2].source); } @@ -143,13 +143,13 @@ exports[`embedded entities in postgres diffing 1`] = ` }); } - if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && (entity.profile2.identity.source == null || entity.profile2.identity.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && typeof entity.profile2.identity !== 'undefined' && (entity.profile2.identity.source == null || entity.profile2.identity.source.__helper?.hasPrimaryKey())) { ret.profile2.identity.source = getPrimaryKeyValues_profile2_identity_source(entity.profile2.identity.source); } } - if (typeof entity.profile2 !== 'undefined' && (entity.profile2.source == null || entity.profile2.source.__helper.hasPrimaryKey())) { + if (typeof entity.profile2 !== 'undefined' && (entity.profile2.source == null || entity.profile2.source.__helper?.hasPrimaryKey())) { ret.profile2.source = getPrimaryKeyValues_profile2_source(entity.profile2.source); } diff --git a/tests/issues/GHx1.test.ts b/tests/issues/GHx1.test.ts new file mode 100644 index 000000000000..20ecc7170b4b --- /dev/null +++ b/tests/issues/GHx1.test.ts @@ -0,0 +1,75 @@ +import { Entity, Enum, ManyToOne, MikroORM, PrimaryKey, Property } from '@mikro-orm/core'; + +enum TaskStatus { + OPENED = 'OPENED', + IN_PROGRESS = 'IN_PROGRESS', + COMPLETED = 'COMPLETED', + PAUSED = 'PAUSED', +} + +@Entity() +class Status { + + @Enum({ primary: true, length: 20, items: () => TaskStatus }) + name!: TaskStatus; + + @Property() + displayName!: string; + +} + +@Entity() +class Task { + + @PrimaryKey() + id!: number; + + @ManyToOne({ + entity: () => Status, + default: TaskStatus.OPENED, + }) + status!: Status; + +} + +test(`default value for relation property`, async () => { + const orm = await MikroORM.init({ + entities: [Task, Status], + type: 'sqlite', + dbName: ':memory:', + }); + await orm.getSchemaGenerator().refreshDatabase(); + + const status = new Status(); + status.name = TaskStatus.OPENED; + status.displayName = 'opened'; + await orm.em.persist(status).flush(); + const task = new Task(); + await orm.em.persist(task).flush(); + expect(task.status).toBeInstanceOf(Status); + const t1 = await orm.em.fork().findOneOrFail(Task, task); + expect(t1.status).toBeInstanceOf(Status); + + await orm.close(true); +}); + +test(`default value for relation property (postgres/returning)`, async () => { + const orm = await MikroORM.init({ + entities: [Task, Status], + type: 'postgresql', + dbName: 'mikro_orm_test_tmp', + }); + await orm.getSchemaGenerator().refreshDatabase(); + + const status = new Status(); + status.name = TaskStatus.OPENED; + status.displayName = 'opened'; + await orm.em.persist(status).flush(); + const task = new Task(); + await orm.em.persist(task).flush(); + expect(task.status).toBeInstanceOf(Status); + const t1 = await orm.em.fork().findOneOrFail(Task, task); + expect(t1.status).toBeInstanceOf(Status); + + await orm.close(true); +});