Explorar el Código

use keepOriginal in default

Yuki Takei hace 6 años
padre
commit
386b486ec3
Se han modificado 3 ficheros con 32 adiciones y 9 borrados
  1. 1 0
      package.json
  2. 26 9
      src/server/service/import.js
  3. 5 0
      yarn.lock

+ 1 - 0
package.json

@@ -107,6 +107,7 @@
     "i18next-express-middleware": "^1.4.1",
     "i18next-express-middleware": "^1.4.1",
     "i18next-node-fs-backend": "^2.1.0",
     "i18next-node-fs-backend": "^2.1.0",
     "i18next-sprintf-postprocessor": "^0.2.2",
     "i18next-sprintf-postprocessor": "^0.2.2",
+    "is-iso-date": "^0.0.1",
     "md5": "^2.2.1",
     "md5": "^2.2.1",
     "method-override": "^3.0.0",
     "method-override": "^3.0.0",
     "migrate-mongo": "^7.0.1",
     "migrate-mongo": "^7.0.1",

+ 26 - 9
src/server/service/import.js

@@ -2,6 +2,9 @@ const logger = require('@alias/logger')('growi:services:ImportService'); // esli
 const fs = require('fs');
 const fs = require('fs');
 const path = require('path');
 const path = require('path');
 
 
+const isIsoDate = require('is-iso-date');
+const parseISO = require('date-fns/parseISO');
+
 const { Writable, Transform } = require('stream');
 const { Writable, Transform } = require('stream');
 const JSONStream = require('JSONStream');
 const JSONStream = require('JSONStream');
 const streamToPromise = require('stream-to-promise');
 const streamToPromise = require('stream-to-promise');
@@ -92,16 +95,27 @@ class ImportService {
    *
    *
    * @memberOf ImportService
    * @memberOf ImportService
    * @param {any} value value from imported document
    * @param {any} value value from imported document
-   * @param {{ document: object, schema: object, key: string }}
+   * @param {{ document: object, schema: object, propertyName: string }}
    * @return {any} new value for the document
    * @return {any} new value for the document
    */
    */
   keepOriginal(value, { document, schema, propertyName }) {
   keepOriginal(value, { document, schema, propertyName }) {
-    let _value;
-    if (schema[propertyName].instance === 'ObjectID' && ObjectId.isValid(value)) {
+    let _value = value;
+
+    // _id
+    if (propertyName === '_id' && ObjectId.isValid(value)) {
       _value = ObjectId(value);
       _value = ObjectId(value);
     }
     }
-    else {
-      _value = value;
+    // Date
+    else if (isIsoDate(value)) {
+      _value = parseISO(value);
+    }
+
+    // Model
+    if (schema != null) {
+      // ObjectID
+      if (schema[propertyName].instance === 'ObjectID' && ObjectId.isValid(value)) {
+        _value = ObjectId(value);
+      }
     }
     }
 
 
     return _value;
     return _value;
@@ -398,14 +412,17 @@ class ImportService {
     const schema = (Model != null) ? Model.schema.paths : null;
     const schema = (Model != null) ? Model.schema.paths : null;
     const convertMap = this.convertMap[collectionName];
     const convertMap = this.convertMap[collectionName];
 
 
-    let _document;
+    const _document = {};
 
 
+    // not Mongoose Model
     if (convertMap == null) {
     if (convertMap == null) {
-      _document = Object.assign({}, document);
+      // apply keepOriginal to all of properties
+      Object.entries(document).forEach(([propertyName, value]) => {
+        _document[propertyName] = this.keepOriginal(value, { document, propertyName });
+      });
     }
     }
+    // Mongoose Model
     else {
     else {
-      _document = {};
-
       // assign value from documents being imported
       // assign value from documents being imported
       Object.entries(convertMap).forEach(([propertyName, convertedValue]) => {
       Object.entries(convertMap).forEach(([propertyName, convertedValue]) => {
         const value = document[propertyName];
         const value = document[propertyName];

+ 5 - 0
yarn.lock

@@ -6616,6 +6616,11 @@ is-installed-globally@^0.1.0:
     global-dirs "^0.1.0"
     global-dirs "^0.1.0"
     is-path-inside "^1.0.0"
     is-path-inside "^1.0.0"
 
 
+is-iso-date@^0.0.1:
+  version "0.0.1"
+  resolved "https://registry.yarnpkg.com/is-iso-date/-/is-iso-date-0.0.1.tgz#d1727b0a4f40cf4dd0dbf95a56a58cc991bb76e2"
+  integrity sha1-0XJ7Ck9Az03Q2/laVqWMyZG7duI=
+
 is-npm@^1.0.0:
 is-npm@^1.0.0:
   version "1.0.0"
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/is-npm/-/is-npm-1.0.0.tgz#f2fb63a65e4905b406c86072765a1a4dc793b9f4"
   resolved "https://registry.yarnpkg.com/is-npm/-/is-npm-1.0.0.tgz#f2fb63a65e4905b406c86072765a1a4dc793b9f4"