Skip to content

Commit

Permalink
fix(core): fix mapping default values of relation properties
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Feb 21, 2022
1 parent fff1581 commit bc57ed0
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 43 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/EntityManager.ts
Expand Up @@ -29,8 +29,8 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
private readonly repositoryMap: Dictionary<EntityRepository<AnyEntity>> = {};
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<FilterDef> = {};
private filterParams: Dictionary<Dictionary> = {};
Expand Down Expand Up @@ -835,7 +835,11 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
/**
* 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;
}

Expand Down
8 changes: 5 additions & 3 deletions 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';
Expand All @@ -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<T extends AnyEntity<T>, P extends string = string>(entityName: EntityName<T>, data: EntityData<T>, options: FactoryOptions = {}): New<T, P> {
data = Reference.unwrapReference(data);
Expand Down Expand Up @@ -303,4 +301,8 @@ export class EntityFactory {
});
}

private get unitOfWork() {
return this.em.getUnitOfWork(false);
}

}
14 changes: 1 addition & 13 deletions packages/core/src/unit-of-work/ChangeSetPersister.ts
Expand Up @@ -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<T>(changeSet.entity, meta, data as EntityData<T>, this.factory, 'returning', false, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/EntityComparator.ts
Expand Up @@ -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;
Expand Down
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -53,15 +53,15 @@ 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);
}
}
});
}
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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -127,29 +127,29 @@ 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);
}
}
});
}
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);
}
}
});
}
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);
}
Expand Down

0 comments on commit bc57ed0

Please sign in to comment.