import { Validation } from "src/core" ; import { DatasourceDto } from "src/dto" ; import { createConnection } from "typeorm" ; import { Controller , Post , Body } from "../core/decorator" ; import BaseController from "./BaseController" ; @ Controller ( "/datasource" ) export default class DataSourceController extends BaseController { param dto @ Post ( "/test" ) async connectTest ( @ Body ( new Validation ( { groups : [ "test" ] } ) ) dto ) { const { type , host , port , username , password } = dto ; try { const rel = await createConnection ( { type , host , port , username , password , synchronize : false , logging : false , } ) ; if ( rel ) { rel . close ( ) ; return this . success ( null ) ; } } catch ( error ) { return this . fail ( "连接失败" ) ; } } param dto @ Post ( "/connect" ) async connect ( ) { const rel = await createConnection ( { type : "mysql" , host : "localhost" , port : 3306 , username : "root" , password : "" , database : "test" , synchronize : false , logging : false , } ) ; console . log ( "------------------------------------" , rel ) ; return this . fail ( "添加失败" ) ; } param dto @ Post ( "/query" ) async query ( @ Body ( "query" ) queryStr ) { const rel = await createConnection ( { type : "mysql" , host : "localhost" , port : 3306 , username : "root" , password : "" , database : "test" , synchronize : false , logging : false , } ) ; console . log ( "------------------------------------" , rel ) ; return this . fail ( "添加失败" ) ; } }