Skip to content

Commit

Permalink
fix(core): map embedded constructor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed May 21, 2024
1 parent 0837e0f commit 24f3ee6
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 74 deletions.
17 changes: 15 additions & 2 deletions packages/core/src/hydration/ObjectHydrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,31 @@ export class ObjectHydrator extends Hydrator {

ret.push(` if (${createCond(prop, dataKey).join(' || ')}) {`);

if (prop.object) {
ret.push(` const embeddedData = data${dataKey};`);
} else {
ret.push(` const embeddedData = {`);

for (const childProp of Object.values(prop.embeddedProps)) {
const key = childProp.embedded![1].match(/^\w+$/) ? childProp.embedded![1] : `'${childProp.embedded![1]}'`;
ret.push(` ${key}: data${this.wrap(childProp.name)},`);
}

ret.push(` };`);
}

if (prop.targetMeta?.polymorphs) {
prop.targetMeta.polymorphs!.forEach(meta => {
const childProp = prop.embeddedProps[prop.targetMeta!.discriminatorColumn!];
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded![1]) : this.wrap(childProp.name);
// weak comparison as we can have numbers that might have been converted to strings due to being object keys
ret.push(` if (data${childDataKey} == '${meta.discriminatorValue}' && entity${entityKey} == null) {`);
ret.push(` entity${entityKey} = factory.createEmbeddable('${meta.className}', data${prop.object ? dataKey : ''}, { newEntity, convertCustomTypes });`);
ret.push(` entity${entityKey} = factory.createEmbeddable('${meta.className}', embeddedData, { newEntity, convertCustomTypes });`);
ret.push(` }`);
});
} else {
ret.push(` if (entity${entityKey} == null) {`);
ret.push(` entity${entityKey} = factory.createEmbeddable('${prop.targetMeta!.className}', data${prop.object ? dataKey : ''}, { newEntity, convertCustomTypes });`);
ret.push(` entity${entityKey} = factory.createEmbeddable('${prop.targetMeta!.className}', embeddedData, { newEntity, convertCustomTypes });`);
ret.push(` }`);
}

Expand Down
Loading

0 comments on commit 24f3ee6

Please sign in to comment.