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

Merge pull request #2101 from weseek/fix/2080-import-models

use SchemaTyppe.cast for importing mongoose model documents
Yuki Takei 6 лет назад
Родитель
Сommit
cf56e0e649
1 измененных файлов с 13 добавлено и 14 удалено
  1. 13 14
      src/server/service/import.js

+ 13 - 14
src/server/service/import.js

@@ -97,28 +97,27 @@ class ImportService {
    * @param {any} value value from imported document
    * @param {any} value value from imported document
    * @param {{ document: object, schema: object, propertyName: string }}
    * @param {{ document: object, schema: object, propertyName: string }}
    * @return {any} new value for the document
    * @return {any} new value for the document
+   *
+   * @see https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-cast
    */
    */
   keepOriginal(value, { document, schema, propertyName }) {
   keepOriginal(value, { document, schema, propertyName }) {
-    let _value = value;
+    // Model
+    if (schema != null && schema.path(propertyName) != null) {
+      const schemaType = schema.path(propertyName);
+      return schemaType.cast(value);
+    }
 
 
     // _id
     // _id
     if (propertyName === '_id' && ObjectId.isValid(value)) {
     if (propertyName === '_id' && ObjectId.isValid(value)) {
-      _value = ObjectId(value);
-    }
-    // Date
-    else if (isIsoDate(value)) {
-      _value = parseISO(value);
+      return ObjectId(value);
     }
     }
 
 
-    // Model
-    if (schema != null) {
-      // ObjectID
-      if (schema[propertyName] != null && schema[propertyName].instance === 'ObjectID' && ObjectId.isValid(value)) {
-        _value = ObjectId(value);
-      }
+    // Date
+    if (isIsoDate(value)) {
+      return parseISO(value);
     }
     }
 
 
-    return _value;
+    return value;
   }
   }
 
 
   /**
   /**
@@ -409,7 +408,7 @@ class ImportService {
    */
    */
   convertDocuments(collectionName, document, overwriteParams) {
   convertDocuments(collectionName, document, overwriteParams) {
     const Model = this.growiBridgeService.getModelFromCollectionName(collectionName);
     const Model = this.growiBridgeService.getModelFromCollectionName(collectionName);
-    const schema = (Model != null) ? Model.schema.paths : null;
+    const schema = (Model != null) ? Model.schema : null;
     const convertMap = this.convertMap[collectionName];
     const convertMap = this.convertMap[collectionName];
 
 
     const _document = {};
     const _document = {};