فهرست منبع

refactor ImportService

Yuki Takei 1 سال پیش
والد
کامیت
8e7716f5ad

+ 6 - 0
apps/app/src/server/service/import/import-mode.ts

@@ -0,0 +1,6 @@
+export const ImportMode = {
+  insert: 'insert',
+  upsert: 'upsert',
+  flushAndInsert: 'flushAndInsert',
+} as const;
+export type ImportMode = typeof ImportMode[keyof typeof ImportMode];

+ 12 - 0
apps/app/src/server/service/import/import-settings.ts

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

+ 17 - 35
apps/app/src/server/service/import.js → apps/app/src/server/service/import/import.js

@@ -3,23 +3,22 @@
  * @typedef {import("@types/unzip-stream").Entry} Entry
  * @typedef {import("@types/unzip-stream").Entry} Entry
  */
  */
 
 
+import fs from 'fs';
+import path from 'path';
+import { Writable, Transform } from 'stream';
+
+import JSONStream from 'JSONStream';
 import { parseISO } from 'date-fns/parseISO';
 import { parseISO } from 'date-fns/parseISO';
 import gc from 'expose-gc/function';
 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';
 
 
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
-const fs = require('fs');
-const path = require('path');
-const { Writable, Transform } = require('stream');
-
-const JSONStream = require('JSONStream');
-const isIsoDate = require('is-iso-date');
-const mongoose = require('mongoose');
-const streamToPromise = require('stream-to-promise');
-const unzipStream = require('unzip-stream');
-
-const CollectionProgressingStatus = require('../models/vo/collection-progressing-status');
-const { createBatchStream } = require('../util/batch-stream');
+import CollectionProgressingStatus from '../../models/vo/collection-progressing-status';
+import { createBatchStream } from '../../util/batch-stream';
 
 
 const { ObjectId } = mongoose.Types;
 const { ObjectId } = mongoose.Types;
 
 
@@ -29,16 +28,6 @@ const logger = loggerFactory('growi:services:ImportService'); // eslint-disable-
 const BULK_IMPORT_SIZE = 100;
 const BULK_IMPORT_SIZE = 100;
 
 
 
 
-export class ImportSettings {
-
-  constructor(mode) {
-    this.mode = mode || 'insert';
-    this.jsonFileName = null;
-    this.overwriteParams = null;
-  }
-
-}
-
 class ImportingCollectionError extends Error {
 class ImportingCollectionError extends Error {
 
 
   constructor(collectionProgress, error) {
   constructor(collectionProgress, error) {
@@ -49,7 +38,7 @@ class ImportingCollectionError extends Error {
 }
 }
 
 
 
 
-class ImportService {
+export class ImportService {
 
 
   constructor(crowi) {
   constructor(crowi) {
     this.crowi = crowi;
     this.crowi = crowi;
@@ -67,14 +56,6 @@ class ImportService {
     this.currentProgressingStatus = null;
     this.currentProgressingStatus = null;
   }
   }
 
 
-  /**
-   * generate ImportSettings instance
-   * @param {string} mode bulk operation mode (insert | upsert | flushAndInsert)
-   */
-  generateImportSettings(mode) {
-    return new ImportSettings(mode);
-  }
-
   /**
   /**
    * initialize convert map. set keepOriginal as default
    * initialize convert map. set keepOriginal as default
    *
    *
@@ -89,6 +70,8 @@ class ImportService {
       }
       }
 
 
       const collectionName = model.collection.name;
       const collectionName = model.collection.name;
+
+      console.log({ collectionName });
       this.convertMap[collectionName] = {};
       this.convertMap[collectionName] = {};
 
 
       for (const key of Object.keys(model.schema.paths)) {
       for (const key of Object.keys(model.schema.paths)) {
@@ -181,8 +164,9 @@ class ImportService {
   /**
   /**
    * import collections from json
    * import collections from json
    *
    *
-   * @param {string} collections MongoDB collection name
-   * @param {array} importSettingsMap key: collection name, value: ImportSettings instance
+   * @param {string[]} collections MongoDB collection name
+   * @param {{ [collectionName: string]: ImportSettings }} importSettingsMap key: collection name, value: ImportSettings instance
+   * @return {Promise<void>}
    */
    */
   async import(collections, importSettingsMap) {
   async import(collections, importSettingsMap) {
     // init status object
     // init status object
@@ -537,5 +521,3 @@ class ImportService {
   }
   }
 
 
 }
 }
-
-module.exports = ImportService;

+ 23 - 0
apps/app/src/server/service/import/index.ts

@@ -0,0 +1,23 @@
+import type Crowi from '~/server/crowi';
+
+import { ImportService } from './import';
+
+
+let instance: ImportService;
+
+export const initializeImportService = (crowi: Crowi): void => {
+  if (instance == null) {
+    instance = new ImportService(crowi);
+  }
+};
+
+export const getImportService = (): ImportService => {
+  if (instance == null) {
+    throw new Error('ImportService has not been initialized');
+  }
+  return instance;
+};
+
+
+export * from './import-mode';
+export * from './import-settings';