Nestjs构建Certeasy证书自动化平台 - 框架搭建

112 阅读3分钟

1_zjbQKzeIt3UM1ezHkDvHNw.webp

摘要

经过前期的系统规划和自身技术能力的考究,最终选定基础架构:NestJS + TypeORM + MySQL

所以本篇讲述从NestJS的搭建(涵盖ENV配置输入处理输出处理日志处理及异常处理),到TypeORM 依赖引入和链接到MySQL的整体流程

NestJS 环境

1.前言

开发系统前,默认你有了解nodejs的基础能力,有使用npm等依赖管理工具的能力及有相关的开发能力,本人不会从头开始教学从环境搭建到系统构建。

2.Nest起始

根据官方文档Documentation | NestJS - A progressive Node.js framework说明,需要先安装好CLI工具方可构建基础模板

$ npm i -g @nestjs/cli
$ nest new project-name

又或者使用GIT工具拉取基础模板仓库

$ git clone https://github.com/nestjs/typescript-starter.git project
$ cd project
$ npm install
$ npm run start

3.构建基础模板

# 1. 搭建模板目录
nest new certeay_nest_open

微信截图_20240926114225.png

构建出来的目录结构如下,其中我们主要开发目录在 src

image.png

4.改造目录结构

如下图所示,common 来处理全局的数据,config 来配置全局env声明,modules 是我们开发主要模块目录,share 开发公共模块,utils 用来存储常用工具方法集合

image.png

5.引入配置依赖和创建配置文件

根据官方文档:Configuration | NestJS - A progressive Node.js framework

$ npm i --save @nestjs/config

创建配置文件和导出配置

  • /src/config/configuration.ts
export default (): any => ({
  app: {
    env: process.env.APP_ENV,
    host: process.env.APP_HOST || '0.0.0.0',
    port: process.env.APP_PORT,
    domain: process.env.APP_DOMAIN,
  },
  .....
});
  • /src/config/index.ts
import { ConfigModuleOptions } from '@nestjs/config';

// dayjs 全局设置
import * as dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
dayjs.locale('zh-cn'); // 全局使用

// 读取配置
import configuration from './configuration';
// 加载配置
export const configModuleOptions: ConfigModuleOptions = {
  envFilePath: `.env.${process.env.NODE_ENV || 'development'}`,
  load: [configuration],
  isGlobal: true,
};

主模块引入 app.module.ts

// configModuleOptions
import { configModuleOptions } from './config';

......
@Module({
  imports: [
  ConfigModule.forRoot(configModuleOptions),

......

6. common 公共功能就不一一讲解,请查看开源代码仓库

certeasy_nest_open/src/common at main · CerteasyTeam/certeasy_nest_open (github.com)

image.png

6.1 常量

系统相关常量存储,例如是否转换数据是否开放权限是否忽略日志缓存前缀

common_constants.png

6.2 装饰器

目前全局装饰器只处理了 Request 相关内容

image.png

6.3 DTO

公共 DTO 存储目录,例如 BaseApiResponse响应基础

image.png

6.4 异常

image.png

6.5 过滤器

image.png

6.6 拦截器

image.png

6.7 中间件

数据处理中间件,目前仅有 acme-challenge 的中间件

image.png

6.8 管道

存放 params 传入数据校验相关内容

image.png

6.9 策略

授权策略

image.png

6.10 验证器

自定义数据验证器

image.png

TypeORM 使用

TypeORM - 一个 ORM 框架 | TypeORM 中文文档 | TypeORM 中文网 (bootcss.com)

1. 依赖

根据 NestJS 文档指引 Database | NestJS - A progressive Node.js framework

$ npm install --save @nestjs/typeorm typeorm mysql2

2.使用 TypeORM

依赖安装完成后,在 app.module.ts 内引入。我们系统需要从configService读取响应的配置数据进行数据库链接,因此使用 TypeOrmModule.forRootAsync。 具体如下

+ import { ConfigModule, ConfigService } from '@nestjs/config';
+ import { TypeOrmModule } from '@nestjs/typeorm';

......
TypeOrmModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: async (configService: ConfigService) => {
    return {
      type: configService.get<'mysql' | 'mariadb'>('database.type', 'mysql'),
      host: configService.get<string>('database.host', '127.0.0.1'),
      port: configService.get<number | undefined>('database.port', 3306),
      database: configService.get<string>('database.name', 'certeasy'),
      username: configService.get<string>('database.user', 'root'),
      password: configService.get<string>('database.pass', '123456'),
      entityPrefix: configService.get<string>('database.prefix', 'ce_'),
      entities: [], // 实体不在此设置,在每个模块配置
      timezone: '+08:00', // 时区
      dateStrings: true,
      autoLoadEntities: true,
      synchronize: false, // 同步
      insecureAuth: true, //  允许连接到要求旧(不安全)身份验证方法的 MySQL 实例
      supportBigNumbers: true,
      logging:
        configService.get<string>('app.env', 'development') === 'development',
      debug:
        configService.get<string>('app.env', 'development') === 'development',
    };
  },
}),

在此处引入是全局的,后续在其他modules内使用可直接引入 configService 来读取相关配置

系列文章

开源

联系

wechat: zuxcloud

Email: zuxing.xu@lettered.cn