Просмотр исходного кода

Add isSynchronously option to create method

Taichi Masuyama 3 лет назад
Родитель
Сommit
1d727a58b3

+ 1 - 0
packages/app/src/server/models/interfaces/page-operation.ts

@@ -33,6 +33,7 @@ export type IOptionsForCreate = {
   grantUserGroupId?: ObjectIdLike,
   grant?: PageGrant,
   overwriteScopesOfDescendants?: boolean,
+  isSynchronously?: boolean,
 };
 
 export type IOptionsForResuming = {

+ 1 - 1
packages/app/src/server/service/installer.ts

@@ -52,7 +52,7 @@ export class InstallerService {
   private async createPage(filePath, pagePath, owner): Promise<IPage|undefined> {
     try {
       const markdown = fs.readFileSync(filePath);
-      return this.crowi.pageService.create(pagePath, markdown, owner, {}) as IPage;
+      return this.crowi.pageService.create(pagePath, markdown, owner, { isSynchronously: true }) as IPage;
     }
     catch (err) {
       logger.error(`Failed to create ${pagePath}`, err);

+ 10 - 1
packages/app/src/server/service/page.ts

@@ -3486,6 +3486,10 @@ class PageService {
     return true;
   }
 
+  /**
+   * Create a page
+   * Set options.isSynchronously to true to await all process when you want to run this method multiple times at short intervals.
+   */
   async create(path: string, body: string, user, options: IOptionsForCreate = {}): Promise<PageDocument> {
     const Page = mongoose.model('Page') as unknown as PageModel;
 
@@ -3564,7 +3568,12 @@ class PageService {
       throw err;
     }
 
-    this.createSubOperation(savedPage, user, options, pageOp._id);
+    if (options.isSynchronously) {
+      await this.createSubOperation(savedPage, user, options, pageOp._id);
+    }
+    else {
+      this.createSubOperation(savedPage, user, options, pageOp._id);
+    }
 
     return savedPage;
   }