Skip to content

Commit

Permalink
Adding skip if source collection is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
simonedeponti committed Nov 9, 2023
1 parent 3ee69b2 commit 6a2fa3e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,24 @@ class MongodbAnonymizer extends Command {
}

this.log("Anonymizing collection: " + collectionName);
this.log("Cleaning up target collection: " + collectionName);
await targetDb.collection(collectionName).deleteMany({});
const sourceCollection = await db
.collection(collectionName);
const targetCollection = await targetDb
.collection(collectionName);
const list = flags.list.split(",");
await this.anonymizeCollection(
sourceCollection,
targetCollection,
collectionName,
list
);
if((await sourceCollection.countDocuments()) > 0) {
this.log("Cleaning up target collection: " + collectionName);
await targetCollection.deleteMany({});
await this.anonymizeCollection(
sourceCollection,
targetCollection,
collectionName,
list
);
}
else {
this.log(`Skipping collection ${collectionName} as it's empty`);
}
}
this.log("Done!");

Expand Down

0 comments on commit 6a2fa3e

Please sign in to comment.