Browse Source

refine schema contronller

Signed-off-by: Gitea <zizhao.chen@zilliz.com>
Gitea 3 years ago
parent
commit
fabfe7cdc9
3 changed files with 168 additions and 53 deletions
  1. 63 0
      express/src/schema/dto.ts
  2. 3 53
      express/src/schema/index.ts
  3. 102 0
      express/src/schema/schema.controller.ts

+ 63 - 0
express/src/schema/dto.ts

@@ -0,0 +1,63 @@
+import { CreateIndexParam } from "@zilliz/milvus2-sdk-node/dist/milvus/types";
+import {
+  IsNotEmpty,
+  IsString,
+  IsEnum,
+  IsOptional,
+  IsObject,
+} from "class-validator";
+
+class KeyValuePair {
+  key: string;
+  value: string;
+}
+
+export enum ManageType {
+  DELETE = "delete",
+  CREATE = "create",
+}
+
+export class ManageIndexDto {
+  @IsEnum(ManageType, { message: "Type allow delete and create" })
+  readonly type: ManageType;
+
+  @IsString()
+  readonly collection_name: string;
+
+  @IsString()
+  readonly field_name: string;
+
+  @IsObject()
+  @IsOptional()
+  readonly extra_params?: CreateIndexParam;
+}
+
+export class DescribeIndexDto {
+  @IsString()
+  readonly collection_name: string;
+
+  @IsString()
+  @IsOptional()
+  readonly field_name?: string;
+}
+
+export class GetIndexStateDto {
+  @IsString()
+  readonly collection_name: string;
+
+  @IsString()
+  @IsOptional()
+  readonly field_name?: string;
+}
+
+export class GetIndexProgressDto {
+  @IsString()
+  readonly collection_name: string;
+
+  @IsString()
+  readonly index_name: string;
+
+  @IsString()
+  @IsOptional()
+  readonly field_name?: string;
+}

+ 3 - 53
express/src/schema/index.ts

@@ -1,55 +1,5 @@
-import express from "express";
-import { SchemaService } from "./schema.service";
-import { milvusService } from "../milvus";
-
-const router = express.Router();
-
-const schemaService = new SchemaService(milvusService);
-
-router.post("/index", async (req, res, next) => {
-  const { type, collection_name, extra_params, field_name } = req.body;
-  try {
-    const result =
-      type.toLocaleLowerCase() === "create"
-        ? await schemaService.createIndex({
-            collection_name,
-            extra_params,
-            field_name,
-          })
-        : await schemaService.dropIndex({ collection_name, field_name });
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.get("/index", async (req, res, next) => {
-  const data = "" + req.query?.collection_name;
-  try {
-    const result = await schemaService.describeIndex({ collection_name: data });
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.get("/index/progress", async (req, res, next) => {
-  const data = "" + req.query?.collection_name;
-  try {
-    const result = await schemaService.getIndexBuildProgress({
-      collection_name: data,
-    });
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.get("/index/state", async (req, res, next) => {
-  const data = "" + req.query?.collection_name;
-  try {
-    const result = await schemaService.getIndexState({ collection_name: data });
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
+import { SchemaController } from "./schema.controller";
+const schemaManager = new SchemaController();
+const router = schemaManager.generateRoutes();
 
 export { router };

+ 102 - 0
express/src/schema/schema.controller.ts

@@ -0,0 +1,102 @@
+import { NextFunction, Request, Response, Router } from "express";
+import { dtoValidationMiddleware } from "../middlewares/validation";
+import { SchemaService } from "./schema.service";
+import { milvusService } from "../milvus";
+
+import {
+  ManageIndexDto,
+  DescribeIndexDto,
+  GetIndexProgressDto,
+  GetIndexStateDto,
+} from "./dto";
+
+export class SchemaController {
+  private router: Router;
+  private schemaService: SchemaService;
+
+  constructor() {
+    this.schemaService = new SchemaService(milvusService);
+    this.router = Router();
+  }
+
+  generateRoutes() {
+    this.router.post(
+      "/index",
+      dtoValidationMiddleware(ManageIndexDto),
+      this.manageIndex.bind(this)
+    );
+
+    this.router.get(
+      "/index",
+      dtoValidationMiddleware(DescribeIndexDto),
+      this.describeIndex.bind(this)
+    );
+
+    this.router.post(
+      "/index/progress",
+      dtoValidationMiddleware(GetIndexProgressDto),
+      this.getIndexBuildProgress.bind(this)
+    );
+
+    this.router.post(
+      "/index/state",
+      dtoValidationMiddleware(GetIndexStateDto),
+      this.getIndexState.bind(this)
+    );
+
+    return this.router;
+  }
+
+  async manageIndex(req: Request, res: Response, next: NextFunction) {
+    const { type, collection_name, extra_params, field_name } = req.body;
+    try {
+      const result =
+        type.toLocaleLowerCase() === "create"
+          ? await this.schemaService.createIndex({
+              collection_name,
+              extra_params,
+              field_name,
+            })
+          : await this.schemaService.dropIndex({ collection_name, field_name });
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async describeIndex(req: Request, res: Response, next: NextFunction) {
+    const data = "" + req.query?.collection_name;
+    try {
+      const result = await this.schemaService.describeIndex({
+        collection_name: data,
+      });
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async getIndexBuildProgress(req: Request, res: Response, next: NextFunction) {
+    const data = "" + req.query?.collection_name;
+    try {
+      const result = await this.schemaService.getIndexBuildProgress({
+        collection_name: data,
+      });
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async getIndexState(req: Request, res: Response, next: NextFunction) {
+    const data = "" + req.query?.collection_name;
+    try {
+      const result = await this.schemaService.getIndexState({
+        collection_name: data,
+      });
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+}