Skip to content

Commit 2c0c747

Browse files
feat(deleteReferences): Add deleteAll flag
1 parent e4b47aa commit 2c0c747

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

src/common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export function isConfig(arg: Rule | Config): arg is Config {
1818
return (arg as Config).config !== undefined;
1919
}
2020

21+
export function isSubCollection(collection: string): boolean {
22+
const filteredCollections = collection.split('/').filter(c => c.length > 0);
23+
const numCollections = filteredCollections.length;
24+
// Return false is equal to 1 or an even number
25+
return numCollections !== 1 && numCollections % 2 === 1;
26+
}
27+
2128
enum Key {
2229
Primary = '{(.*?)}',
2330
Foreign = '([$][^/]*|$)',

src/rules/deleteReferences.ts

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import { Config, Rule, replaceReferencesWith, getPrimaryKey } from '../common';
1+
import {
2+
Config,
3+
Rule,
4+
replaceReferencesWith,
5+
getPrimaryKey,
6+
isSubCollection,
7+
} from '../common';
28

39
export interface DeleteReferencesRule extends Rule {
410
source: {
511
collection: string;
612
};
713
targets: {
814
collection: string;
9-
foreignKey: string;
15+
foreignKey?: string;
1016
isCollectionGroup?: boolean;
17+
deleteAll?: boolean;
1118
}[];
1219
hooks?: {
1320
pre?: Function;
@@ -60,13 +67,24 @@ export function integrifyDeleteReferences(
6067
// Loop over each target
6168
const db = config.config.db;
6269
rule.targets.forEach(target => {
63-
console.log(
64-
`integrify: Deleting all docs in collection ${
65-
target.isCollectionGroup ? 'group ' : ''
66-
}[${target.collection}] where foreign key [${
67-
target.foreignKey
68-
}] matches [${primaryKeyValue}]`
69-
);
70+
// Check delete all flag
71+
if (!target.deleteAll) {
72+
target.deleteAll = false;
73+
}
74+
// CHeck the foreign key
75+
if (!target.foreignKey) {
76+
target.foreignKey = '';
77+
}
78+
79+
if (!target.deleteAll && target.foreignKey.trim().length === 0) {
80+
throw new Error(
81+
`integrify: missing foreign key or set deleteAll to true`
82+
);
83+
} else if (target.deleteAll && !isSubCollection(target.collection)) {
84+
throw new Error(
85+
`integrify: [${target.collection}] is an invalid sub-collection`
86+
);
87+
}
7088

7189
// Replace the context.params in the target collection
7290
const paramSwap = replaceReferencesWith(
@@ -89,20 +107,33 @@ export function integrifyDeleteReferences(
89107
whereable = db.collection(target.collection);
90108
}
91109

110+
if (target.deleteAll) {
111+
console.log(
112+
`integrify: Deleting all docs in sub-collection [${target.collection}]`
113+
);
114+
} else {
115+
console.log(
116+
`integrify: Deleting all docs in collection ${
117+
target.isCollectionGroup ? 'group ' : ''
118+
}[${target.collection}] where foreign key [${
119+
target.foreignKey
120+
}] matches [${primaryKeyValue}]`
121+
);
122+
123+
whereable = whereable.where(target.foreignKey, '==', primaryKeyValue);
124+
}
125+
92126
promises.push(
93-
whereable
94-
.where(target.foreignKey, '==', primaryKeyValue)
95-
.get()
96-
.then(querySnap => {
97-
querySnap.forEach(doc => {
98-
console.log(
99-
`integrify: Deleting [${target.collection}]${
100-
target.isCollectionGroup ? ' (group)' : ''
101-
}, id [${doc.id}]`
102-
);
103-
promises.push(doc.ref.delete());
104-
});
105-
})
127+
whereable.get().then(querySnap => {
128+
querySnap.forEach(doc => {
129+
console.log(
130+
`integrify: Deleting [${target.collection}]${
131+
target.isCollectionGroup ? ' (group)' : ''
132+
}, id [${doc.id}]`
133+
);
134+
promises.push(doc.ref.delete());
135+
});
136+
})
106137
);
107138
});
108139
return Promise.all(promises);

0 commit comments

Comments
 (0)