Browse Source

add swagger property and schema api

nameczz 4 years ago
parent
commit
5f0c89f763

+ 2 - 0
server/src/app.module.ts

@@ -11,6 +11,7 @@ import { AuthModule } from './auth/auth.module';
 import { ServeStaticModule } from '@nestjs/serve-static';
 import { join } from 'path';
 import { PartitionsModule } from './partitions/partitions.module';
+import { SchemaModule } from './schema/schema.module';
 
 @Module({
   imports: [
@@ -23,6 +24,7 @@ import { PartitionsModule } from './partitions/partitions.module';
     UsersModule,
     AuthModule,
     PartitionsModule,
+    SchemaModule,
   ],
   controllers: [AppController],
   providers: [

+ 1 - 2
server/src/collections/collections.service.ts

@@ -8,7 +8,7 @@ import {
   GetIndexStateReq,
   LoadCollectionReq,
   ReleaseLoadCollectionReq,
-} from '@zilliz/milvus-sdk-node-dev/dist/milvus/types'; // todo: need improve like export types in root file.
+} from '@zilliz/milvus-sdk-node-dev/dist/milvus/types';
 import { throwErrorFromSDK } from '../utils/Error';
 import { findKeyValue } from '../utils/Helper';
 import { ROW_COUNT } from '../utils/Const';
@@ -71,7 +71,6 @@ export class CollectionsService {
    */
   async getIndexStatus(data: GetIndexStateReq) {
     const res = await this.milvusClient.getIndexState(data);
-    throwErrorFromSDK(res.status);
     return res;
   }
 

+ 4 - 0
server/src/collections/dto.ts

@@ -7,18 +7,22 @@ import {
   ArrayNotEmpty,
 } from 'class-validator';
 import { FieldType } from '@zilliz/milvus-sdk-node-dev/dist/milvus/types/Collection'; // todo: need improve like export types in root file.
+import { ApiProperty } from '@nestjs/swagger';
 
 export class CreateCollection {
+  @ApiProperty()
   @IsString()
   @IsNotEmpty({
     message: 'collection_name is empty',
   })
   readonly collection_name: string;
 
+  @ApiProperty()
   @IsBoolean()
   @IsOptional()
   readonly autoID: boolean;
 
+  @ApiProperty()
   @IsArray()
   @ArrayNotEmpty()
   @IsNotEmpty({

+ 3 - 0
server/src/milvus/dto.ts

@@ -1,6 +1,8 @@
+import { ApiProperty } from '@nestjs/swagger';
 import { IsNotEmpty, IsString } from 'class-validator';
 
 export class ConnectMilvus {
+  @ApiProperty()
   @IsString()
   @IsNotEmpty({
     message: 'address is empty',
@@ -9,6 +11,7 @@ export class ConnectMilvus {
 }
 
 export class CheckMilvus {
+  @ApiProperty()
   @IsString()
   @IsNotEmpty({
     message: 'address is empty',

+ 7 - 0
server/src/partitions/dto.ts

@@ -1,3 +1,4 @@
+import { ApiProperty } from '@nestjs/swagger';
 import {
   IsNotEmpty,
   IsString,
@@ -11,6 +12,7 @@ export enum ManageType {
   CREATE = 'create',
 }
 export class GetPartitionsInfo {
+  @ApiProperty()
   @IsString()
   @IsNotEmpty({
     message: 'collection_name is empty',
@@ -19,29 +21,34 @@ export class GetPartitionsInfo {
 }
 
 export class ManagePartition {
+  @ApiProperty()
   @IsString()
   @IsNotEmpty({
     message: 'collection_name is empty',
   })
   readonly collection_name: string;
 
+  @ApiProperty()
   @IsString()
   @IsNotEmpty({
     message: 'partition_name is empty',
   })
   readonly partition_name: string;
 
+  @ApiProperty()
   @IsEnum(ManageType, { message: 'Type allow delete and create' })
   readonly type: ManageType;
 }
 
 export class LoadPartitions {
+  @ApiProperty()
   @IsString()
   @IsNotEmpty({
     message: 'collection_name is empty',
   })
   readonly collection_name: string;
 
+  @ApiProperty()
   @IsArray()
   @ArrayNotEmpty()
   @IsNotEmpty({

+ 31 - 0
server/src/schema/dto.ts

@@ -0,0 +1,31 @@
+import { ApiProperty } from '@nestjs/swagger';
+import { IsNotEmpty, IsString, IsArray } from 'class-validator';
+
+class KeyValuePair {
+  @ApiProperty()
+  key: string;
+  @ApiProperty()
+  value: string;
+}
+
+export class CreateIndex {
+  @ApiProperty()
+  @IsString()
+  @IsNotEmpty({
+    message: 'collection_name is empty',
+  })
+  readonly collection_name: string;
+
+  @ApiProperty()
+  @IsString()
+  @IsNotEmpty({
+    message: 'field_name is empty',
+  })
+  readonly field_name: string;
+
+  @ApiProperty({
+    type: [KeyValuePair],
+  })
+  @IsArray()
+  readonly extra_params: KeyValuePair[];
+}

+ 18 - 0
server/src/schema/schema.controller.spec.ts

@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing';
+import { SchemaController } from './schema.controller';
+
+describe('SchemaController', () => {
+  let controller: SchemaController;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      controllers: [SchemaController],
+    }).compile();
+
+    controller = module.get<SchemaController>(SchemaController);
+  });
+
+  it('should be defined', () => {
+    expect(controller).toBeDefined();
+  });
+});

+ 21 - 0
server/src/schema/schema.controller.ts

@@ -0,0 +1,21 @@
+import {
+  Body,
+  Controller,
+  Post,
+  UsePipes,
+  ValidationPipe,
+} from '@nestjs/common';
+import { CreateIndex } from './dto';
+import { SchemaService } from './schema.service';
+
+@Controller('schema')
+export class SchemaController {
+  constructor(private schemaService: SchemaService) {}
+
+  @Post('index')
+  @UsePipes(new ValidationPipe())
+  async createIndex(@Body() body: CreateIndex) {
+    console.log(body);
+    return await this.schemaService.createIndex(body);
+  }
+}

+ 11 - 0
server/src/schema/schema.module.ts

@@ -0,0 +1,11 @@
+import { Module } from '@nestjs/common';
+import { MilvusModule } from '../milvus/milvus.module';
+import { SchemaController } from './schema.controller';
+import { SchemaService } from './schema.service';
+
+@Module({
+  imports: [MilvusModule],
+  controllers: [SchemaController],
+  providers: [SchemaService],
+})
+export class SchemaModule {}

+ 18 - 0
server/src/schema/schema.service.spec.ts

@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing';
+import { SchemaService } from './schema.service';
+
+describe('SchemaService', () => {
+  let service: SchemaService;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      providers: [SchemaService],
+    }).compile();
+
+    service = module.get<SchemaService>(SchemaService);
+  });
+
+  it('should be defined', () => {
+    expect(service).toBeDefined();
+  });
+});

+ 43 - 0
server/src/schema/schema.service.ts

@@ -0,0 +1,43 @@
+import { Injectable } from '@nestjs/common';
+import {
+  CreateIndexReq,
+  DescribeIndexReq,
+  DropIndexReq,
+  GetIndexBuildProgressReq,
+  GetIndexStateReq,
+} from '@zilliz/milvus-sdk-node-dev/dist/milvus/types';
+import { MilvusService } from '../milvus/milvus.service';
+
+@Injectable()
+export class SchemaService {
+  constructor(private milvusService: MilvusService) {}
+
+  get milvusClient() {
+    return this.milvusService.milvusClientGetter;
+  }
+
+  async createIndex(data: CreateIndexReq) {
+    const res = await this.milvusClient.createIndex(data);
+    return res;
+  }
+
+  async describeIndex(data: DescribeIndexReq) {
+    const res = await this.milvusClient.describeIndex(data);
+    return res;
+  }
+
+  async dropIndex(data: DropIndexReq) {
+    const res = await this.milvusClient.dropIndex(data);
+    return res;
+  }
+
+  async getIndexState(data: GetIndexStateReq) {
+    const res = await this.milvusClient.getIndexState(data);
+    return res;
+  }
+
+  async getIndexBuildProgress(data: GetIndexBuildProgressReq) {
+    const res = await this.milvusClient.getIndexBuildProgress(data);
+    return res;
+  }
+}