Skip to content

Commit

Permalink
Merge pull request #21 from GitLiveApp/fix-target-collection-no-updating
Browse files Browse the repository at this point in the history
Fix target collection no updating
  • Loading branch information
nbransby authored Mar 18, 2021
2 parents bfc655e + ceba6da commit ce102cf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "integrify",
"version": "4.0.3",
"version": "5.0.0",
"description": "Enforce referential integrity in Firestore using Cloud Functions",
"keywords": [
"firebase",
Expand Down
14 changes: 6 additions & 8 deletions src/rules/deleteReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export function integrifyDeleteReferences(
// Call "pre" hook if defined
if (rule.hooks && rule.hooks.pre) {
await rule.hooks.pre(snap, context);
// logger.debug(`integrify: Running pre-hook: ${rule.hooks.pre}`);
}

// Loop over each target
Expand All @@ -93,25 +92,25 @@ export function integrifyDeleteReferences(
{ source: snap.data() || {}, ...context.params },
target.collection
);
target.collection = fieldSwap.targetCollection;
const targetCollection = fieldSwap.targetCollection;

// Delete all docs in this target corresponding to deleted master doc
let whereable: FirebaseFirestore.Query<FirebaseFirestore.DocumentData> = null;
if (target.isCollectionGroup) {
whereable = db.collectionGroup(target.collection);
whereable = db.collectionGroup(targetCollection);
} else {
whereable = db.collection(target.collection);
whereable = db.collection(targetCollection);
}

if (target.deleteAll) {
logger.debug(
`integrify: Deleting all docs in sub-collection [${target.collection}]`
`integrify: Deleting all docs in sub-collection [${targetCollection}]`
);
} else {
logger.debug(
`integrify: Deleting all docs in collection ${
target.isCollectionGroup ? 'group ' : ''
}[${target.collection}] where foreign key [${
}[${targetCollection}] where foreign key [${
target.foreignKey
}] matches [${primaryKeyValue}]`
);
Expand All @@ -123,7 +122,7 @@ export function integrifyDeleteReferences(
const querySnap = await whereable.get();
for (const doc of querySnap.docs) {
logger.debug(
`integrify: Deleting [${target.collection}]${
`integrify: Deleting [${targetCollection}]${
target.isCollectionGroup ? ' (group)' : ''
}, id [${doc.id}]`
);
Expand All @@ -135,7 +134,6 @@ export function integrifyDeleteReferences(
// Call "post" hook if defined
if (rule.hooks && rule.hooks.post) {
await rule.hooks.post(snap, context);
// logger.debug(`integrify: Running post-hook: ${rule.hooks.post}`);
}
});
}
5 changes: 3 additions & 2 deletions src/rules/maintainCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ export function integrifyMaintainCount(
rule.target.collection,
rule?.hooks?.pre
);
const targetCollection = fieldSwap.targetCollection;

// For maintain it must reference a doc
const targetRef = db.doc(fieldSwap.targetCollection);
const targetRef = db.doc(targetCollection);
const targetSnap = await targetRef.get();

// No-op if target does not exist
if (!targetSnap.exists) {
logger.debug(
`integrify: WARNING: Target document does not exist in [${fieldSwap.targetCollection}]`
`integrify: WARNING: Target document does not exist in [${targetCollection}]`
);
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/rules/replicateAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export function integrifyReplicateAttributes(
// Call "pre" hook if defined
if (rule.hooks && rule.hooks.pre) {
await rule.hooks.pre(change, context);
// logger.debug(`integrify: Running pre-hook: ${rule.hooks.pre}`);
}

// Check if at least one of the attributes to be replicated was changed
Expand Down

0 comments on commit ce102cf

Please sign in to comment.