Explorar o código

devide module

Yuki Takei hai 1 ano
pai
achega
368d66bc8a

+ 1 - 0
apps/app/src/server/crowi/index.js

@@ -100,6 +100,7 @@ class Crowi {
 
     this.tokens = null;
 
+    /** @type {{ [modelName: string]: mongoose.Model }} */
     this.models = {};
 
     this.env = process.env;

+ 41 - 0
apps/app/src/server/service/import/construct-convert-map.ts

@@ -0,0 +1,41 @@
+import type Crowi from '~/server/crowi';
+
+import type { OverwriteFunction } from './overwrite-function';
+import { keepOriginal } from './overwrite-function';
+
+
+export type ConvertMap = {
+  [collectionName: string]: {
+    [propertyName: string]: OverwriteFunction,
+  }
+}
+
+/**
+ * initialize convert map. set keepOriginal as default
+ *
+ * @param {Crowi} crowi Crowi instance
+ */
+export const constructConvertMap = (crowi: Crowi): ConvertMap => {
+  const convertMap: ConvertMap = {};
+
+  const models = crowi.models;
+
+  // by default, original value is used for imported documents
+  for (const model of Object.values(models)) {
+    if (model.collection == null) {
+      continue;
+    }
+
+    const collectionName = model.collection.name;
+
+    console.log({ collectionName });
+
+    convertMap[collectionName] = {};
+
+    for (const key of Object.keys(model.schema.paths)) {
+      convertMap[collectionName][key] = keepOriginal;
+    }
+  }
+
+  return convertMap;
+};

+ 3 - 27
apps/app/src/server/service/import/import.js

@@ -18,6 +18,7 @@ import loggerFactory from '~/utils/logger';
 import CollectionProgressingStatus from '../../models/vo/collection-progressing-status';
 import { createBatchStream } from '../../util/batch-stream';
 
+import { constructConvertMap } from './construct-convert-map';
 import { keepOriginal } from './overwrite-function';
 
 const logger = loggerFactory('growi:services:ImportService'); // eslint-disable-line no-unused-vars
@@ -46,37 +47,12 @@ export class ImportService {
 
     this.adminEvent = crowi.event('admin');
 
-    // { pages: { _id: ..., path: ..., ...}, users: { _id: ..., username: ..., }, ... }
-    this.convertMap = {};
-    this.initConvertMap(crowi.models);
+    /** @type {import('./construct-convert-map').ConvertMap} */
+    this.convertMap = constructConvertMap(crowi);
 
     this.currentProgressingStatus = null;
   }
 
-  /**
-   * initialize convert map. set keepOriginal as default
-   *
-   * @memberOf ImportService
-   * @param {object} models from models/index.js
-   */
-  initConvertMap(models) {
-    // by default, original value is used for imported documents
-    for (const model of Object.values(models)) {
-      if (model.collection == null) {
-        continue;
-      }
-
-      const collectionName = model.collection.name;
-
-      console.log({ collectionName });
-      this.convertMap[collectionName] = {};
-
-      for (const key of Object.keys(model.schema.paths)) {
-        this.convertMap[collectionName][key] = keepOriginal;
-      }
-    }
-  }
-
   /**
    * parse all zip files in downloads dir
    *