create-dp-koa 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# 企业级数据库架构重构文档
|
|
2
|
+
|
|
3
|
+
## 📋 概述
|
|
4
|
+
|
|
5
|
+
本文档描述了基于企业级标准的数据库架构重构,实现了更好的分层、解耦和可维护性。
|
|
6
|
+
|
|
7
|
+
## 🏗️ 架构层次
|
|
8
|
+
|
|
9
|
+
### 1. 实体层 (Entity Layer)
|
|
10
|
+
- **位置**: `src/entity/`
|
|
11
|
+
- **职责**: 定义数据模型和实体关系
|
|
12
|
+
- **特点**:
|
|
13
|
+
- 继承 `BaseEntity` 提供统一基础字段
|
|
14
|
+
- 使用 TypeORM 装饰器定义关系
|
|
15
|
+
- 支持外键约束和索引优化
|
|
16
|
+
|
|
17
|
+
### 2. Repository 层
|
|
18
|
+
- **位置**: `src/repository/`
|
|
19
|
+
- **职责**: 数据访问抽象和封装
|
|
20
|
+
- **特点**:
|
|
21
|
+
- 接口与实现分离 (`IBaseRepository`, `BaseRepository`)
|
|
22
|
+
- 提供通用的 CRUD 操作
|
|
23
|
+
- 支持复杂查询和业务逻辑
|
|
24
|
+
|
|
25
|
+
### 3. DTO 验证层
|
|
26
|
+
- **位置**: `src/dto/`
|
|
27
|
+
- **职责**: 数据传输对象和验证
|
|
28
|
+
- **特点**:
|
|
29
|
+
- 使用 `class-validator` 进行数据验证
|
|
30
|
+
- 支持创建、更新、查询等不同场景的 DTO
|
|
31
|
+
- 提供详细的错误信息
|
|
32
|
+
|
|
33
|
+
### 4. 服务层 (Service Layer)
|
|
34
|
+
- **位置**: `src/service/`
|
|
35
|
+
- **职责**: 业务逻辑处理
|
|
36
|
+
- **特点**:
|
|
37
|
+
- 使用 Repository 模式进行数据访问
|
|
38
|
+
- 集成事务管理
|
|
39
|
+
- 支持 DTO 验证和错误处理
|
|
40
|
+
|
|
41
|
+
### 5. 控制器层 (Controller Layer)
|
|
42
|
+
- **位置**: `src/controllers/`
|
|
43
|
+
- **职责**: HTTP 请求处理和响应
|
|
44
|
+
- **特点**:
|
|
45
|
+
- 使用装饰器进行缓存管理
|
|
46
|
+
- 统一的响应格式
|
|
47
|
+
- 完整的错误处理
|
|
48
|
+
|
|
49
|
+
## 🔧 核心组件
|
|
50
|
+
|
|
51
|
+
### 1. 事务管理器 (`TransactionManager`)
|
|
52
|
+
```typescript
|
|
53
|
+
// 使用示例
|
|
54
|
+
await transactionManager.executeInTransaction(async (manager) => {
|
|
55
|
+
// 业务逻辑
|
|
56
|
+
await userRepository.save(user);
|
|
57
|
+
await orderRepository.save(order);
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**特性**:
|
|
62
|
+
- 支持不同隔离级别
|
|
63
|
+
- 嵌套事务支持
|
|
64
|
+
- 只读事务优化
|
|
65
|
+
- 批量操作事务
|
|
66
|
+
|
|
67
|
+
### 2. 缓存管理器 (`CacheManager`)
|
|
68
|
+
```typescript
|
|
69
|
+
// 使用示例
|
|
70
|
+
@Cacheable({ ttl: 300 })
|
|
71
|
+
async getUserById(id: number) {
|
|
72
|
+
return await this.userRepository.findById(id);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@CacheEvict({ keys: ['user:${id}'] })
|
|
76
|
+
async updateUser(id: number, data: any) {
|
|
77
|
+
// 更新逻辑
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**特性**:
|
|
82
|
+
- 支持内存缓存和 Redis 缓存
|
|
83
|
+
- 装饰器模式简化使用
|
|
84
|
+
- 自动缓存键生成
|
|
85
|
+
- 批量缓存操作
|
|
86
|
+
|
|
87
|
+
### 3. DTO 验证器 (`DtoValidator`)
|
|
88
|
+
```typescript
|
|
89
|
+
// 使用示例
|
|
90
|
+
const validation = await DtoValidator.validateAndTransform(CreateUserDto, data);
|
|
91
|
+
if (!validation.isValid) {
|
|
92
|
+
return new CommonServiceResult(VALIDATION_ERROR, null, validation.errors.join(', '));
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**特性**:
|
|
97
|
+
- 自动类型转换
|
|
98
|
+
- 详细的错误信息
|
|
99
|
+
- 批量验证支持
|
|
100
|
+
- 白名单过滤
|
|
101
|
+
|
|
102
|
+
### 4. 迁移管理器 (`MigrationManager`)
|
|
103
|
+
```typescript
|
|
104
|
+
// 使用示例
|
|
105
|
+
// 生产环境自动执行迁移
|
|
106
|
+
if (process.env.NODE_ENV === 'production') {
|
|
107
|
+
await migrationManager.runMigrations();
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**特性**:
|
|
112
|
+
- 环境特定配置
|
|
113
|
+
- 自动迁移执行
|
|
114
|
+
- 迁移状态监控
|
|
115
|
+
- 数据库备份支持
|
|
116
|
+
|
|
117
|
+
## 📊 数据库设计
|
|
118
|
+
|
|
119
|
+
### 实体关系图
|
|
120
|
+
```
|
|
121
|
+
YtUserEntity (1) ←→ (1) ShopAndUserEntity (1) ←→ (1) ShopEntity
|
|
122
|
+
↓ (1) ↓ (1)
|
|
123
|
+
↓ (N) ↓ (N)
|
|
124
|
+
YtGoodsUnlockerEntity YtGoodsEntity
|
|
125
|
+
↓ (1) ↓ (1)
|
|
126
|
+
↓ (N) ↓ (N)
|
|
127
|
+
YtGoodsEntity GoodsImagesUnlockKeyEntity
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 外键约束
|
|
131
|
+
- `shop_and_user.shopId` → `shop.id`
|
|
132
|
+
- `shop_and_user.userId` → `yt_user.id`
|
|
133
|
+
- `yt_goods.shopId` → `shop.id`
|
|
134
|
+
- `yt_goods_unlock.goodsId` → `yt_goods.id`
|
|
135
|
+
- `yt_goods_unlock.ytUserId` → `yt_user.id`
|
|
136
|
+
- `goods_images_unlock_key.goodsId` → `yt_goods.id`
|
|
137
|
+
|
|
138
|
+
## 🚀 使用指南
|
|
139
|
+
|
|
140
|
+
### 1. 创建新的实体
|
|
141
|
+
```typescript
|
|
142
|
+
@Entity({ name: "new_entity" })
|
|
143
|
+
export class NewEntity extends BaseEntity {
|
|
144
|
+
@Column({ default: "" })
|
|
145
|
+
name: string;
|
|
146
|
+
|
|
147
|
+
@ManyToOne(() => YtUserEntity, user => user.relations)
|
|
148
|
+
@JoinColumn({ name: 'userId' })
|
|
149
|
+
user: YtUserEntity;
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### 2. 创建 Repository
|
|
154
|
+
```typescript
|
|
155
|
+
export interface INewEntityRepository extends IBaseRepository<NewEntity> {
|
|
156
|
+
findByUserId(userId: number): Promise<NewEntity[]>;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export class NewEntityRepository extends BaseRepository<NewEntity> implements INewEntityRepository {
|
|
160
|
+
async findByUserId(userId: number): Promise<NewEntity[]> {
|
|
161
|
+
return await this.repository.find({ where: { userId } });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 3. 创建 DTO
|
|
167
|
+
```typescript
|
|
168
|
+
export class CreateNewEntityDto {
|
|
169
|
+
@IsNotEmpty({ message: '名称不能为空' })
|
|
170
|
+
@IsString({ message: '名称必须是字符串' })
|
|
171
|
+
name: string;
|
|
172
|
+
|
|
173
|
+
@IsNotEmpty({ message: '用户ID不能为空' })
|
|
174
|
+
@IsInt({ message: '用户ID必须是整数' })
|
|
175
|
+
userId: number;
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 4. 创建服务
|
|
180
|
+
```typescript
|
|
181
|
+
@Injectable()
|
|
182
|
+
export class NewEntityService {
|
|
183
|
+
private repository: INewEntityRepository;
|
|
184
|
+
|
|
185
|
+
async createEntity(dto: CreateNewEntityDto): Promise<CommonServiceResult<NewEntity>> {
|
|
186
|
+
const validation = await DtoValidator.validateAndTransform(CreateNewEntityDto, dto);
|
|
187
|
+
if (!validation.isValid) {
|
|
188
|
+
return new CommonServiceResult(VALIDATION_ERROR, null, validation.errors.join(', '));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const entity = await transactionManager.executeInTransaction(async (manager) => {
|
|
192
|
+
return await this.repository.save(validation.data);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
return new CommonServiceResult(SUCCESS, entity, "创建成功");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### 5. 创建控制器
|
|
201
|
+
```typescript
|
|
202
|
+
@Controller('/api/v1/new-entities')
|
|
203
|
+
export class NewEntityController extends BaseController {
|
|
204
|
+
@Post('/')
|
|
205
|
+
@CacheEvict({ keys: ['new-entity:list'] })
|
|
206
|
+
async createEntity(@Body() dto: CreateNewEntityDto) {
|
|
207
|
+
const result = await this.newEntityService.createEntity(dto);
|
|
208
|
+
return result.code === 'SUCCESS' ? this.success(result.data) : this.fail(400, result.message);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
@Get('/')
|
|
212
|
+
@Cacheable({ ttl: 60 })
|
|
213
|
+
async getEntities(@Query() query: QueryNewEntityDto) {
|
|
214
|
+
const result = await this.newEntityService.getEntities(query);
|
|
215
|
+
return this.success(result.data);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## 🔒 安全特性
|
|
221
|
+
|
|
222
|
+
### 1. 数据验证
|
|
223
|
+
- 输入数据验证和清理
|
|
224
|
+
- SQL 注入防护
|
|
225
|
+
- XSS 攻击防护
|
|
226
|
+
|
|
227
|
+
### 2. 事务安全
|
|
228
|
+
- ACID 特性保证
|
|
229
|
+
- 死锁检测和处理
|
|
230
|
+
- 连接池管理
|
|
231
|
+
|
|
232
|
+
### 3. 缓存安全
|
|
233
|
+
- 缓存键命名规范
|
|
234
|
+
- 敏感数据不缓存
|
|
235
|
+
- 缓存过期策略
|
|
236
|
+
|
|
237
|
+
## 📈 性能优化
|
|
238
|
+
|
|
239
|
+
### 1. 数据库优化
|
|
240
|
+
- 索引优化
|
|
241
|
+
- 查询优化
|
|
242
|
+
- 连接池配置
|
|
243
|
+
|
|
244
|
+
### 2. 缓存优化
|
|
245
|
+
- 多级缓存策略
|
|
246
|
+
- 缓存预热
|
|
247
|
+
- 缓存穿透防护
|
|
248
|
+
|
|
249
|
+
### 3. 事务优化
|
|
250
|
+
- 只读事务
|
|
251
|
+
- 批量操作
|
|
252
|
+
- 嵌套事务
|
|
253
|
+
|
|
254
|
+
## 🧪 测试策略
|
|
255
|
+
|
|
256
|
+
### 1. 单元测试
|
|
257
|
+
- Repository 层测试
|
|
258
|
+
- Service 层测试
|
|
259
|
+
- DTO 验证测试
|
|
260
|
+
|
|
261
|
+
### 2. 集成测试
|
|
262
|
+
- 数据库集成测试
|
|
263
|
+
- 事务测试
|
|
264
|
+
- 缓存测试
|
|
265
|
+
|
|
266
|
+
### 3. 性能测试
|
|
267
|
+
- 并发测试
|
|
268
|
+
- 压力测试
|
|
269
|
+
- 缓存性能测试
|
|
270
|
+
|
|
271
|
+
## 📝 最佳实践
|
|
272
|
+
|
|
273
|
+
### 1. 代码规范
|
|
274
|
+
- 使用 TypeScript 严格模式
|
|
275
|
+
- 遵循 SOLID 原则
|
|
276
|
+
- 统一的错误处理
|
|
277
|
+
|
|
278
|
+
### 2. 数据库规范
|
|
279
|
+
- 命名规范统一
|
|
280
|
+
- 索引策略合理
|
|
281
|
+
- 外键约束完整
|
|
282
|
+
|
|
283
|
+
### 3. 缓存规范
|
|
284
|
+
- 缓存键命名规范
|
|
285
|
+
- TTL 设置合理
|
|
286
|
+
- 缓存失效策略
|
|
287
|
+
|
|
288
|
+
## 🔄 迁移指南
|
|
289
|
+
|
|
290
|
+
### 从旧架构迁移
|
|
291
|
+
1. 逐步替换 Service 层使用 Repository
|
|
292
|
+
2. 添加 DTO 验证
|
|
293
|
+
3. 集成事务管理
|
|
294
|
+
4. 添加缓存策略
|
|
295
|
+
5. 执行数据库迁移
|
|
296
|
+
|
|
297
|
+
### 环境配置
|
|
298
|
+
```bash
|
|
299
|
+
# 开发环境
|
|
300
|
+
NODE_ENV=development
|
|
301
|
+
CACHE_TYPE=memory
|
|
302
|
+
|
|
303
|
+
# 生产环境
|
|
304
|
+
NODE_ENV=production
|
|
305
|
+
CACHE_TYPE=redis
|
|
306
|
+
REDIS_HOST=localhost
|
|
307
|
+
REDIS_PORT=6379
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## 📚 相关文档
|
|
311
|
+
|
|
312
|
+
- [Repository 模式详解](./docs/REPOSITORY_PATTERN.md)
|
|
313
|
+
- [事务管理指南](./docs/TRANSACTION_GUIDE.md)
|
|
314
|
+
- [缓存策略说明](./docs/CACHE_STRATEGY.md)
|
|
315
|
+
- [DTO 验证规范](./docs/DTO_VALIDATION.md)
|
|
316
|
+
- [数据库迁移指南](./docs/MIGRATION_GUIDE.md)
|
|
317
|
+
|
|
318
|
+
|