From bfc937c5850335a60095dda8b2f7093c110247ac Mon Sep 17 00:00:00 2001 From: wuhan <18852676227@163.com> Date: Mon, 29 Jan 2024 17:38:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A1=B9=E7=9B=AE=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/.dev.yaml | 2 +- .config/.prod.yaml | 2 +- package.json | 6 ++-- src/app.module.ts | 19 ++--------- src/utils/config.ts | 78 +++++++++++++++++++++++++++++++++------------ 5 files changed, 65 insertions(+), 42 deletions(-) diff --git a/.config/.dev.yaml b/.config/.dev.yaml index c95fa74..dc214f6 100644 --- a/.config/.dev.yaml +++ b/.config/.dev.yaml @@ -4,5 +4,5 @@ TEST_VALUE: DATABASE: drive USER: root PORT: 3306 -HOST: localhost +HOST: 127.0.0.1 PASSWORD: zhonglian405 \ No newline at end of file diff --git a/.config/.prod.yaml b/.config/.prod.yaml index 95df2b2..8542de1 100644 --- a/.config/.prod.yaml +++ b/.config/.prod.yaml @@ -4,5 +4,5 @@ TEST_VALUE: DATABASE: drive USER: root PORT: 3306 -HOST: localhost +HOST: 127.0.0.1 PASSWORD: zhonglian405 \ No newline at end of file diff --git a/package.json b/package.json index cd02e71..494733e 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { - "@nestjs/common": "^10.0.5", + "@nestjs/common": "^10.3.1", "@nestjs/config": "^3.0.0", "@nestjs/core": "^10.0.0", "@nestjs/mapped-types": "*", @@ -29,7 +29,7 @@ "@nestjs/platform-fastify": "^10.0.4", "@nestjs/platform-socket.io": "^10.3.0", "@nestjs/serve-static": "^4.0.0", - "@nestjs/typeorm": "^10.0.0", + "@nestjs/typeorm": "^10.0.1", "@nestjs/websockets": "^10.3.0", "axios": "^1.4.0", "chalk": "^3.0.0", @@ -100,4 +100,4 @@ "coverageDirectory": "../coverage", "testEnvironment": "node" } -} \ No newline at end of file +} diff --git a/src/app.module.ts b/src/app.module.ts index e162308..cb640a9 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -4,6 +4,7 @@ import { AppService } from './app.service'; import { ClientModule } from './client/client.module'; import { GatewayModule } from './gateway/gateway.module'; import { TypeOrmModule, TypeOrmModuleAsyncOptions } from '@nestjs/typeorm'; +import { getMysql } from './utils/config'; // import 'winston-daily-rotate-file'; @Module({ @@ -11,23 +12,7 @@ import { TypeOrmModule, TypeOrmModuleAsyncOptions } from '@nestjs/typeorm'; // 加载连接数据库 TypeOrmModule.forRootAsync({ useFactory: async () => { - return { - // type: 'mysql', - // host: getConfig().HOST, - // port: getConfig().PORT, - // username: getConfig().USER, - // password: getConfig().PASSWORD, - // database: getConfig().DATABASE, - type: 'mysql', - host: '192.168.10.13', - port: 3306, - username: 'root', - password: '123456', - database: 'drive', - entities: [__dirname + '/**/*.entity{.ts,.js}'], // 扫描本项目中.entity.ts或者.entity.js的文件 - synchronize: false, - logging: true, - } as TypeOrmModuleAsyncOptions; + return getMysql() as TypeOrmModuleAsyncOptions; }, }), ClientModule, diff --git a/src/utils/config.ts b/src/utils/config.ts index 34ae8da..e6ba3e8 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -1,23 +1,61 @@ // /* eslint-disable @typescript-eslint/no-var-requires */ -// import { parse } from 'yaml'; -// // import path from 'path'; -// // import fs from 'fs'; -// const path = require('path'); -// const fs = require('fs'); +import { parse } from 'yaml'; +// import path from 'path'; +// import fs from 'fs'; +const path = require('path'); +const fs = require('fs'); -// // 获取项目运行环境 -// export const getEnv = () => { -// // console.log(process.env); -// return process.env.RUNNING_ENV || 'prod'; -// }; +// 获取项目运行环境 +export const getEnv = () => { + // console.log(process.env); + return process.env.RUNNING_ENV || 'prod'; +}; -// // 读取项目配置 -// export const getConfig = () => { -// const environment = getEnv(); -// const yamlPath = path.join(process.cwd(), `./.config/.${environment}.yaml`); -// const file = fs.readFileSync(yamlPath, 'utf8'); -// let config = parse(file); -// // 融合注入参数 -// config = Object.assign(config, process.env); -// return config; -// }; +let configAll = { isExist: false } +// 读取项目配置 +export const getConfig = () => { + if (configAll.isExist) { + return configAll + } + const environment = getEnv(); + const yamlPath = path.join(process.cwd(), `./.config/.${environment}.yaml`); + const file = fs.readFileSync(yamlPath, 'utf8'); + let config = parse(file); + if (process?.argv[2]?.length) { + config = { ...config, HOST: process.argv[2] } + } + if (process?.argv[3]?.length) { + config = { ...config, USER: process.argv[3] } + } + if (process?.argv[4]?.length) { + config = { ...config, PASSWORD: process.argv[4] } + } + // 融合注入参数 + // config = Object.assign(config, process.env); + console.log(config) + configAll = { ...config, isExist: true } + return config; +}; + +export const getMysql = () => { + const mysql = { + type: 'mysql', + host: getConfig().HOST, + port: getConfig().PORT, + username: getConfig().USER, + password: getConfig().PASSWORD, + database: getConfig().DATABASE, + // type: 'mysql', + // host: '192.168.10.13', + // port: 3306, + // username: 'root', + // password: '123456', + // database: 'drive', + entities: [__dirname + '/**/*.entity{.ts,.js}'], // 扫描本项目中.entity.ts或者.entity.js的文件 + synchronize: false, + logging: true, + } + console.log(mysql); + + return mysql +}