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

devide getModelFromCollectionName method

Yuki Takei 1 год назад
Родитель
Сommit
c63d199537

+ 0 - 16
apps/app/src/server/service/growi-bridge/index.ts

@@ -1,4 +1,3 @@
-import { Model } from 'mongoose';
 import unzipStream, { type Entry } from 'unzip-stream';
 
 import loggerFactory from '~/utils/logger';
@@ -52,21 +51,6 @@ class GrowiBridgeService {
     return this.metaFileName;
   }
 
-  /**
-   * get a model from collection name
-   *
-   * @memberOf GrowiBridgeService
-   * @param {string} collectionName collection name
-   * @return {object} instance of mongoose model
-   */
-  getModelFromCollectionName(collectionName) {
-    const Model = Object.values(this.crowi.models).find((m: Model<unknown>) => {
-      return m.collection != null && m.collection.name === collectionName;
-    });
-
-    return Model;
-  }
-
   /**
    * get the absolute path to a file
    * this method must must be bound to the caller (this.baseDir is undefined in this service)

+ 20 - 0
apps/app/src/server/service/import/get-model-from-collection-name.ts

@@ -0,0 +1,20 @@
+import type { Model } from 'mongoose';
+import mongoose from 'mongoose';
+
+/**
+   * get a model from collection name
+   *
+   * @memberOf GrowiBridgeService
+   * @param collectionName collection name
+   * @return instance of mongoose model
+   */
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export const getModelFromCollectionName = (collectionName: string): Model<any> => {
+  const models = mongoose.modelNames().map(modelName => mongoose.model(modelName));
+
+  const Model = Object.values(models).find((m) => {
+    return m.collection != null && m.collection.name === collectionName;
+  });
+
+  return Model;
+};

+ 2 - 1
apps/app/src/server/service/import/import.js

@@ -19,6 +19,7 @@ import CollectionProgressingStatus from '../../models/vo/collection-progressing-
 import { createBatchStream } from '../../util/batch-stream';
 
 import { constructConvertMap } from './construct-convert-map';
+import { getModelFromCollectionName } from './get-model-from-collection-name';
 import { keepOriginal } from './overwrite-function';
 
 const logger = loggerFactory('growi:services:ImportService'); // eslint-disable-line no-unused-vars
@@ -392,7 +393,7 @@ export class ImportService {
    * @return {object} document to be persisted
    */
   convertDocuments(collectionName, document, overwriteParams) {
-    const Model = this.growiBridgeService.getModelFromCollectionName(collectionName);
+    const Model = getModelFromCollectionName(collectionName);
     const schema = (Model != null) ? Model.schema : null;
     const convertMap = this.convertMap[collectionName];