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

add type for crowi and get mongoose model before queries

Futa Arai 1 год назад
Родитель
Сommit
96d2dc1377

BIN
apps/app/.swc/plugins/v7_linux_aarch64_0.106.16/85face98bcf0ea217842cb93383692497c6ae8a7da71a76bf3d93a3a42b4228c


+ 8 - 14
apps/app/src/features/page-bulk-export/server/service/page-bulk-export-job-cron/index.ts

@@ -1,16 +1,16 @@
 import fs from 'fs';
 import type { Readable } from 'stream';
 
-import type { IPage, IUser } from '@growi/core';
+import type { IUser } from '@growi/core';
 import { isPopulated, getIdForRef } from '@growi/core';
 import mongoose from 'mongoose';
 
 
 import type { SupportedActionType } from '~/interfaces/activity';
 import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity';
+import type Crowi from '~/server/crowi';
 import type { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
 import type { ActivityDocument } from '~/server/models/activity';
-import type { PageModel } from '~/server/models/page';
 import { configManager } from '~/server/service/config-manager';
 import CronService from '~/server/service/cron';
 import type { FileUploader } from '~/server/service/file-uploader';
@@ -32,8 +32,7 @@ import { exportPagesToFsAsync } from './steps/export-pages-to-fs-async';
 const logger = loggerFactory('growi:service:page-bulk-export-job-cron');
 
 export interface IPageBulkExportJobCronService {
-  crowi: any;
-  pageModel: PageModel;
+  crowi: Crowi;
   pageBatchSize: number;
   maxPartSize: number;
   compressExtension: string;
@@ -49,7 +48,7 @@ export interface IPageBulkExportJobCronService {
  */
 class PageBulkExportJobCronService extends CronService implements IPageBulkExportJobCronService {
 
-  crowi: any;
+  crowi: Crowi;
 
   activityEvent: any;
 
@@ -64,10 +63,6 @@ class PageBulkExportJobCronService extends CronService implements IPageBulkExpor
   // TODO: If necessary, change to a proper path in https://redmine.weseek.co.jp/issues/149512
   tmpOutputRootDir = '/tmp/page-bulk-export';
 
-  pageModel: PageModel;
-
-  userModel: mongoose.Model<IUser>;
-
   // Keep track of the stream executed for PageBulkExportJob to destroy it on job failure.
   // The key is the id of a PageBulkExportJob.
   private streamInExecutionMemo: {
@@ -76,12 +71,10 @@ class PageBulkExportJobCronService extends CronService implements IPageBulkExpor
 
   private parallelExecLimit: number;
 
-  constructor(crowi) {
+  constructor(crowi: Crowi) {
     super();
     this.crowi = crowi;
     this.activityEvent = crowi.event('activity');
-    this.pageModel = mongoose.model<IPage, PageModel>('Page');
-    this.userModel = mongoose.model<IUser>('User');
     this.parallelExecLimit = configManager.getConfig('crowi', 'app:pageBulkExportParallelExecLimit');
   }
 
@@ -149,8 +142,9 @@ class PageBulkExportJobCronService extends CronService implements IPageBulkExpor
     if (pageBulkExportJob.status === pageBulkExportJob.statusOnPreviousCronExec) {
       return;
     }
+    const User = mongoose.model<IUser>('User');
     try {
-      const user = await this.userModel.findById(getIdForRef(pageBulkExportJob.user));
+      const user = await User.findById(getIdForRef(pageBulkExportJob.user));
 
       // update statusOnPreviousCronExec before starting processes that updates status
       pageBulkExportJob.statusOnPreviousCronExec = pageBulkExportJob.status;
@@ -272,6 +266,6 @@ class PageBulkExportJobCronService extends CronService implements IPageBulkExpor
 
 // eslint-disable-next-line import/no-mutable-exports
 export let pageBulkExportJobCronService: PageBulkExportJobCronService | undefined; // singleton instance
-export default function instanciate(crowi): void {
+export default function instanciate(crowi: Crowi): void {
   pageBulkExportJobCronService = new PageBulkExportJobCronService(crowi);
 }

+ 8 - 4
apps/app/src/features/page-bulk-export/server/service/page-bulk-export-job-cron/steps/create-page-snapshots-async.ts

@@ -2,10 +2,12 @@ import { createHash } from 'crypto';
 import { Writable, pipeline } from 'stream';
 
 import { getIdForRef, getIdStringForRef } from '@growi/core';
+import type { IPage } from '@growi/core';
+import mongoose from 'mongoose';
 
 import { PageBulkExportJobStatus } from '~/features/page-bulk-export/interfaces/page-bulk-export';
 import { SupportedAction } from '~/interfaces/activity';
-import type { PageDocument } from '~/server/models/page';
+import type { PageDocument, PageModel } from '~/server/models/page';
 
 import type { IPageBulkExportJobCronService } from '..';
 import type { PageBulkExportJobDocument } from '../../../models/page-bulk-export-job';
@@ -35,10 +37,12 @@ async function reuseDuplicateExportIfExists(this: IPageBulkExportJobCronService,
  * 'revisionListHash' is calulated and saved to the pageBulkExportJob at the end of the pipeline.
  */
 export async function createPageSnapshotsAsync(this: IPageBulkExportJobCronService, user, pageBulkExportJob: PageBulkExportJobDocument): Promise<void> {
+  const Page = mongoose.model<IPage, PageModel>('Page');
+
   // if the process of creating snapshots was interrupted, delete the snapshots and create from the start
   await PageBulkExportPageSnapshot.deleteMany({ pageBulkExportJob });
 
-  const basePage = await this.pageModel.findById(getIdForRef(pageBulkExportJob.page));
+  const basePage = await Page.findById(getIdForRef(pageBulkExportJob.page));
   if (basePage == null) {
     throw new Error('Base page not found');
   }
@@ -46,8 +50,8 @@ export async function createPageSnapshotsAsync(this: IPageBulkExportJobCronServi
   const revisionListHash = createHash('sha256');
 
   // create a Readable for pages to be exported
-  const { PageQueryBuilder } = this.pageModel;
-  const builder = await new PageQueryBuilder(this.pageModel.find())
+  const { PageQueryBuilder } = Page;
+  const builder = await new PageQueryBuilder(Page.find())
     .addConditionToListWithDescendants(basePage.path)
     .addViewerCondition(user);
   const pagesReadable = builder