From 886aa79be3304e7267432d1c4e69ba8787ef5e5c Mon Sep 17 00:00:00 2001 From: dayjoy Date: Wed, 11 Mar 2026 14:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=8C=96=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 5 +++++ eslint.config.mjs | 2 +- src/app.controller.spec.ts | 22 ---------------------- src/app.controller.ts | 12 ------------ src/app.module.ts | 15 ++++++++++----- src/app.service.ts | 8 -------- src/config/database.config.ts | 18 ++++++++++++++++++ src/main.ts | 19 +++++++++++++++++-- tsconfig.json | 6 +++--- 9 files changed, 54 insertions(+), 53 deletions(-) create mode 100644 .env.example delete mode 100644 src/app.controller.spec.ts delete mode 100644 src/app.controller.ts delete mode 100644 src/app.service.ts create mode 100644 src/config/database.config.ts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..71e7ad5 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +DB_HOST=localhost +DB_PORT=3306 +DB_USERNAME=root +DB_PASSWORD=your_password +DB_DATABASE=your_database diff --git a/eslint.config.mjs b/eslint.config.mjs index 4e9f827..b11ccdf 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -26,7 +26,7 @@ export default tseslint.config( }, { rules: { - '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-explicit-any': 'warn', '@typescript-eslint/no-floating-promises': 'warn', '@typescript-eslint/no-unsafe-argument': 'warn', "prettier/prettier": ["error", { endOfLine: "auto" }], diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index d22f389..0000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/src/app.controller.ts b/src/app.controller.ts deleted file mode 100644 index cce879e..0000000 --- a/src/app.controller.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; -import { AppService } from './app.service'; - -@Controller() -export class AppController { - constructor(private readonly appService: AppService) {} - - @Get() - getHello(): string { - return this.appService.getHello(); - } -} diff --git a/src/app.module.ts b/src/app.module.ts index 8662803..f5846a7 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,15 @@ import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; +import { ConfigModule, ConfigService } from '@nestjs/config'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import databaseConfig from '@/config/database.config'; @Module({ - imports: [], - controllers: [AppController], - providers: [AppService], + imports: [ + ConfigModule.forRoot({ isGlobal: true, load: [databaseConfig] }), + TypeOrmModule.forRootAsync({ + inject: [ConfigService], + useFactory: (config: ConfigService) => config.get('database')!, + }), + ], }) export class AppModule {} diff --git a/src/app.service.ts b/src/app.service.ts deleted file mode 100644 index 927d7cc..0000000 --- a/src/app.service.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() -export class AppService { - getHello(): string { - return 'Hello World!'; - } -} diff --git a/src/config/database.config.ts b/src/config/database.config.ts new file mode 100644 index 0000000..786a196 --- /dev/null +++ b/src/config/database.config.ts @@ -0,0 +1,18 @@ +import { registerAs } from '@nestjs/config'; +import { TypeOrmModuleOptions } from '@nestjs/typeorm'; + +export default registerAs( + 'database', + (): TypeOrmModuleOptions => ({ + type: 'mysql', + host: process.env.DB_HOST || 'localhost', + port: parseInt(process.env.DB_PORT ?? '3306', 10), + username: process.env.DB_USERNAME || 'root', + password: process.env.DB_PASSWORD || '', + database: process.env.DB_DATABASE, + autoLoadEntities: true, + synchronize: process.env.NODE_ENV !== 'production', + logging: + process.env.NODE_ENV !== 'production' ? ['query', 'error'] : ['error'], + }), +); diff --git a/src/main.ts b/src/main.ts index f76bc8d..648d311 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,23 @@ -import { NestFactory } from '@nestjs/core'; +import { NestFactory, Reflector } from '@nestjs/core'; +import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common'; +import 'reflect-metadata'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); - await app.listen(process.env.PORT ?? 3000); + + app.useGlobalPipes( + new ValidationPipe({ + whitelist: true, // 自动剥离未声明的属性 + forbidNonWhitelisted: true, // 收到未声明属性时报错 + transform: true, // 自动类型转换 + }), + ); + + app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector))); + + await app.listen(3000); + console.log('Application is running on: http://localhost:3000'); } + bootstrap(); diff --git a/tsconfig.json b/tsconfig.json index 950af17..c61c8c3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,9 +18,9 @@ "skipLibCheck": true, "strictNullChecks": true, "forceConsistentCasingInFileNames": true, - "noImplicitAny": false, - "strictBindCallApply": false, - "noFallthroughCasesInSwitch": false, + "noImplicitAny": true, + "strictBindCallApply": true, + "noFallthroughCasesInSwitch": true, "paths": { "@/*": ["src/*"] }