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.
Files changed (235) hide show
  1. package/README.md +50 -0
  2. package/index.mjs +97 -0
  3. package/package.json +33 -0
  4. package/template/.env.development +9 -0
  5. package/template/.env.production +12 -0
  6. package/template/.github/workflows/ci-cd.yml +182 -0
  7. package/template/.trae/documents/controller_development_plan.md +386 -0
  8. package/template/.trae/skills/00-backend-core.skill.md +50 -0
  9. package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
  10. package/template/.trae/skills/10-backend-api.skill.md +54 -0
  11. package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
  12. package/template/.trae/skills/20-backend-repository.skill.md +25 -0
  13. package/template/.trae/skills/21-backend-service.skill.md +135 -0
  14. package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
  15. package/template/.trae/skills/30-backend-validation.skill.md +320 -0
  16. package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
  17. package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
  18. package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
  19. package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
  20. package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
  21. package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
  22. package/template/.trae/skills/90-backend-testing.skill.md +29 -0
  23. package/template/.trae/skills/README.md +49 -0
  24. package/template/.vscode/launch.json +38 -0
  25. package/template/.vscode/settings.json +1 -0
  26. package/template/Dockerfile +36 -0
  27. package/template/README.md +229 -0
  28. package/template/docker-compose.yml +135 -0
  29. package/template/docs/API_DOCUMENTATION.md +837 -0
  30. package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
  31. package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
  32. package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
  33. package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
  34. package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
  35. package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
  36. package/template/docs/DOCUMENTATION_INDEX.md +294 -0
  37. package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
  38. package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
  39. package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
  40. package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
  41. package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
  42. package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
  43. package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
  44. package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
  45. package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
  46. package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
  47. package/template/docs/INSTALLATION_GUIDE.md +611 -0
  48. package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
  49. package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
  50. package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
  51. package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
  52. package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
  53. package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
  54. package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
  55. package/template/docs/QUICK_START.md +268 -0
  56. package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
  57. package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
  58. package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
  59. package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
  60. package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
  61. package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
  62. package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
  63. package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
  64. package/template/docs/TROUBLESHOOTING.md +869 -0
  65. package/template/env.production.example +62 -0
  66. package/template/jest.config.js +34 -0
  67. package/template/package-lock.json +13240 -0
  68. package/template/package.json +119 -0
  69. package/template/patches/typeorm+0.3.25.patch +22 -0
  70. package/template/scripts/sync-template.mjs +84 -0
  71. package/template/scripts/test-annotation-system.sh +48 -0
  72. package/template/scripts/test-core-functionality.sh +28 -0
  73. package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
  74. package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
  75. package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
  76. package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
  77. package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
  78. package/template/src/annotations/index.ts +50 -0
  79. package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
  80. package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
  81. package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
  82. package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
  83. package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
  84. package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
  85. package/template/src/annotations/processors/index.ts +10 -0
  86. package/template/src/app.ts +123 -0
  87. package/template/src/controllers/base.controller.ts +41 -0
  88. package/template/src/controllers/cacheManagement.controller.ts +131 -0
  89. package/template/src/controllers/captcha.controller.ts +57 -0
  90. package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
  91. package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
  92. package/template/src/controllers/example/ExampleController.ts +110 -0
  93. package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
  94. package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
  95. package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
  96. package/template/src/controllers/health.controller.ts +235 -0
  97. package/template/src/controllers/home/register.controller.ts +58 -0
  98. package/template/src/controllers/home/ytGoods.controller.ts +92 -0
  99. package/template/src/controllers/home/ytShop.controller.ts +135 -0
  100. package/template/src/controllers/home/ytUser.controller.ts +89 -0
  101. package/template/src/controllers/logManagement.controller.ts +396 -0
  102. package/template/src/controllers/public/emailSend.controller.ts +65 -0
  103. package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
  104. package/template/src/controllers/testData.controller.ts +253 -0
  105. package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
  106. package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
  107. package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
  108. package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
  109. package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
  110. package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
  111. package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
  112. package/template/src/dto/goods.dto.ts +212 -0
  113. package/template/src/dto/service/ytService.dto.ts +13 -0
  114. package/template/src/dto/user.dto.ts +177 -0
  115. package/template/src/entity/base.entity.ts +13 -0
  116. package/template/src/entity/columnTypes.ts +13 -0
  117. package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
  118. package/template/src/entity/goodsUnlocker.entity.ts +34 -0
  119. package/template/src/entity/index.ts +15 -0
  120. package/template/src/entity/shop.entity.ts +52 -0
  121. package/template/src/entity/shopUser.entity.ts +41 -0
  122. package/template/src/entity/ytGoods.entity.ts +94 -0
  123. package/template/src/entity/ytUser.entity.ts +96 -0
  124. package/template/src/examples/InterceptorExampleRunner.ts +284 -0
  125. package/template/src/examples/ServiceInterceptorExample.ts +214 -0
  126. package/template/src/examples/SwaggerProcessorExample.ts +169 -0
  127. package/template/src/examples/TransactionManagerDemo.ts +377 -0
  128. package/template/src/examples/cacheExamples.ts +155 -0
  129. package/template/src/framework/decorator/controller.ts +311 -0
  130. package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
  131. package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
  132. package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
  133. package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
  134. package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
  135. package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
  136. package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
  137. package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
  138. package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
  139. package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
  140. package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
  141. package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
  142. package/template/src/framework/decorator/refactored/index.ts +98 -0
  143. package/template/src/framework/decorator/swagger.ts +150 -0
  144. package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
  145. package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
  146. package/template/src/framework/interceptors/index.ts +19 -0
  147. package/template/src/framework/plugins/registry.ts +63 -0
  148. package/template/src/framework/plugins/types.ts +15 -0
  149. package/template/src/framework/types/ServiceResult.ts +151 -0
  150. package/template/src/framework/types/index.ts +16 -0
  151. package/template/src/framework/utils/CacheManager.ts +430 -0
  152. package/template/src/framework/utils/CacheService.ts +248 -0
  153. package/template/src/framework/utils/DtoValidator.ts +164 -0
  154. package/template/src/framework/utils/MigrationHelper.ts +179 -0
  155. package/template/src/framework/utils/MigrationManager.ts +256 -0
  156. package/template/src/framework/utils/NewRouter.ts +207 -0
  157. package/template/src/framework/utils/TransactionManager.ts +172 -0
  158. package/template/src/framework/utils/bootstrap.ts +445 -0
  159. package/template/src/framework/utils/cache.ts +269 -0
  160. package/template/src/framework/utils/databaseConfig.ts +148 -0
  161. package/template/src/framework/utils/db.ts +39 -0
  162. package/template/src/framework/utils/dbMonitor.ts +106 -0
  163. package/template/src/framework/utils/dynamicSwagger.ts +410 -0
  164. package/template/src/framework/utils/function.ts +61 -0
  165. package/template/src/framework/utils/gracefulShutdown.ts +131 -0
  166. package/template/src/framework/utils/logger.ts +388 -0
  167. package/template/src/framework/utils/metrics.ts +182 -0
  168. package/template/src/framework/utils/router.ts +417 -0
  169. package/template/src/framework/utils/swagger.ts +184 -0
  170. package/template/src/framework/utils/testDb.ts +19 -0
  171. package/template/src/framework/utils/token.ts +23 -0
  172. package/template/src/framework/utils/transform.ts +17 -0
  173. package/template/src/libs/aokEmailSender.ts +42 -0
  174. package/template/src/libs/captcha.ts +37 -0
  175. package/template/src/libs/cos.ts +45 -0
  176. package/template/src/libs/mCache.ts +7 -0
  177. package/template/src/libs/serviceValidate.ts +3 -0
  178. package/template/src/libs/tecentSms.ts +51 -0
  179. package/template/src/middlewares/a.middleware.ts +6 -0
  180. package/template/src/middlewares/error.middleware.ts +14 -0
  181. package/template/src/middlewares/logging.middleware.ts +187 -0
  182. package/template/src/middlewares/static.middleware.ts +79 -0
  183. package/template/src/middlewares/swagger.middleware.ts +70 -0
  184. package/template/src/middlewares/token.middleware.ts +32 -0
  185. package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
  186. package/template/src/migrations/index.ts +6 -0
  187. package/template/src/plugins/weboffice/core/context.ts +47 -0
  188. package/template/src/plugins/weboffice/core/errors.ts +51 -0
  189. package/template/src/plugins/weboffice/core/types.ts +63 -0
  190. package/template/src/plugins/weboffice/core/utils.ts +7 -0
  191. package/template/src/plugins/weboffice/entities/index.ts +3 -0
  192. package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
  193. package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
  194. package/template/src/plugins/weboffice/http/routes.ts +179 -0
  195. package/template/src/plugins/weboffice/index.ts +23 -0
  196. package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
  197. package/template/src/repository/UserRepository.ts +122 -0
  198. package/template/src/repository/base/BaseRepository.ts +124 -0
  199. package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
  200. package/template/src/routers/index.ts +49 -0
  201. package/template/src/service/base.service.ts +116 -0
  202. package/template/src/service/paramValidateTest.service.ts +139 -0
  203. package/template/src/service/ytGoods.service.ts +42 -0
  204. package/template/src/service/ytShop.service.ts +90 -0
  205. package/template/src/service/ytUser.service.ts +451 -0
  206. package/template/src/test/swaggerParameterTest.ts +90 -0
  207. package/template/src/utils/testDataInitializer.ts +296 -0
  208. package/template/static/output.json +15203 -0
  209. package/template/test/controllers/controllers.test.ts +173 -0
  210. package/template/test/controllers/example/ExampleController.test.ts +222 -0
  211. package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
  212. package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
  213. package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
  214. package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
  215. package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
  216. package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
  217. package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
  218. package/template/test/framework/annotation/NewRouter.test.ts +272 -0
  219. package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
  220. package/template/test/framework/annotation/setup.ts +26 -0
  221. package/template/test/framework/cache.test.ts +101 -0
  222. package/template/test/framework/databaseConfig.test.ts +142 -0
  223. package/template/test/integration/integration.test.ts +153 -0
  224. package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
  225. package/template/test/service/business.test.ts +87 -0
  226. package/template/test/service/paramValidateTest.service.test.ts +184 -0
  227. package/template/test/service/ytUser.service.test.ts +566 -0
  228. package/template/test/setup.ts +20 -0
  229. package/template/test/setupAfterEnv.ts +14 -0
  230. package/template/test/utils/testHelpers.ts +220 -0
  231. package/template/test_output.txt +0 -0
  232. package/template/tsconfig.build.json +17 -0
  233. package/template/tsconfig.json +31 -0
  234. package/template/webpack.config.js +71 -0
  235. package/template/yarn.lock +7354 -0
