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

+ 1 - 3
apps/app/src/server/service/import/import-settings.ts

@@ -1,8 +1,6 @@
-import type { Document, Schema } from 'mongoose';
-
 import type { ImportMode } from './import-mode';
+import type { OverwriteFunction } from './overwrite-function';
 
-export type OverwriteFunction = (value: unknown, ctx: { document: Document, propertyName: string, schema: Schema }) => unknown;
 export type OverwriteParams = { [propertyName: string]: OverwriteFunction | unknown }
 
 export type ImportSettings = {

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

@@ -8,9 +8,7 @@ import path from 'path';
 import { Writable, Transform } from 'stream';
 
 import JSONStream from 'JSONStream';
-import { parseISO } from 'date-fns/parseISO';
 import gc from 'expose-gc/function';
-import isIsoDate from 'is-iso-date';
 import mongoose from 'mongoose';
 import streamToPromise from 'stream-to-promise';
 import unzipStream from 'unzip-stream';
@@ -20,7 +18,7 @@ import loggerFactory from '~/utils/logger';
 import CollectionProgressingStatus from '../../models/vo/collection-progressing-status';
 import { createBatchStream } from '../../util/batch-stream';
 
-const { ObjectId } = mongoose.Types;
+import { keepOriginal } from './overwrite-function';
 
 const logger = loggerFactory('growi:services:ImportService'); // eslint-disable-line no-unused-vars
 
@@ -45,7 +43,6 @@ export class ImportService {
     this.growiBridgeService = crowi.growiBridgeService;
     this.getFile = this.growiBridgeService.getFile.bind(this);
     this.baseDir = path.join(crowi.tmpDir, 'imports');
-    this.keepOriginal = this.keepOriginal.bind(this);
 
     this.adminEvent = crowi.event('admin');
 
@@ -75,42 +72,11 @@ export class ImportService {
       this.convertMap[collectionName] = {};
 
       for (const key of Object.keys(model.schema.paths)) {
-        this.convertMap[collectionName][key] = this.keepOriginal;
+        this.convertMap[collectionName][key] = keepOriginal;
       }
     }
   }
 
-  /**
-   * keep original value
-   * automatically convert ObjectId
-   *
-   * @memberOf ImportService
-   * @param {any} value value from imported document
-   * @param {{ document: object, schema: object, propertyName: string }}
-   * @return {any} new value for the document
-   *
-   * @see https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-cast
-   */
-  keepOriginal(value, { document, schema, propertyName }) {
-    // Model
-    if (schema != null && schema.path(propertyName) != null) {
-      const schemaType = schema.path(propertyName);
-      return schemaType.cast(value);
-    }
-
-    // _id
-    if (propertyName === '_id' && ObjectId.isValid(value)) {
-      return ObjectId(value);
-    }
-
-    // Date
-    if (isIsoDate(value)) {
-      return parseISO(value);
-    }
-
-    return value;
-  }
-
   /**
    * parse all zip files in downloads dir
    *
@@ -460,7 +426,7 @@ export class ImportService {
     if (convertMap == null) {
       // apply keepOriginal to all of properties
       Object.entries(document).forEach(([propertyName, value]) => {
-        _document[propertyName] = this.keepOriginal(value, { document, propertyName });
+        _document[propertyName] = keepOriginal(value, { document, propertyName });
       });
     }
     // Mongoose Model

+ 39 - 0
apps/app/src/server/service/import/overwrite-function.ts

@@ -0,0 +1,39 @@
+import { parseISO } from 'date-fns/parseISO';
+import isIsoDate from 'is-iso-date';
+import { Types, type Document, type Schema } from 'mongoose';
+
+const { ObjectId } = Types;
+
+export type OverwriteFunction = (value: unknown, ctx: { document: Document, propertyName: string, schema?: Schema }) => unknown;
+
+/**
+ * keep original value
+ * automatically convert ObjectId
+ *
+ * @param value value from imported document
+ * @param ctx context object
+ * @return new value for the document
+ *
+ * @see https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-cast
+ */
+export const keepOriginal: OverwriteFunction = (value, { document, schema, propertyName }) => {
+  // Model
+  if (schema != null && schema.path(propertyName) != null) {
+    const schemaType = schema.path(propertyName);
+    return schemaType.cast(value, document, true);
+  }
+
+  // _id
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  if (propertyName === '_id' && ObjectId.isValid(value as any)) {
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
+    return new ObjectId(value as any);
+  }
+
+  // Date
+  if (isIsoDate(value)) {
+    return parseISO(value as string);
+  }
+
+  return value;
+};