Skip to content

Commit 4f2d677

Browse files
fix(replicateAttributes): Always delete missing fields
1 parent e3340ce commit 4f2d677

File tree

2 files changed

+3
-15
lines changed

2 files changed

+3
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports.replicateMasterToDetail = integrify({
3636
collection: 'detail1',
3737
foreignKey: 'masterId',
3838
attributeMapping: {
39-
masterField1: 'detail1Field1',
39+
masterField1: 'detail1Field1', // If an field is missing after the update, the field will be deleted
4040
masterField2: 'detail1Field2',
4141
},
4242
},

src/rules/replicateAttributes.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export interface ReplicateAttributesRule extends Rule {
1313
[sourceAttribute: string]: string;
1414
};
1515
isCollectionGroup?: boolean;
16-
deleteMissing?: boolean;
1716
}[];
1817
hooks?: {
1918
pre?: Function;
@@ -99,23 +98,12 @@ export function integrifyReplicateAttributes(
9998
rule.targets.forEach(target => {
10099
const targetCollection = target.collection;
101100
const update = {};
102-
let shouldDelete = false;
103-
104-
if (target.deleteMissing) {
105-
shouldDelete = target.deleteMissing;
106-
}
107101

108102
// Create "update" mapping each changed attribute from source => target,
109103
// if delete is set delete field
110104
Object.keys(target.attributeMapping).forEach(changedAttribute => {
111-
if (newValue[changedAttribute]) {
112-
update[target.attributeMapping[changedAttribute]] =
113-
newValue[changedAttribute];
114-
} else if (shouldDelete) {
115-
update[
116-
target.attributeMapping[changedAttribute]
117-
] = FieldValue.delete();
118-
}
105+
update[target.attributeMapping[changedAttribute]] =
106+
newValue[changedAttribute] || FieldValue.delete();
119107
});
120108

121109
console.log(

0 commit comments

Comments
 (0)