Преглед изворни кода

update descendantCount of ancestors

yohei0125 пре 4 година
родитељ
комит
8d5887b68e
2 измењених фајлова са 21 додато и 0 уклоњено
  1. 15 0
      packages/app/src/server/models/page.ts
  2. 6 0
      packages/app/src/server/service/page.js

+ 15 - 0
packages/app/src/server/models/page.ts

@@ -375,6 +375,21 @@ schema.statics.getAggrConditionForPageWithProvidedPathAndDescendants = function(
   ];
 };
 
+// update descendantCount of ancestors of path by count
+schema.statics.recountDescendantCountOfAncestors = async function(path:string, count: number):Promise<void> {
+  const paths = collectAncestorPaths(path);
+  const pages = await this.aggregate([{ $match: { path: { $in: paths } } }]);
+  const operations = pages.map((page) => {
+    return {
+      updateOne: {
+        filter: { path: page.path },
+        update: { descendantCount: page.descendantCount + count },
+      },
+    };
+  });
+  await this.bulkWrite(operations);
+};
+
 schema.statics.recountPage = async function(id:mongoose.Types.ObjectId):Promise<void> {
   const res = await this.aggregate(
     [

+ 6 - 0
packages/app/src/server/service/page.js

@@ -1290,6 +1290,12 @@ class PageService {
     await streamToPromise(recountWriteStream);
   }
 
+  // update descendantCount of all pages that are ancestors of path by passed count
+  async updateDescendantCountOfAncestors(path = '/', count = 0) {
+    const Page = this.crowi.model('Page');
+    await Page.recountDescendantCountOfAncestors(path, count);
+  }
+
 }
 
 module.exports = PageService;