collection-progressing-status.js 978 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const mongoose = require('mongoose');
  2. const CollectionProgress = require('./collection-progress');
  3. class CollectionProgressingStatus {
  4. constructor() {
  5. this.totalCount = 0;
  6. this.progressList = null;
  7. this.progressMap = {};
  8. }
  9. async init(collections) {
  10. const promisesForCreatingInstance = collections.map(async(collectionName) => {
  11. const collection = mongoose.connection.collection(collectionName);
  12. const totalCount = await collection.count();
  13. return new CollectionProgress(collectionName, totalCount);
  14. });
  15. this.progressList = await Promise.all(promisesForCreatingInstance);
  16. // collection name to instance mapping
  17. this.progressList.forEach((p) => {
  18. this.progressMap[p.collectionName] = p;
  19. this.totalCount += p.totalCount;
  20. });
  21. }
  22. get currentCount() {
  23. return this.progressList.reduce(
  24. (acc, crr) => acc + crr.currentCount,
  25. 0,
  26. );
  27. }
  28. }
  29. module.exports = CollectionProgressingStatus;