Skip to content

Commit 4a4a982

Browse files
refactor(deleteReferences): Change target to use $
1 parent 70820a2 commit 4a4a982

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

src/common.ts

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

21-
export function replaceReferenceWithMasterId(
22-
targetRef: string,
23-
masterId: string
24-
): string {
25-
return targetRef.replace('{masterId}', masterId);
26-
}
27-
2821
export function replaceReferenceWithFields(
2922
fields: FirebaseFirestore.DocumentData,
3023
targetCollection: string
3124
): { hasFields: boolean; targetCollection: string } {
32-
const pRegex = /\{([^)]+)\}/g;
33-
const matches = pRegex.exec(targetCollection);
25+
const pRegex = /([\$][^\/]*|$)/g;
26+
const matches = targetCollection.match(pRegex); // Using global flag always returns an empty string at the end
27+
matches.pop();
28+
3429
let hasFields = false;
35-
if (matches && fields) {
30+
if (matches.length > 0 && fields) {
3631
hasFields = true;
37-
matches.forEach(() => {
38-
const field = fields[matches[1]];
32+
matches.forEach(match => {
33+
const field = fields[match.replace('$', '')];
3934
if (field) {
4035
console.log(
41-
`integrify: Detected dynamic reference, replacing [${matches[0]}] with [${field}]`
36+
`integrify: Detected dynamic reference, replacing [${match}] with [${field}]`
4237
);
43-
targetCollection = targetCollection.replace(matches[0], field);
38+
targetCollection = targetCollection.replace(match, field);
4439
} else {
45-
throw new Error(
46-
`integrify: Missing dynamic reference: [${matches[0]}]`
47-
);
40+
throw new Error(`integrify: Missing dynamic reference: [${match}]`);
4841
}
4942
});
5043
}

src/rules/deleteReferences.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
Config,
3-
Rule,
4-
replaceReferenceWithMasterId,
5-
replaceReferenceWithFields,
6-
} from '../common';
1+
import { Config, Rule, replaceReferenceWithFields } from '../common';
72

83
export interface DeleteReferencesRule extends Rule {
94
source: {
@@ -61,18 +56,23 @@ export function integrifyDeleteReferences(
6156
}] matches [${masterId}]`
6257
);
6358

64-
// Replace masterId in target collection
65-
target.collection = replaceReferenceWithMasterId(
66-
target.collection,
67-
masterId
68-
);
59+
try {
60+
// Replace the context.params in the target collection
61+
const paramSwap = replaceReferenceWithFields(
62+
context.params,
63+
target.collection
64+
);
65+
target.collection = paramSwap.targetCollection;
6966

70-
// Replace the fields in the target collection
71-
const { hasFields, targetCollection } = replaceReferenceWithFields(
72-
snap.data(),
73-
target.collection
74-
);
75-
target.collection = targetCollection;
67+
// Replace the snapshot fields in the target collection
68+
const fieldSwap = replaceReferenceWithFields(
69+
snap.data(),
70+
target.collection
71+
);
72+
target.collection = fieldSwap.targetCollection;
73+
} catch (error) {
74+
throw new Error(error);
75+
}
7676

7777
// Delete all docs in this target corresponding to deleted master doc
7878
let whereable = null;

0 commit comments

Comments
 (0)