@@ -0,0 +1,364 @@
1
+ # NewRouter 集成使用指南
2
+
3
+ ## 1. 快速开始
4
+
5
+ ### 1.1 环境配置
6
+
7
+ 在项目根目录创建环境配置文件:
8
+
9
+ ```bash
10
+ # .env.development
11
+ USE_NEW_ANNOTATION_SYSTEM=1
12
+
13
+ # .env.production
14
+ USE_NEW_ANNOTATION_SYSTEM=0
15
+
16
+ # .env.test
17
+ USE_NEW_ANNOTATION_SYSTEM=1
18
+ ```
19
+
20
+ ### 1.2 应用启动
21
+
22
+ 新注解系统会在应用启动时自动初始化:
23
+
24
+ ```typescript
25
+ // src/app.ts
26
+ import { MigrationHelper } from "./framework/utils/MigrationHelper";
27
+
28
+ setBeforeBootstrap(async function () {
29
+ // 自动初始化新注解处理器系统
30
+ MigrationHelper.initializeNewSystem();
31
+
32
+ // 记录系统状态
33
+ const systemStatus = MigrationHelper.getSystemStatus();
34
+ logger.info(`注解系统状态: ${systemStatus.newSystemEnabled ? '新系统' : '旧系统'}`);
35
+
36
+ // ... 其他初始化代码
37
+ });
38
+ ```
39
+
40
+ ## 2. 控制器开发
41
+
42
+ ### 2.1 基础用法
43
+
44
+ ```typescript
45
+ import { Get, Post, Query, Body } from '../../framework/decorator/controller';
46
+ import { Logging, Permission, RateLimit } from '../../framework/decorator/processor/AnnotationDecorators';
47
+ import { BaseController } from '../base.controller';
48
+
49
+ export class MyController extends BaseController {
50
+ @Get('/data')
51
+ @Logging()
52
+ @Permission({ requiredPermission: 'read:data' })
53
+ @RateLimit({ maxRequests: 10, windowMs: 60000 })
54
+ async getData(@Query() query: any): Promise<any> {
55
+ return this.success({ data: 'example' });
56
+ }
57
+ }
58
+ ```
59
+
60
+ ### 2.2 注解组合使用
61
+
62
+ ```typescript
63
+ export class AdvancedController extends BaseController {
64
+ @Post('/submit')
65
+ @Logging() // 记录请求日志
66
+ @Permission({ requiredPermission: 'write:data' }) // 权限检查
67
+ @RateLimit({ maxRequests: 5, windowMs: 300000 }) // 限流:5分钟内最多5次
68
+ @ResponseCode(201) // 设置响应码
69
+ @ResponseHeader('X-Custom-Header', 'value') // 设置响应头
70
+ async submitData(@Body() body: any): Promise<any> {
71
+ // 业务逻辑
72
+ return this.success({ message: 'submitted' });
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### 2.3 新旧注解混合使用
78
+
79
+ ```typescript
80
+ export class MixedController extends BaseController {
81
+ @Get('/legacy-compatible')
82
+ @Logging() // 新注解
83
+ @ResponseCode(200) // 旧注解
84
+ @ResponseHeader('X-Legacy', 'true') // 旧注解
85
+ async legacyCompatible(@Query() query: any): Promise<any> {
86
+ return this.success({ message: '兼容模式' });
87
+ }
88
+ }
89
+ ```
90
+
91
+ ## 3. 自定义注解处理器
92
+
93
+ ### 3.1 创建自定义处理器
94
+
95
+ ```typescript
96
+ // src/framework/decorator/processor/processors/CustomProcessors.ts
97
+ import { AnnotationProcessor } from '../AnnotationProcessor';
98
+ import { Context } from 'koa';
99
+
100
+ export class CustomProcessor implements AnnotationProcessor {
101
+ readonly name = 'CustomProcessor';
102
+ readonly priority = 50;
103
+
104
+ async process(
105
+ ctx: Context,
106
+ controller: any,
107
+ methodName: string,
108
+ annotationData: any,
109
+ callParams: any[]
110
+ ): Promise<boolean> {
111
+ // 自定义处理逻辑
112
+ console.log(`执行自定义处理器: ${methodName}`);
113
+
114
+ // 返回 true 继续执行,false 中断执行
115
+ return true;
116
+ }
117
+ }
118
+ ```
119
+
120
+ ### 3.2 注册自定义处理器
121
+
122
+ ```typescript
123
+ // src/framework/decorator/processor/ProcessorManager.ts
124
+ import { CustomProcessor } from './processors/CustomProcessors';
125
+
126
+ export class ProcessorManager {
127
+ static initialize() {
128
+ // 注册默认处理器
129
+ // ...
130
+
131
+ // 注册自定义处理器
132
+ AnnotationExecutor.registerProcessor(new CustomProcessor());
133
+ }
134
+ }
135
+ ```
136
+
137
+ ### 3.3 创建自定义注解装饰器
138
+
139
+ ```typescript
140
+ // src/framework/decorator/processor/AnnotationDecorators.ts
141
+ import { createAnnotationDecorator } from './AnnotationDecorators';
142
+
143
+ export const CustomAnnotation = createAnnotationDecorator('CustomProcessor');
144
+ ```
145
+
146
+ ### 3.4 使用自定义注解
147
+
148
+ ```typescript
149
+ export class MyController extends BaseController {
150
+ @Get('/custom')
151
+ @CustomAnnotation({ customData: 'value' })
152
+ async customMethod(): Promise<any> {
153
+ return this.success({ message: 'custom' });
154
+ }
155
+ }
156
+ ```
157
+
158
+ ## 4. 系统管理
159
+
160
+ ### 4.1 检查系统状态
161
+
162
+ ```typescript
163
+ import { MigrationHelper } from './framework/utils/MigrationHelper';
164
+
165
+ // 获取系统状态
166
+ const status = MigrationHelper.getSystemStatus();
167
+ console.log('新系统启用:', status.newSystemEnabled);
168
+ console.log('系统已初始化:', status.newSystemInitialized);
169
+ console.log('当前环境:', status.environment);
170
+ ```
171
+
172
+ ### 4.2 切换注解系统
173
+
174
+ ```typescript
175
+ // 启用新系统
176
+ MigrationHelper.enableNewSystem();
177
+
178
+ // 禁用新系统
179
+ MigrationHelper.disableNewSystem();
180
+
181
+ // 切换系统
182
+ const isNewSystem = MigrationHelper.toggleSystem();
183
+ ```
184
+
185
+ ### 4.3 控制器兼容性检查
186
+
187
+ ```typescript
188
+ // 检查单个控制器
189
+ const compatibility = MigrationHelper.checkControllerCompatibility(controller);
190
+ console.log('兼容性:', compatibility.compatible);
191
+ console.log('问题:', compatibility.issues);
192
+ console.log('建议:', compatibility.recommendations);
193
+
194
+ // 生成迁移报告
195
+ const report = MigrationHelper.generateMigrationReport(controllers);
196
+ console.log('总控制器数:', report.total);
197
+ console.log('旧系统控制器:', report.legacy);
198
+ console.log('新系统控制器:', report.new);
199
+ console.log('混合系统控制器:', report.mixed);
200
+ ```
201
+
202
+ ## 5. 测试
203
+
204
+ ### 5.1 单元测试
205
+
206
+ ```typescript
207
+ import { TestDatabaseHelper } from '../utils/testHelpers';
208
+ import { MigrationHelper } from '../../../src/framework/utils/MigrationHelper';
209
+
210
+ describe('控制器测试', () => {
211
+ beforeAll(async () => {
212
+ await TestDatabaseHelper.initTestDatabase();
213
+ MigrationHelper.enableNewSystem();
214
+ });
215
+
216
+ test('应该能够执行控制器方法', async () => {
217
+ const controller = new MyController();
218
+ const result = await controller.getData({ id: '123' });
219
+
220
+ expect(result.code).toBe(0);
221
+ expect(result.data).toBeDefined();
222
+ });
223
+ });
224
+ ```
225
+
226
+ ### 5.2 集成测试
227
+
228
+ ```typescript
229
+ describe('新注解系统集成测试', () => {
230
+ test('应该能够处理复合注解', async () => {
231
+ const controller = new AdvancedController();
232
+ const result = await controller.submitData({ name: 'test' });
233
+
234
+ expect(result.code).toBe(0);
235
+ expect(result.data.message).toBe('submitted');
236
+ });
237
+ });
238
+ ```
239
+
240
+ ## 6. 性能优化
241
+
242
+ ### 6.1 处理器优先级
243
+
244
+ ```typescript
245
+ export class OptimizedProcessor implements AnnotationProcessor {
246
+ readonly name = 'OptimizedProcessor';
247
+ readonly priority = 10; // 较低优先级,在其他处理器之后执行
248
+
249
+ async process(): Promise<boolean> {
250
+ // 轻量级处理逻辑
251
+ return true;
252
+ }
253
+ }
254
+ ```
255
+
256
+ ### 6.2 缓存优化
257
+
258
+ ```typescript
259
+ export class CachedProcessor implements AnnotationProcessor {
260
+ private static cache = new Map();
261
+
262
+ async process(ctx: Context, controller: any, methodName: string, annotationData: any): Promise<boolean> {
263
+ const cacheKey = `${methodName}_${JSON.stringify(annotationData)}`;
264
+
265
+ if (CachedProcessor.cache.has(cacheKey)) {
266
+ // 使用缓存结果
267
+ return CachedProcessor.cache.get(cacheKey);
268
+ }
269
+
270
+ // 执行处理逻辑
271
+ const result = await this.executeLogic();
272
+
273
+ // 缓存结果
274
+ CachedProcessor.cache.set(cacheKey, result);
275
+
276
+ return result;
277
+ }
278
+ }
279
+ ```
280
+
281
+ ## 7. 故障排除
282
+
283
+ ### 7.1 常见问题
284
+
285
+ **问题1:注解处理器未执行**
286
+ ```typescript
287
+ // 检查处理器是否注册
288
+ const processor = ProcessorManager.getProcessor('ProcessorName');
289
+ if (!processor) {
290
+ console.error('处理器未注册');
291
+ }
292
+ ```
293
+
294
+ **问题2:权限检查失败**
295
+ ```typescript
296
+ // 检查用户权限
297
+ const user = ctx.state.user;
298
+ if (!user || !user.permissions) {
299
+ console.error('用户权限信息缺失');
300
+ }
301
+ ```
302
+
303
+ **问题3:限流不生效**
304
+ ```typescript
305
+ // 检查限流配置
306
+ const rateLimitData = { maxRequests: 10, windowMs: 60000 };
307
+ // 确保 windowMs 大于 0
308
+ ```
309
+
310
+ ### 7.2 调试技巧
311
+
312
+ ```typescript
313
+ // 启用详细日志
314
+ process.env.LOG_LEVEL = 'debug';
315
+
316
+ // 检查注解数据
317
+ console.log('控制器注解:', controller.$_Annotations);
318
+
319
+ // 检查处理器状态
320
+ const allProcessors = ProcessorManager.getAllProcessors();
321
+ console.log('已注册处理器:', allProcessors.map(p => p.name));
322
+ ```
323
+
324
+ ## 8. 最佳实践
325
+
326
+ ### 8.1 注解使用原则
327
+
328
+ 1. **单一职责**:每个注解处理器只负责一个功能
329
+ 2. **优先级合理**:根据执行顺序设置合适的优先级
330
+ 3. **性能考虑**:避免在处理器中执行耗时操作
331
+ 4. **错误处理**:处理器应该优雅地处理错误
332
+
333
+ ### 8.2 迁移策略
334
+
335
+ 1. **渐进式迁移**:先在新控制器中使用新注解
336
+ 2. **兼容性测试**:确保新旧系统可以共存
337
+ 3. **性能监控**:监控迁移过程中的性能变化
338
+ 4. **回滚准备**:准备快速回滚到旧系统的方案
339
+
340
+ ### 8.3 代码组织
341
+
342
+ ```typescript
343
+ // 按功能分组注解处理器
344
+ src/framework/decorator/processor/processors/
345
+ ├── DefaultProcessors.ts // 默认处理器
346
+ ├── CustomProcessors.ts // 自定义处理器
347
+ ├── SecurityProcessors.ts // 安全相关处理器
348
+ ├── PerformanceProcessors.ts // 性能相关处理器
349
+ └── BusinessProcessors.ts // 业务相关处理器
350
+ ```
351
+
352
+ ## 9. 总结
353
+
354
+ 新注解处理器系统提供了:
355
+
356
+ - **灵活性**:可以轻松创建自定义注解处理器
357
+ - **可扩展性**:支持插件式的功能扩展
358
+ - **兼容性**:与现有注解系统完全兼容
359
+ - **性能**:优化的执行流程和缓存机制
360
+ - **可维护性**:清晰的代码结构和职责分离
361
+
362
+ 通过合理使用新注解系统,可以显著提高代码的可读性、可维护性和可扩展性。
363
+
364
+
@@ -0,0 +1,268 @@
1
+ # 快速开始指南
2
+
3
+ 本指南将帮助您在 5 分钟内快速启动 DP-Koa Framework 项目。
4
+
5
+ ## 🎯 目标
6
+
7
+ 通过本指南,您将能够:
8
+ - 在本地环境中运行项目
9
+ - 访问 Swagger API 文档
10
+ - 运行基本的测试用例
11
+ - 了解项目的基本结构
12
+
13
+ ## 📋 前置条件
14
+
15
+ 确保您的系统已安装:
16
+ - Node.js 18.0.0 或更高版本
17
+ - npm 8.0.0 或更高版本
18
+ - Git
19
+
20
+ ### 检查版本
21
+
22
+ ```bash
23
+ node --version # 应该显示 v18.0.0 或更高
24
+ npm --version # 应该显示 8.0.0 或更高
25
+ git --version # 任何版本都可以
26
+ ```
27
+
28
+ ## 🚀 快速启动
29
+
30
+ ### 1. 克隆项目
31
+
32
+ ```bash
33
+ git clone <your-repository-url>
34
+ cd dp-koa-framework
35
+ ```
36
+
37
+ ### 2. 安装依赖
38
+
39
+ ```bash
40
+ npm install
41
+ ```
42
+
43
+ ### 3. 配置环境变量
44
+
45
+ ```bash
46
+ # 复制环境变量模板
47
+ cp env.production.example .env
48
+
49
+ # 编辑环境变量(可选,默认配置已足够用于快速开始)
50
+ # 默认使用内存数据库模式,无需额外配置
51
+ ```
52
+
53
+ ### 4. 启动开发服务器
54
+
55
+ ```bash
56
+ npm run dev
57
+ ```
58
+
59
+ ### 5. 验证启动
60
+
61
+ 打开浏览器访问:
62
+ - **应用首页**: http://localhost:3000
63
+ - **健康检查**: http://localhost:3000/health
64
+ - **Swagger API 文档**: http://localhost:3000/swagger-ui
65
+
66
+ ## 🧪 运行测试
67
+
68
+ ### 运行所有测试
69
+
70
+ ```bash
71
+ npm test
72
+ ```
73
+
74
+ ### 运行特定测试
75
+
76
+ ```bash
77
+ # 运行框架测试
78
+ npm run test:framework
79
+
80
+ # 运行拦截器测试
81
+ npm run test:interceptors
82
+
83
+ # 运行服务层测试
84
+ npm run test:service
85
+
86
+ # 运行集成测试
87
+ npm run test:integration
88
+ ```
89
+
90
+ ### 查看测试覆盖率
91
+
92
+ ```bash
93
+ npm run test:coverage
94
+ ```
95
+
96
+ ## 📁 项目结构
97
+
98
+ ```
99
+ dp-koa-framework/
100
+ ├── src/ # 源代码目录
101
+ │ ├── app.ts # 应用入口文件
102
+ │ ├── controllers/ # 控制器
103
+ │ ├── service/ # 服务层
104
+ │ ├── entity/ # 数据实体
105
+ │ ├── framework/ # 框架核心
106
+ │ │ ├── decorator/ # 装饰器系统
107
+ │ │ ├── interceptors/ # 拦截器
108
+ │ │ └── utils/ # 工具函数
109
+ │ └── examples/ # 示例代码
110
+ ├── test/ # 测试文件
111
+ ├── docs/ # 文档
112
+ ├── docker/ # Docker 配置
113
+ └── package.json # 项目配置
114
+ ```
115
+
116
+ ## 🔧 常用命令
117
+
118
+ ### 开发命令
119
+
120
+ ```bash
121
+ # 启动开发服务器(热重载)
122
+ npm run dev
123
+
124
+ # 构建生产版本
125
+ npm run build
126
+
127
+ # 启动生产服务器
128
+ npm start
129
+
130
+ # 类型检查
131
+ npm run type-check
132
+
133
+ # 代码检查
134
+ npm run lint
135
+
136
+ # 修复代码格式
137
+ npm run lint:fix
138
+ ```
139
+
140
+ ### Docker 命令
141
+
142
+ ```bash
143
+ # 构建 Docker 镜像
144
+ npm run docker:build
145
+
146
+ # 运行 Docker 容器
147
+ npm run docker:run
148
+
149
+ # 使用 Docker Compose 启动完整环境
150
+ npm run docker:compose:up
151
+
152
+ # 停止 Docker Compose 服务
153
+ npm run docker:compose:down
154
+
155
+ # 查看 Docker Compose 日志
156
+ npm run docker:compose:logs
157
+ ```
158
+
159
+ ### 健康检查
160
+
161
+ ```bash
162
+ # 检查应用健康状态
163
+ npm run health:check
164
+
165
+ # 检查监控指标
166
+ npm run metrics:check
167
+ ```
168
+
169
+ ## 🌟 快速体验
170
+
171
+ ### 1. 查看 API 文档
172
+
173
+ 访问 http://localhost:3000/swagger-ui 查看 Swagger 文档,您可以:
174
+ - 浏览所有可用的 API 端点
175
+ - 在线测试 API 接口
176
+ - 查看请求/响应示例
177
+
178
+ ### 2. 测试基本功能
179
+
180
+ ```bash
181
+ # 测试健康检查端点
182
+ curl http://localhost:3000/health
183
+
184
+ # 测试用户相关 API
185
+ curl http://localhost:3000/api/users
186
+
187
+ # 测试商品相关 API
188
+ curl http://localhost:3000/api/goods
189
+ ```
190
+
191
+ ### 3. 查看日志
192
+
193
+ ```bash
194
+ # 查看应用日志
195
+ tail -f logs/log.$(date +%Y-%m-%d).log
196
+
197
+ # 查看错误日志
198
+ tail -f logs/error.$(date +%Y-%m-%d).log
199
+
200
+ # 查看性能日志
201
+ tail -f logs/performance.$(date +%Y-%m-%d).log
202
+ ```
203
+
204
+ ## 🔍 故障排除
205
+
206
+ ### 常见问题
207
+
208
+ #### 1. 端口被占用
209
+
210
+ ```bash
211
+ # 检查端口占用
212
+ netstat -tulpn | grep :3000
213
+
214
+ # 杀死占用进程
215
+ kill -9 <PID>
216
+ ```
217
+
218
+ #### 2. 依赖安装失败
219
+
220
+ ```bash
221
+ # 清理缓存
222
+ npm cache clean --force
223
+
224
+ # 删除 node_modules 重新安装
225
+ rm -rf node_modules package-lock.json
226
+ npm install
227
+ ```
228
+
229
+ #### 3. 数据库连接问题
230
+
231
+ 项目默认使用内存数据库模式,无需额外配置。如果遇到数据库问题:
232
+
233
+ ```bash
234
+ # 检查环境变量
235
+ cat .env
236
+
237
+ # 确保使用内存数据库模式
238
+ echo "DB_TYPE=memory" >> .env
239
+ ```
240
+
241
+ ### 获取帮助
242
+
243
+ 如果遇到问题:
244
+ 1. 查看 [故障排除指南](TROUBLESHOOTING.md)
245
+ 2. 检查 [错误处理指南](ENTERPRISE_ERROR_HANDLING_GUIDE.md)
246
+ 3. 提交 [GitHub Issue](https://github.com/your-repo/issues)
247
+
248
+ ## 📚 下一步
249
+
250
+ 现在您已经成功启动了项目,建议您:
251
+
252
+ 1. **阅读开发指南**: [开发指南](DEVELOPMENT_GUIDE.md)
253
+ 2. **了解注解系统**: [注解系统指南](ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md)
254
+ 3. **学习服务层设计**: [服务层指南](SERVICE_PATTERN_GUIDE.md)
255
+ 4. **查看 API 文档**: [API 文档](API_DOCUMENTATION.md)
256
+
257
+ ## 🎉 恭喜!
258
+
259
+ 您已经成功完成了 DP-Koa Framework 的快速开始!现在可以开始您的开发之旅了。
260
+
261
+ ---
262
+
263
+ **相关链接**:
264
+ - [安装配置指南](INSTALLATION_GUIDE.md) - 详细安装步骤
265
+ - [开发指南](DEVELOPMENT_GUIDE.md) - 开发规范和最佳实践
266
+ - [故障排除指南](TROUBLESHOOTING.md) - 常见问题解决
267
+
268
+