摘要
经过前期的系统规划和自身技术能力的考究,最终选定基础架构: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
构建出来的目录结构如下,其中我们主要开发目录在 src
4.改造目录结构
如下图所示,common
来处理全局的数据,config
来配置全局env声明,modules
是我们开发主要模块目录,share
开发公共模块,utils
用来存储常用工具方法集合
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)
6.1 常量
系统相关常量
存储,例如是否转换数据
、是否开放权限
、是否忽略日志
及缓存前缀
等
6.2 装饰器
目前全局装饰器只处理了 Request
相关内容
6.3 DTO
公共 DTO
存储目录,例如 BaseApiResponse
响应基础
6.4 异常
6.5 过滤器
6.6 拦截器
6.7 中间件
数据处理中间件,目前仅有 acme-challenge
的中间件
6.8 管道
存放 params
传入数据校验相关内容
6.9 策略
授权策略
6.10 验证器
自定义数据验证器
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
来读取相关配置
系列文章
- Nestjs构建Certeasy证书自动化平台 - 介绍
- Nestjs构建Certeasy证书自动化平台 - 框架搭建
- Nestjs构建Certeasy证书自动化平台 - 业务实现(登录注册)
- Nestjs构建Certeasy证书自动化平台 - 业务实现(证书模块)
- Nestjs构建Certeasy证书自动化平台 - 业务实现(DNS授权模块)
- Nestjs构建Certeasy证书自动化平台 - 业务实现(云资源模块)
- Nestjs构建Certeasy证书自动化平台 - 业务实现(证书监控模块)
- Nestjs构建Certeasy证书自动化平台 - 业务实现(用户模块)
- Nestjs构建Certeasy证书自动化平台 - 业务实现(通知模块)
- Nestjs构建Certeasy证书自动化平台 - 业务实现(充值模块)
开源
-
Monorepo:gitee.com: certeasy_nest_monorepo
联系
wechat: zuxcloud
Email: zuxing.xu@lettered.cn