Skip to content

Commit

Permalink
fix(core): do not snapshot missing embedded properties as undefined
Browse files Browse the repository at this point in the history
This fixes extra update queries being produced by populating relations with embeddables.
  • Loading branch information
B4nan committed Feb 9, 2023
1 parent ee363a5 commit 4ad4cdf
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 98 deletions.
7 changes: 4 additions & 3 deletions packages/core/src/utils/EntityComparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ export class EntityComparator {
ret += meta.props.filter(p => p.embedded?.[0] === prop.name).map(childProp => {
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded![1]) : this.wrap(childProp.name);
const childEntityKey = [...path, childProp.embedded![1]].map(k => this.wrap(k)).join('');
const childCond = `typeof entity${childEntityKey} !== 'undefined'`;

if (childProp.reference === ReferenceType.EMBEDDED) {
return this.getPropertySnapshot(meta, childProp, context, childDataKey, childEntityKey, [...path, childProp.embedded![1]], level + 1, prop.object);
Expand All @@ -416,13 +417,13 @@ export class EntityComparator {
context.set(`convertToDatabaseValue_${convertorKey}`, (val: any) => childProp.customType.convertToDatabaseValue(val, this.platform, { mode: 'serialization' }));

if (['number', 'string', 'boolean'].includes(childProp.customType.compareAsType().toLowerCase())) {
return `${padding} ret${childDataKey} = convertToDatabaseValue_${convertorKey}(entity${childEntityKey});`;
return `${padding} if (${childCond}) ret${childDataKey} = convertToDatabaseValue_${convertorKey}(entity${childEntityKey});`;
}

return `${padding} ret${childDataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${childEntityKey}));`;
return `${padding} if (${childCond}) ret${childDataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${childEntityKey}));`;
}

return `${padding} ret${childDataKey} = clone(entity${childEntityKey});`;
return `${padding} if (${childCond}) ret${childDataKey} = clone(entity${childEntityKey});`;
}).join('\n') + `\n`;

if (this.shouldSerialize(prop, dataKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ exports[`embedded entities with custom types snapshot generator 1`] = `
}
if (entity.nested != null) {
ret.nested_someValue = clone(convertToDatabaseValue_nested_someValue(entity.nested.someValue));
if (typeof entity.nested.someValue !== 'undefined') ret.nested_someValue = clone(convertToDatabaseValue_nested_someValue(entity.nested.someValue));
if (entity.nested.deep != null) {
ret.nested_deep_someValue = clone(convertToDatabaseValue_nested_deep_someValue(entity.nested.deep.someValue));
if (typeof entity.nested.deep.someValue !== 'undefined') ret.nested_deep_someValue = clone(convertToDatabaseValue_nested_deep_someValue(entity.nested.deep.someValue));
}
}
if (entity.nested2 === null) ret.nested2 = null;
if (entity.nested2 != null) {
ret.nested2 = {};
ret.nested2.someValue = clone(convertToDatabaseValue_nested2_someValue(entity.nested2.someValue));
if (typeof entity.nested2.someValue !== 'undefined') ret.nested2.someValue = clone(convertToDatabaseValue_nested2_someValue(entity.nested2.someValue));
if (entity.nested2.deep === null) ret.nested2.deep = null;
if (entity.nested2.deep != null) {
ret.nested2.deep = {};
ret.nested2.deep.someValue = clone(convertToDatabaseValue_nested2_deep_someValue(entity.nested2.deep.someValue));
if (typeof entity.nested2.deep.someValue !== 'undefined') ret.nested2.deep.someValue = clone(convertToDatabaseValue_nested2_deep_someValue(entity.nested2.deep.someValue));
}
ret.nested2 = cloneEmbeddable(ret.nested2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`embedded entities in postgresql schema: embeddables 1 1`] = `
"create table "user" ("id" serial primary key, "email" varchar(255) not null, "address1_street" varchar(255) not null, "address1_number" int not null, "address1_rank" real null, "address1_postal_code" varchar(255) not null, "address1_city" varchar(255) not null, "address1_country" varchar(255) not null, "addr_street" varchar(255) null, "addr_postal_code" varchar(255) null, "addr_city" varchar(255) null, "addr_country" varchar(255) null, "street" varchar(255) not null, "number" int not null, "rank" real null, "postal_code" varchar(255) not null, "city" varchar(255) not null, "country" varchar(255) not null, "address4" jsonb not null, "addresses" jsonb not null, "after" int null);
"create table "user" ("id" serial primary key, "email" varchar(255) not null, "address1_street" varchar(255) null, "address1_number" int not null, "address1_rank" real null, "address1_postal_code" varchar(255) null, "address1_city" varchar(255) null, "address1_country" varchar(255) null, "addr_street" varchar(255) null, "addr_postal_code" varchar(255) null, "addr_city" varchar(255) null, "addr_country" varchar(255) null, "street" varchar(255) null, "number" int not null, "rank" real null, "postal_code" varchar(255) null, "city" varchar(255) null, "country" varchar(255) null, "address4" jsonb not null, "addresses" jsonb not null, "after" int null);
alter table "user" add constraint "user_email_unique" unique ("email");
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ exports[`embedded entities in mongo diffing 1`] = `
}
if (entity.profile1 != null) {
ret.profile1_username = clone(entity.profile1.username);
if (typeof entity.profile1.username !== 'undefined') ret.profile1_username = clone(entity.profile1.username);
if (entity.profile1.identity != null) {
ret.profile1_identity_email = clone(entity.profile1.identity.email);
if (typeof entity.profile1.identity.email !== 'undefined') ret.profile1_identity_email = clone(entity.profile1.identity.email);
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.identity.meta.foo !== 'undefined') ret.profile1_identity_meta_foo = clone(entity.profile1.identity.meta.foo);
if (typeof entity.profile1.identity.meta.bar !== 'undefined') 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())) {
ret.profile1_identity_meta_source = getPrimaryKeyValues_profile1_identity_meta_source(entity.profile1.identity.meta.source);
}
Expand All @@ -33,14 +33,14 @@ exports[`embedded entities in mongo diffing 1`] = `
if (entity.profile1.identity.links[idx_0] === null) ret.profile1_identity_links[idx_0] = null;
if (entity.profile1.identity.links[idx_0] != null) {
ret.profile1_identity_links[idx_0] = {};
ret.profile1_identity_links[idx_0].url = clone(entity.profile1.identity.links[idx_0].url);
ret.profile1_identity_links[idx_0].createdAt = clone(entity.profile1.identity.links[idx_0].createdAt);
if (typeof entity.profile1.identity.links[idx_0].url !== 'undefined') ret.profile1_identity_links[idx_0].url = clone(entity.profile1.identity.links[idx_0].url);
if (typeof entity.profile1.identity.links[idx_0].createdAt !== 'undefined') ret.profile1_identity_links[idx_0].createdAt = clone(entity.profile1.identity.links[idx_0].createdAt);
if (entity.profile1.identity.links[idx_0].meta === null) ret.profile1_identity_links[idx_0].meta = null;
if (entity.profile1.identity.links[idx_0].meta != null) {
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.identity.links[idx_0].meta.foo !== 'undefined') ret.profile1_identity_links[idx_0].meta.foo = clone(entity.profile1.identity.links[idx_0].meta.foo);
if (typeof entity.profile1.identity.links[idx_0].meta.bar !== 'undefined') 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())) {
ret.profile1_identity_links[idx_0].meta.source = getPrimaryKeyValues_profile1_identity_links_meta_source(entity.profile1.identity.links[idx_0].meta.source);
}
Expand All @@ -54,8 +54,8 @@ exports[`embedded entities in mongo diffing 1`] = `
if (entity.profile1.identity.links[idx_0].metas[idx_1] === null) ret.profile1_identity_links[idx_0].metas[idx_1] = null;
if (entity.profile1.identity.links[idx_0].metas[idx_1] != null) {
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.identity.links[idx_0].metas[idx_1].foo !== 'undefined') ret.profile1_identity_links[idx_0].metas[idx_1].foo = clone(entity.profile1.identity.links[idx_0].metas[idx_1].foo);
if (typeof entity.profile1.identity.links[idx_0].metas[idx_1].bar !== 'undefined') 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())) {
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);
}
Expand Down Expand Up @@ -88,18 +88,18 @@ exports[`embedded entities in mongo diffing 1`] = `
if (entity.profile2 === null) ret.profile2 = null;
if (entity.profile2 != null) {
ret.profile2 = {};
ret.profile2.username = clone(entity.profile2.username);
if (typeof entity.profile2.username !== 'undefined') ret.profile2.username = clone(entity.profile2.username);
if (entity.profile2.identity === null) ret.profile2.identity = null;
if (entity.profile2.identity != null) {
ret.profile2.identity = {};
ret.profile2.identity.email = clone(entity.profile2.identity.email);
if (typeof entity.profile2.identity.email !== 'undefined') ret.profile2.identity.email = clone(entity.profile2.identity.email);
if (entity.profile2.identity.meta === null) ret.profile2.identity.meta = null;
if (entity.profile2.identity.meta != null) {
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.identity.meta.foo !== 'undefined') ret.profile2.identity.meta.foo = clone(entity.profile2.identity.meta.foo);
if (typeof entity.profile2.identity.meta.bar !== 'undefined') 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())) {
ret.profile2.identity.meta.source = getPrimaryKeyValues_profile2_identity_meta_source(entity.profile2.identity.meta.source);
}
Expand All @@ -113,14 +113,14 @@ exports[`embedded entities in mongo diffing 1`] = `
if (entity.profile2.identity.links[idx_2] === null) ret.profile2.identity.links[idx_2] = null;
if (entity.profile2.identity.links[idx_2] != null) {
ret.profile2.identity.links[idx_2] = {};
ret.profile2.identity.links[idx_2].url = clone(entity.profile2.identity.links[idx_2].url);
ret.profile2.identity.links[idx_2].createdAt = clone(entity.profile2.identity.links[idx_2].createdAt);
if (typeof entity.profile2.identity.links[idx_2].url !== 'undefined') ret.profile2.identity.links[idx_2].url = clone(entity.profile2.identity.links[idx_2].url);
if (typeof entity.profile2.identity.links[idx_2].createdAt !== 'undefined') ret.profile2.identity.links[idx_2].createdAt = clone(entity.profile2.identity.links[idx_2].createdAt);
if (entity.profile2.identity.links[idx_2].meta === null) ret.profile2.identity.links[idx_2].meta = null;
if (entity.profile2.identity.links[idx_2].meta != null) {
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.identity.links[idx_2].meta.foo !== 'undefined') ret.profile2.identity.links[idx_2].meta.foo = clone(entity.profile2.identity.links[idx_2].meta.foo);
if (typeof entity.profile2.identity.links[idx_2].meta.bar !== 'undefined') 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())) {
ret.profile2.identity.links[idx_2].meta.source = getPrimaryKeyValues_profile2_identity_links_meta_source(entity.profile2.identity.links[idx_2].meta.source);
}
Expand All @@ -134,8 +134,8 @@ exports[`embedded entities in mongo diffing 1`] = `
if (entity.profile2.identity.links[idx_2].metas[idx_3] === null) ret.profile2.identity.links[idx_2].metas[idx_3] = null;
if (entity.profile2.identity.links[idx_2].metas[idx_3] != null) {
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.identity.links[idx_2].metas[idx_3].foo !== 'undefined') ret.profile2.identity.links[idx_2].metas[idx_3].foo = clone(entity.profile2.identity.links[idx_2].metas[idx_3].foo);
if (typeof entity.profile2.identity.links[idx_2].metas[idx_3].bar !== 'undefined') 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())) {
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);
}
Expand Down

0 comments on commit 4ad4cdf

Please sign in to comment.