Skip to content

Commit 2ed10ac

Browse files
feat(ReplicateAttributes): Delete field not set by setting flag
1 parent c179f30 commit 2ed10ac

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/rules/replicateAttributes.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Config, Rule } from '../common';
2+
import { firestore } from 'firebase-admin';
3+
const FieldValue = firestore.FieldValue;
24

35
export interface ReplicateAttributesRule extends Rule {
46
source: {
@@ -11,6 +13,7 @@ export interface ReplicateAttributesRule extends Rule {
1113
[sourceAttribute: string]: string;
1214
};
1315
isCollectionGroup?: boolean;
16+
deleteMissing?: boolean;
1417
}[];
1518
hooks?: {
1619
pre?: Function;
@@ -80,19 +83,30 @@ export function integrifyReplicateAttributes(
8083
return null;
8184
}
8285

83-
// Loop over each target specification to replicate atributes
86+
// Loop over each target specification to replicate attributes
8487
const db = config.config.db;
8588
rule.targets.forEach(target => {
8689
const targetCollection = target.collection;
8790
const update = {};
91+
let shouldDelete = false;
8892

89-
// Create "update" mapping each changed attribute from source => target
90-
Object.keys(newValue).forEach(changedAttribute => {
91-
if (target.attributeMapping[changedAttribute]) {
93+
if (target.deleteMissing) {
94+
shouldDelete = target.deleteMissing;
95+
}
96+
97+
// Create "update" mapping each changed attribute from source => target,
98+
// if delete is set delete field
99+
Object.keys(target.attributeMapping).forEach(changedAttribute => {
100+
if (newValue[changedAttribute]) {
92101
update[target.attributeMapping[changedAttribute]] =
93102
newValue[changedAttribute];
103+
} else if (shouldDelete) {
104+
update[
105+
target.attributeMapping[changedAttribute]
106+
] = FieldValue.delete();
94107
}
95108
});
109+
96110
console.log(
97111
`integrify: On collection ${
98112
target.isCollectionGroup ? 'group ' : ''

0 commit comments

Comments
 (0)