@fastcar/core 0.2.38

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 (239) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +379 -0
  3. package/annotation.d.ts +157 -0
  4. package/db.d.ts +272 -0
  5. package/index.d.ts +262 -0
  6. package/package.json +53 -0
  7. package/src/FastCarApplication.ts +761 -0
  8. package/src/annotation/Application.ts +46 -0
  9. package/src/annotation/ExceptionMonitor.ts +15 -0
  10. package/src/annotation/bind/AddRequireModule.ts +18 -0
  11. package/src/annotation/bind/AliasInjection.ts +24 -0
  12. package/src/annotation/bind/Autowired.ts +11 -0
  13. package/src/annotation/bind/CallDependency.ts +24 -0
  14. package/src/annotation/data/DBType.ts +9 -0
  15. package/src/annotation/data/DS.ts +58 -0
  16. package/src/annotation/data/DSIndex.ts +7 -0
  17. package/src/annotation/data/Entity.ts +8 -0
  18. package/src/annotation/data/Field.ts +15 -0
  19. package/src/annotation/data/PrimaryKey.ts +7 -0
  20. package/src/annotation/data/SqlSession.ts +7 -0
  21. package/src/annotation/data/Table.ts +37 -0
  22. package/src/annotation/data/Transactional.ts +60 -0
  23. package/src/annotation/env/ApplicationSetting.ts +24 -0
  24. package/src/annotation/env/BaseFilePath.ts +12 -0
  25. package/src/annotation/env/BasePath.ts +12 -0
  26. package/src/annotation/env/ENV.ts +8 -0
  27. package/src/annotation/env/ResourcePath.ts +7 -0
  28. package/src/annotation/lifeCycle/AddLifeCycleItem.ts +25 -0
  29. package/src/annotation/lifeCycle/ApplicationDestory.ts +13 -0
  30. package/src/annotation/lifeCycle/ApplicationInit.ts +13 -0
  31. package/src/annotation/lifeCycle/ApplicationRunner.ts +5 -0
  32. package/src/annotation/lifeCycle/ApplicationStart.ts +23 -0
  33. package/src/annotation/lifeCycle/ApplicationStop.ts +18 -0
  34. package/src/annotation/property/Deprecate.ts +17 -0
  35. package/src/annotation/property/NotImplemented.ts +5 -0
  36. package/src/annotation/property/Override.ts +4 -0
  37. package/src/annotation/property/Readonly.ts +12 -0
  38. package/src/annotation/scan/ComponentInjection.ts +22 -0
  39. package/src/annotation/scan/ComponentScan.ts +7 -0
  40. package/src/annotation/scan/ComponentScanExclusion.ts +26 -0
  41. package/src/annotation/scan/Hotter.ts +6 -0
  42. package/src/annotation/stereotype/BeanName.ts +9 -0
  43. package/src/annotation/stereotype/Component.ts +6 -0
  44. package/src/annotation/stereotype/Configure.ts +12 -0
  45. package/src/annotation/stereotype/Controller.ts +7 -0
  46. package/src/annotation/stereotype/Injection.ts +10 -0
  47. package/src/annotation/stereotype/Log.ts +17 -0
  48. package/src/annotation/stereotype/Repository.ts +7 -0
  49. package/src/annotation/stereotype/Service.ts +7 -0
  50. package/src/annotation/valid/AddChildValid.ts +61 -0
  51. package/src/annotation/valid/DefaultVal.ts +8 -0
  52. package/src/annotation/valid/NotNull.ts +7 -0
  53. package/src/annotation/valid/Rule.ts +104 -0
  54. package/src/annotation/valid/Size.ts +13 -0
  55. package/src/annotation/valid/Type.ts +8 -0
  56. package/src/annotation/valid/ValidCustom.ts +21 -0
  57. package/src/annotation/valid/ValidForm.ts +146 -0
  58. package/src/annotation.ts +103 -0
  59. package/src/config/ApplicationConfig.ts +5 -0
  60. package/src/config/SysConfig.ts +28 -0
  61. package/src/constant/AppStatusEnum.ts +5 -0
  62. package/src/constant/BootPriority.ts +7 -0
  63. package/src/constant/CommonConstant.ts +14 -0
  64. package/src/constant/ComponentKind.ts +7 -0
  65. package/src/constant/DataTypes.ts +15 -0
  66. package/src/constant/FastCarMetaData.ts +25 -0
  67. package/src/constant/LifeCycleModule.ts +5 -0
  68. package/src/db.ts +182 -0
  69. package/src/index.ts +11 -0
  70. package/src/interface/ApplicationHook.ts +9 -0
  71. package/src/interface/ApplicationRunnerService.ts +3 -0
  72. package/src/interface/DataSourceManager.ts +14 -0
  73. package/src/interface/Logger.ts +9 -0
  74. package/src/model/BaseMapper.ts +115 -0
  75. package/src/model/DataMap.ts +103 -0
  76. package/src/model/FormRuleModel.ts +23 -0
  77. package/src/model/ValidError.ts +1 -0
  78. package/src/model/WinstonLogger.ts +119 -0
  79. package/src/type/ComponentDesc.ts +5 -0
  80. package/src/type/DesignMeta.ts +11 -0
  81. package/src/type/FileHotterDesc.ts +4 -0
  82. package/src/type/MapperType.ts +8 -0
  83. package/src/type/ProcessType.ts +12 -0
  84. package/src/type/SqlError.ts +3 -0
  85. package/src/type/WinstonLoggerType.ts +18 -0
  86. package/src/utils/ClassLoader.ts +72 -0
  87. package/src/utils/ClassUtils.ts +38 -0
  88. package/src/utils/CryptoUtil.ts +106 -0
  89. package/src/utils/DataFormat.ts +97 -0
  90. package/src/utils/DateUtil.ts +85 -0
  91. package/src/utils/FileUtil.ts +172 -0
  92. package/src/utils/FormatStr.ts +13 -0
  93. package/src/utils/Mix.ts +69 -0
  94. package/src/utils/ReflectUtil.ts +22 -0
  95. package/src/utils/TypeUtil.ts +56 -0
  96. package/src/utils/ValidationUtil.ts +138 -0
  97. package/src/utils.ts +13 -0
  98. package/target/FastCarApplication.js +661 -0
  99. package/target/annotation/AddRequireModule.js +21 -0
  100. package/target/annotation/Application.js +45 -0
  101. package/target/annotation/Autowired.js +15 -0
  102. package/target/annotation/Calldependency.js +18 -0
  103. package/target/annotation/ExceptionMonitor.js +16 -0
  104. package/target/annotation/bind/AddRequireModule.js +21 -0
  105. package/target/annotation/bind/AliasInjection.js +23 -0
  106. package/target/annotation/bind/Autowired.js +13 -0
  107. package/target/annotation/bind/CallDependency.js +23 -0
  108. package/target/annotation/data/DBType.js +11 -0
  109. package/target/annotation/data/DS.js +54 -0
  110. package/target/annotation/data/DSIndex.js +9 -0
  111. package/target/annotation/data/Entity.js +10 -0
  112. package/target/annotation/data/Field.js +18 -0
  113. package/target/annotation/data/PrimaryKey.js +9 -0
  114. package/target/annotation/data/SqlSession.js +9 -0
  115. package/target/annotation/data/Table.js +34 -0
  116. package/target/annotation/data/Transactional.js +52 -0
  117. package/target/annotation/env/ApplicationSetting.js +25 -0
  118. package/target/annotation/env/BaseFilePath.js +14 -0
  119. package/target/annotation/env/BasePath.js +14 -0
  120. package/target/annotation/env/ENV.js +10 -0
  121. package/target/annotation/env/ResourcePath.js +9 -0
  122. package/target/annotation/lifeCycle/AddLifeCycleItem.js +18 -0
  123. package/target/annotation/lifeCycle/ApplicationDestory.js +15 -0
  124. package/target/annotation/lifeCycle/ApplicationInit.js +15 -0
  125. package/target/annotation/lifeCycle/ApplicationRunner.js +7 -0
  126. package/target/annotation/lifeCycle/ApplicationStart.js +24 -0
  127. package/target/annotation/lifeCycle/ApplicationStop.js +20 -0
  128. package/target/annotation/property/Deprecate.js +19 -0
  129. package/target/annotation/property/NotImplemented.js +8 -0
  130. package/target/annotation/property/Override.js +7 -0
  131. package/target/annotation/property/Readonly.js +16 -0
  132. package/target/annotation/scan/ComponentInjection.js +21 -0
  133. package/target/annotation/scan/ComponentScan.js +9 -0
  134. package/target/annotation/scan/ComponentScanExclusion.js +25 -0
  135. package/target/annotation/scan/Hotter.js +8 -0
  136. package/target/annotation/stereotype/BeanName.js +11 -0
  137. package/target/annotation/stereotype/Component.js +8 -0
  138. package/target/annotation/stereotype/Configure.js +14 -0
  139. package/target/annotation/stereotype/Controller.js +9 -0
  140. package/target/annotation/stereotype/Injection.js +12 -0
  141. package/target/annotation/stereotype/Log.js +16 -0
  142. package/target/annotation/stereotype/Repository.js +9 -0
  143. package/target/annotation/stereotype/Service.js +9 -0
  144. package/target/annotation/valid/AddChildValid.js +56 -0
  145. package/target/annotation/valid/DefaultVal.js +10 -0
  146. package/target/annotation/valid/NotNull.js +8 -0
  147. package/target/annotation/valid/Rule.js +100 -0
  148. package/target/annotation/valid/Size.js +10 -0
  149. package/target/annotation/valid/Type.js +10 -0
  150. package/target/annotation/valid/ValidCustom.js +17 -0
  151. package/target/annotation/valid/ValidForm.js +131 -0
  152. package/target/annotation.js +101 -0
  153. package/target/config/ApplicationConfig.js +2 -0
  154. package/target/config/SysConfig.js +19 -0
  155. package/target/constant/AppStatusEnum.js +9 -0
  156. package/target/constant/BootPriority.js +11 -0
  157. package/target/constant/CommonConstant.js +16 -0
  158. package/target/constant/ComponentKind.js +11 -0
  159. package/target/constant/DataTypes.js +19 -0
  160. package/target/constant/FastCarMetaData.js +29 -0
  161. package/target/constant/LifeCycleModule.js +9 -0
  162. package/target/db.js +46 -0
  163. package/target/index.js +21 -0
  164. package/target/interface/ApplicationHook.js +2 -0
  165. package/target/interface/ApplicationRunnerService.js +2 -0
  166. package/target/interface/DataSourceManager.js +2 -0
  167. package/target/interface/Logger.js +5 -0
  168. package/target/model/BaseMapper.js +98 -0
  169. package/target/model/DataMap.js +87 -0
  170. package/target/model/FormRuleModel.js +2 -0
  171. package/target/model/ValidError.js +5 -0
  172. package/target/model/WinstonLogger.js +95 -0
  173. package/target/type/ComponentDesc.js +2 -0
  174. package/target/type/DesignMeta.js +15 -0
  175. package/target/type/FileHotterDesc.js +2 -0
  176. package/target/type/MapperType.js +2 -0
  177. package/target/type/ProcessType.js +2 -0
  178. package/target/type/SqlError.js +5 -0
  179. package/target/type/WinstonLoggerType.js +9 -0
  180. package/target/utils/ClassUtils.js +35 -0
  181. package/target/utils/CryptoUtil.js +84 -0
  182. package/target/utils/DataFormat.js +84 -0
  183. package/target/utils/DateUtil.js +71 -0
  184. package/target/utils/FileUtil.js +153 -0
  185. package/target/utils/FormatStr.js +14 -0
  186. package/target/utils/Mix.js +62 -0
  187. package/target/utils/ReflectUtil.js +22 -0
  188. package/target/utils/TypeUtil.js +47 -0
  189. package/target/utils/ValidationUtil.js +118 -0
  190. package/target/utils/classLoader.js +65 -0
  191. package/target/utils.js +23 -0
  192. package/test/example/logs/app.log +0 -0
  193. package/test/example/logs/logger.log +12 -0
  194. package/test/example/logs/logger1.log +12 -0
  195. package/test/example/logs/logger2.log +12 -0
  196. package/test/example/logs/logger3.log +12 -0
  197. package/test/example/logs/logger4.log +12 -0
  198. package/test/example/logs/logger5.log +6 -0
  199. package/test/example/logs/sys.log +7 -0
  200. package/test/example/logs/sys1.log +14 -0
  201. package/test/example/logs/sys10.log +12 -0
  202. package/test/example/logs/sys11.log +5 -0
  203. package/test/example/logs/sys13.log +5 -0
  204. package/test/example/logs/sys2.log +3 -0
  205. package/test/example/logs/sys4.log +15 -0
  206. package/test/example/logs/sys5.log +9 -0
  207. package/test/example/logs/sys6.log +10 -0
  208. package/test/example/logs/sys7.log +15 -0
  209. package/test/example/logs/sys8.log +11 -0
  210. package/test/example/logs/sys9.log +10 -0
  211. package/test/example/resource/application-test.yml +0 -0
  212. package/test/example/resource/application.yml +7 -0
  213. package/test/example/resource/evnconfig-test.yml +1 -0
  214. package/test/example/resource/hello.yml +1 -0
  215. package/test/example/simple/app.ts +99 -0
  216. package/test/example/simple/component/StartRunner.ts +10 -0
  217. package/test/example/simple/component/StopRunner.ts +14 -0
  218. package/test/example/simple/config/EnvConfig.ts +6 -0
  219. package/test/example/simple/config/HelloConfig.ts +8 -0
  220. package/test/example/simple/controller/AliasController.ts +6 -0
  221. package/test/example/simple/controller/HelloController.ts +39 -0
  222. package/test/example/simple/controller/NotFoundController.ts +20 -0
  223. package/test/example/simple/service/CallService.ts +11 -0
  224. package/test/example/simple/service/HelloService.ts +10 -0
  225. package/test/example/simple/service/LogService.ts +19 -0
  226. package/test/logs/logger.log +0 -0
  227. package/test/logs/sys.log +227 -0
  228. package/test/multi/app.ts +15 -0
  229. package/test/multi/service/aService.ts +16 -0
  230. package/test/multi/service/bService.ts +18 -0
  231. package/test/multi/service/cService.ts +21 -0
  232. package/test/unit/dataMap-test.ts +48 -0
  233. package/test/unit/decorators-test.ts +38 -0
  234. package/test/unit/ds-test.ts +33 -0
  235. package/test/unit/logs/sys.log +24 -0
  236. package/test/unit/reflectMetadata-test.ts +15 -0
  237. package/test/unit/valid-test.ts +65 -0
  238. package/test/unit/winston-test.ts +15 -0
  239. package/utils.d.ts +166 -0
@@ -0,0 +1,46 @@
1
+ import FastCarApplication from "../FastCarApplication";
2
+ import ClassUtils from "../utils/ClassUtils";
3
+ import TypeUtil from "../utils/TypeUtil";
4
+
5
+ //基础服务的应用
6
+ export default function Application(target: any) {
7
+ return new Proxy(target, {
8
+ construct: (target: any, args: any) => {
9
+ let app = new FastCarApplication();
10
+ let appProxy = new target(...args);
11
+ Reflect.set(appProxy, "app", app);
12
+
13
+ let keys = ClassUtils.getProtoType(target);
14
+ for (let key of keys) {
15
+ if (key != "constructor") {
16
+ let desc = ClassUtils.getProtoDesc(target, key);
17
+ if (desc) {
18
+ let beforeFun = Object.getOwnPropertyDescriptor(FastCarApplication.prototype, key)?.value;
19
+ let afterFun = desc.value;
20
+
21
+ if (Reflect.has(app, key) && TypeUtil.isFunction(afterFun) && TypeUtil.isFunction(beforeFun)) {
22
+ let mixFn = async (...args: any[]) => {
23
+ let res: any;
24
+ if (TypeUtil.isPromise(beforeFun)) {
25
+ res = await Promise.resolve(Reflect.apply(beforeFun, app, args));
26
+ } else {
27
+ res = Reflect.apply(beforeFun, app, args);
28
+ }
29
+
30
+ TypeUtil.isPromise(afterFun) ? await Promise.resolve(Reflect.apply(afterFun, appProxy, args)) : Reflect.apply(afterFun, appProxy, args);
31
+ return res;
32
+ };
33
+
34
+ Reflect.defineProperty(app, key, Object.assign(desc, { value: mixFn }));
35
+ } else {
36
+ Reflect.defineProperty(app, key, desc);
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ app.init();
43
+ return appProxy;
44
+ },
45
+ });
46
+ }
@@ -0,0 +1,15 @@
1
+ import * as process from "process";
2
+
3
+ //异常监听器
4
+ export default function ExceptionMonitor(target: any) {
5
+ process.on("uncaughtException", (err: any, origin: any) => {
6
+ console.error(`Caught exception: ${err.message}`);
7
+ console.error(`Exception origin: ${origin}`);
8
+ console.error(`stack: ${err.stack}`);
9
+ });
10
+
11
+ process.on("unhandledRejection", (reason, promise) => {
12
+ console.error("Unhandled Rejection at:", promise);
13
+ console.error("reason:", reason);
14
+ });
15
+ }
@@ -0,0 +1,18 @@
1
+ import "reflect-metadata";
2
+ import { FastCarMetaData } from "../../constant/FastCarMetaData";
3
+
4
+ /***
5
+ * @version 1.0 依赖模块注入
6
+ *
7
+ */
8
+ export default function AddRequireModule(target: any, m: string, alias: string) {
9
+ let relyname = FastCarMetaData.IocModule;
10
+ if (Reflect.hasMetadata(relyname, target)) {
11
+ let iocMap: Map<string, string> = Reflect.getMetadata(relyname, target);
12
+ iocMap.set(m, alias);
13
+ } else {
14
+ let modules: Map<string, string> = new Map();
15
+ modules.set(m, alias);
16
+ Reflect.defineMetadata(relyname, modules, target);
17
+ }
18
+ }
@@ -0,0 +1,24 @@
1
+ import { CommonConstant } from "../../constant/CommonConstant";
2
+ import FastCarApplication from "../../FastCarApplication";
3
+
4
+ /***
5
+ * @version 1.0 根据别名注入依赖
6
+ *
7
+ */
8
+ export default function AliasInjection(alias: string) {
9
+ return function (target: any, propertyKey: string) {
10
+ Reflect.defineProperty(target, propertyKey, {
11
+ get: () => {
12
+ let app: FastCarApplication = Reflect.get(global, CommonConstant.FastcarApp);
13
+
14
+ if (!app.hasComponentByName(alias)) {
15
+ //找不到依赖组件异常
16
+ let injectionError = new Error(`Unsatisfied dependency expressed through [${propertyKey}] `);
17
+ throw injectionError;
18
+ }
19
+
20
+ return app.getComponentByName(alias);
21
+ },
22
+ });
23
+ };
24
+ }
@@ -0,0 +1,11 @@
1
+ import CallDependency from "./CallDependency";
2
+
3
+ /***
4
+ * @version 1.0 说明哪些模块需要被加载
5
+ * @version 1.1 更改为和call类型一致
6
+ *
7
+ */
8
+ export default function Autowired(target: any, propertyKey: string) {
9
+ // //反向找设计类型
10
+ CallDependency(target, propertyKey);
11
+ }
@@ -0,0 +1,24 @@
1
+ import { CommonConstant } from "../../constant/CommonConstant";
2
+ import FastCarApplication from "../../FastCarApplication";
3
+ import ReflectUtil from "../../utils/ReflectUtil";
4
+
5
+ /***
6
+ * @version 1.0 在使用该函数时进行调用 声明的类可以不是组件
7
+ *
8
+ */
9
+ export default function CallDependency(target: any, propertyKey: string) {
10
+ Reflect.defineProperty(target, propertyKey, {
11
+ get: () => {
12
+ let key = ReflectUtil.getNameByPropertyKey(target, propertyKey);
13
+ let app: FastCarApplication = Reflect.get(global, CommonConstant.FastcarApp);
14
+
15
+ if (!app.hasComponentByName(key)) {
16
+ //找不到依赖组件异常
17
+ let injectionError = new Error(`Unsatisfied dependency expressed through [${propertyKey}] `);
18
+ throw injectionError;
19
+ }
20
+
21
+ return app.getComponentByName(key);
22
+ },
23
+ });
24
+ }
@@ -0,0 +1,9 @@
1
+ import "reflect-metadata";
2
+ import { DesignMeta } from "../../type/DesignMeta";
3
+
4
+ //字段名称 如果没有则为统一
5
+ export default function DBType(name: string) {
6
+ return function(target: any, propertyKey: string) {
7
+ Reflect.defineMetadata(DesignMeta.dbType, name, target, propertyKey);
8
+ };
9
+ }
@@ -0,0 +1,58 @@
1
+ import "reflect-metadata";
2
+ import { FastCarMetaData } from "../../constant/FastCarMetaData";
3
+ import ClassUtils from "../../utils/ClassUtils";
4
+ import TypeUtil from "../../utils/TypeUtil";
5
+ import ValidationUtil from "../../utils/ValidationUtil";
6
+
7
+ //动态数据源获取 根据就近原则 传入参数-函数-类名
8
+ export default function DS(name: string) {
9
+ return function(target: any, methodName?: string, descriptor?: PropertyDescriptor) {
10
+ if (methodName && descriptor) {
11
+ const orignFunction = descriptor.value;
12
+
13
+ //定义数据源
14
+ Reflect.defineMetadata(FastCarMetaData.DS, name, target, methodName);
15
+
16
+ //取出ds标记的位置 在编译前规避这个问题
17
+ const dsIndex = Reflect.getMetadata(FastCarMetaData.DSIndex, target, methodName);
18
+ if (!ValidationUtil.isNumber(dsIndex)) {
19
+ throw new Error(`${methodName} function dynamic data source not found`);
20
+ }
21
+
22
+ descriptor.value = function(...args: any[]) {
23
+ let dsName = args[dsIndex];
24
+ if (!dsName) {
25
+ args[dsIndex] = name;
26
+ }
27
+
28
+ return Promise.resolve(Reflect.apply(orignFunction, this, args));
29
+ };
30
+ } else {
31
+ Reflect.defineMetadata(FastCarMetaData.DS, name, target.prototype);
32
+
33
+ //找所有的方法 将符合要求的进行注入定义
34
+ let targetProto = target.prototype;
35
+ let keys = ClassUtils.getProtoType(target);
36
+
37
+ for (let key of keys) {
38
+ let dsIndex = Reflect.getMetadata(FastCarMetaData.DSIndex, targetProto, key);
39
+ if (ValidationUtil.isNumber(dsIndex)) {
40
+ let originValue = Reflect.get(targetProto, key);
41
+ if (TypeUtil.isFunction(originValue)) {
42
+ Reflect.defineProperty(targetProto, key, {
43
+ value: function(...args: any[]) {
44
+ let dsName = args[dsIndex];
45
+ if (!dsName) {
46
+ let fnDSName = Reflect.getMetadata(FastCarMetaData.DS, targetProto, key);
47
+ args[dsIndex] = fnDSName || name;
48
+ }
49
+
50
+ return Promise.resolve(Reflect.apply(originValue, this, args));
51
+ },
52
+ });
53
+ }
54
+ }
55
+ }
56
+ }
57
+ };
58
+ }
@@ -0,0 +1,7 @@
1
+ import "reflect-metadata";
2
+ import { FastCarMetaData } from "../../constant/FastCarMetaData";
3
+
4
+ //用于标记数据源位置
5
+ export default function DSIndex(target: any, name: string, index: number) {
6
+ Reflect.defineMetadata(FastCarMetaData.DSIndex, index, target, name);
7
+ }
@@ -0,0 +1,8 @@
1
+ import { DesignMeta } from "../../type/DesignMeta";
2
+
3
+ //这是一个模板类 代表具体的映射关系
4
+ export default function Entity(className: Function) {
5
+ return function(target: any) {
6
+ Reflect.defineMetadata(DesignMeta.entity, className, target.prototype);
7
+ };
8
+ }
@@ -0,0 +1,15 @@
1
+ import "reflect-metadata";
2
+ import { DesignMeta } from "../../type/DesignMeta";
3
+
4
+ //数据库列名称
5
+ export default function Field(name: string) {
6
+ return function(target: any, propertyKey: string) {
7
+ let fieldMap: Set<string> = Reflect.getMetadata(DesignMeta.fieldMap, target);
8
+ if (!fieldMap) {
9
+ Reflect.defineMetadata(DesignMeta.fieldMap, new Set([propertyKey]), target);
10
+ } else {
11
+ fieldMap.add(propertyKey);
12
+ }
13
+ Reflect.defineMetadata(DesignMeta.field, name, target, propertyKey);
14
+ };
15
+ }
@@ -0,0 +1,7 @@
1
+ import "reflect-metadata";
2
+ import { DesignMeta } from "../../type/DesignMeta";
3
+
4
+ //是否为主键
5
+ export default function PrimaryKey(target: any, propertyKey: string) {
6
+ Reflect.defineMetadata(DesignMeta.primaryKey, true, target, propertyKey);
7
+ }
@@ -0,0 +1,7 @@
1
+ import "reflect-metadata";
2
+ import { DesignMeta } from "../../type/DesignMeta";
3
+
4
+ //事务管理会话
5
+ export default function SqlSession(target: any, name: string, index: number) {
6
+ Reflect.defineMetadata(DesignMeta.sqlSession, index, target, name);
7
+ }
@@ -0,0 +1,37 @@
1
+ import "reflect-metadata";
2
+ import { FastCarMetaData } from "../../constant/FastCarMetaData";
3
+ import { DesignMeta } from "../../type/DesignMeta";
4
+ import { MapperType } from "../../type/MapperType";
5
+
6
+ //表名称 不缺省
7
+ export default function Table(name: string) {
8
+ return function(target: any) {
9
+ const proto = target.prototype;
10
+ let fields: Set<string> = Reflect.getOwnMetadata(DesignMeta.fieldMap, proto);
11
+ let mappingMap = new Map<string, MapperType>();
12
+ let dbFields = new Map<string, string>();
13
+
14
+ fields.forEach(c => {
15
+ let tsType = Reflect.getOwnMetadata(FastCarMetaData.designType, proto, c);
16
+ let field = Reflect.getOwnMetadata(DesignMeta.field, proto, c) || c;
17
+ let dbType = Reflect.getOwnMetadata(DesignMeta.dbType, proto, c) || "varchar";
18
+ let primaryKey = !!Reflect.getOwnMetadata(DesignMeta.primaryKey, proto, c);
19
+
20
+ let tsName: string = tsType.name;
21
+
22
+ const m: MapperType = {
23
+ name: c, //变量名称
24
+ type: tsName.toLowerCase(), //ts类型
25
+ field, //数据库列名
26
+ dbType: dbType, //数据类型
27
+ primaryKey, //是否为主键 默认为false
28
+ };
29
+ dbFields.set(field, c);
30
+ mappingMap.set(c, m);
31
+ });
32
+
33
+ Reflect.defineMetadata(DesignMeta.dbFields, dbFields, target); //作用的列名
34
+ Reflect.defineMetadata(DesignMeta.mapping, mappingMap, target); //映射关系
35
+ Reflect.defineMetadata(DesignMeta.table, name, target); //注入表名
36
+ };
37
+ }
@@ -0,0 +1,60 @@
1
+ import "reflect-metadata";
2
+ import { CommonConstant } from "../../constant/CommonConstant";
3
+ import FastCarApplication from "../../FastCarApplication";
4
+ import DataSourceManager from "../../interface/DataSourceManager";
5
+ import Logger from "../../interface/Logger";
6
+ import { DesignMeta } from "../../type/DesignMeta";
7
+ import SqlError from "../../type/SqlError";
8
+
9
+ /**
10
+ * @version 1.0 事务管理 不建议多个事务的嵌套(避免长事务) 尽量做到一个方法一个事务
11
+ * */
12
+ export default function Transactional(driver: string = "MysqlDataSourceManager") {
13
+ return function (target: any, methodName: string, descriptor: PropertyDescriptor) {
14
+ const orignFunction = descriptor.value;
15
+
16
+ //在初始化时就应该检测是否注入了sessionID
17
+ const paramsIndex = Reflect.getOwnMetadata(DesignMeta.sqlSession, target, methodName);
18
+ if (typeof paramsIndex != "number") {
19
+ throw new SqlError(`${methodName} needs to inject the SqlSession`);
20
+ }
21
+
22
+ descriptor.value = async function (...args: any[]) {
23
+ //创建会话id
24
+
25
+ let app: FastCarApplication = Reflect.get(global, CommonConstant.FastcarApp);
26
+
27
+ let sysLogger: Logger = app.getSysLogger() || console;
28
+ let dsm: DataSourceManager = app.getComponentByName(driver);
29
+
30
+ if (!dsm) {
31
+ sysLogger.error(`DataSourceManager ${driver} not found`);
32
+ return Promise.reject(new SqlError(`DataSourceManager ${driver} not found`));
33
+ }
34
+
35
+ let sessionId = args[paramsIndex];
36
+ if (sessionId) {
37
+ return Promise.resolve(Reflect.apply(orignFunction, this, args));
38
+ }
39
+ let errFlag = false;
40
+ sessionId = dsm.createSession();
41
+ args[paramsIndex] = sessionId;
42
+ let res: any = null;
43
+
44
+ return new Promise((resolve, reject) => {
45
+ Reflect.apply(orignFunction, this, args)
46
+ .then((result: any) => {
47
+ res = result;
48
+ })
49
+ .catch((e: Error) => {
50
+ sysLogger.error(e);
51
+ errFlag = true;
52
+ })
53
+ .finally(async () => {
54
+ await dsm.destorySession(sessionId, errFlag);
55
+ return !errFlag ? resolve(res) : reject(new SqlError(`${methodName} exec fail `));
56
+ });
57
+ });
58
+ };
59
+ };
60
+ }
@@ -0,0 +1,24 @@
1
+ import { CommonConstant } from "../../constant/CommonConstant";
2
+ import FastCarApplication from "../../FastCarApplication";
3
+ //应用程序设置 具有最高权限
4
+ export default function ApplicationSetting(setting: { [key: string]: any }) {
5
+ return function (target: any) {
6
+ let fastcarSetting: Map<string, any> = Reflect.getMetadata(CommonConstant.FastcarSetting, FastCarApplication.prototype);
7
+ if (!fastcarSetting) {
8
+ fastcarSetting = new Map<string, any>();
9
+ }
10
+ Object.keys(setting).forEach((key) => {
11
+ let afterConfig = setting[key];
12
+ let beforeConfig = fastcarSetting.get(key);
13
+ if (beforeConfig) {
14
+ if (typeof beforeConfig == "object") {
15
+ afterConfig = Object.assign(beforeConfig, afterConfig);
16
+ }
17
+ }
18
+
19
+ fastcarSetting.set(key, afterConfig);
20
+ });
21
+
22
+ Reflect.defineMetadata(CommonConstant.FastcarSetting, fastcarSetting, FastCarApplication.prototype);
23
+ };
24
+ }
@@ -0,0 +1,12 @@
1
+ import { CommonConstant } from "../../constant/CommonConstant";
2
+ import * as fs from "fs";
3
+
4
+ //设置运行是的主路径
5
+ export default function BaseFilePath(name: string) {
6
+ return function(target: any) {
7
+ let stats = fs.statSync(name);
8
+ if (stats.isFile()) {
9
+ Reflect.set(target.prototype, CommonConstant.BaseFileName, name);
10
+ }
11
+ };
12
+ }
@@ -0,0 +1,12 @@
1
+ import { CommonConstant } from "../../constant/CommonConstant";
2
+ import * as fs from "fs";
3
+
4
+ //设置运行是的主路径
5
+ export default function BasePath(name: string) {
6
+ return function(target: any) {
7
+ let stats = fs.statSync(name);
8
+ if (stats.isDirectory()) {
9
+ Reflect.set(target.prototype, CommonConstant.BasePath, name);
10
+ }
11
+ };
12
+ }
@@ -0,0 +1,8 @@
1
+ import { CommonConstant } from "../../constant/CommonConstant";
2
+
3
+ //设置初始化的env 注入在原始的application上面
4
+ export default function ENV(name: string) {
5
+ return function(target: any) {
6
+ Reflect.set(target.prototype, CommonConstant.ENV, name);
7
+ };
8
+ }
@@ -0,0 +1,7 @@
1
+ import { CommonConstant } from "../../constant/CommonConstant";
2
+
3
+ export default function ResourcePath(name: string) {
4
+ return function (target: any) {
5
+ Reflect.set(target.prototype, CommonConstant.ResourcePath, name);
6
+ };
7
+ }
@@ -0,0 +1,25 @@
1
+ import { LifeCycleModule } from "../../constant/LifeCycleModule";
2
+ import { BootPriority } from "../../constant/BootPriority";
3
+
4
+ export type LifeCycleType = {
5
+ order: BootPriority;
6
+ exec: string;
7
+ };
8
+
9
+ export function AddLifeCycleItem(target: any, lifeCycle: LifeCycleModule, item: LifeCycleType) {
10
+ let list: LifeCycleType[] = Reflect.getMetadata(lifeCycle, target);
11
+ if (!list) {
12
+ list = [];
13
+ }
14
+
15
+ let exist = list.some((fitem) => {
16
+ return item.exec == fitem.exec;
17
+ });
18
+
19
+ if (exist) {
20
+ return;
21
+ }
22
+
23
+ list.push(item);
24
+ Reflect.defineMetadata(lifeCycle, list, target);
25
+ }
@@ -0,0 +1,13 @@
1
+ import { BootPriority } from "../../constant/BootPriority";
2
+ import { LifeCycleModule } from "../../constant/LifeCycleModule";
3
+ import { AddLifeCycleItem } from "./AddLifeCycleItem";
4
+
5
+ //启动时机
6
+ export default function ApplicationDestory(order: number = BootPriority.Sys) {
7
+ return function (target: any, method: string, value: any) {
8
+ AddLifeCycleItem(target, LifeCycleModule.ApplicationStop, {
9
+ order,
10
+ exec: method,
11
+ });
12
+ };
13
+ }
@@ -0,0 +1,13 @@
1
+ import { BootPriority } from "../../constant/BootPriority";
2
+ import { LifeCycleModule } from "../../constant/LifeCycleModule";
3
+ import { AddLifeCycleItem } from "./AddLifeCycleItem";
4
+
5
+ //启动时机
6
+ export default function ApplicationInit(order: number = BootPriority.Sys) {
7
+ return function (target: any, method: string, value: any) {
8
+ AddLifeCycleItem(target, LifeCycleModule.ApplicationStart, {
9
+ order,
10
+ exec: method,
11
+ });
12
+ };
13
+ }
@@ -0,0 +1,5 @@
1
+ import Component from "../stereotype/Component";
2
+
3
+ export default function ApplicationRunner(target: any) {
4
+ Component(target);
5
+ }
@@ -0,0 +1,23 @@
1
+ import { BootPriority } from "../../constant/BootPriority";
2
+ import { LifeCycleModule } from "../../constant/LifeCycleModule";
3
+ import { AddLifeCycleItem } from "./AddLifeCycleItem";
4
+ import ApplicationRunner from "./ApplicationRunner";
5
+
6
+ /****
7
+ * @version 1.0 在应用启动后自动执行
8
+ * @params order 排序 序号越小排在越前面 系统级的组件 如数据库等一般为0
9
+ * @params exec 执行方法
10
+ */
11
+ export default function ApplicationStart(order: number = BootPriority.Sys, exec: string = "run") {
12
+ return function (target: any) {
13
+ if (!Reflect.has(target.prototype, exec)) {
14
+ throw new Error(`${target.name} has no implementation ${exec} method`);
15
+ }
16
+
17
+ ApplicationRunner(target);
18
+ AddLifeCycleItem(target.prototype, LifeCycleModule.ApplicationStart, {
19
+ order,
20
+ exec,
21
+ });
22
+ };
23
+ }
@@ -0,0 +1,18 @@
1
+ import { BootPriority } from "../../constant/BootPriority";
2
+ import { LifeCycleModule } from "../../constant/LifeCycleModule";
3
+ import { AddLifeCycleItem } from "./AddLifeCycleItem";
4
+ import ApplicationRunner from "./ApplicationRunner";
5
+
6
+ //在应用停止前触发
7
+ export default function ApplicationStop(order: number = BootPriority.Sys, exec: string = "run") {
8
+ return function (target: any) {
9
+ if (!Reflect.has(target.prototype, exec)) {
10
+ throw new Error(`${target.name} has no implementation ${exec} method`);
11
+ }
12
+ ApplicationRunner(target);
13
+ AddLifeCycleItem(target.prototype, LifeCycleModule.ApplicationStop, {
14
+ order,
15
+ exec,
16
+ });
17
+ };
18
+ }
@@ -0,0 +1,17 @@
1
+ const DEFAULT_MSG = "This function will be removed in future versions.";
2
+
3
+ /****
4
+ * @version 1.0 用于标记弃用
5
+ */
6
+ export default function Deprecate(msg = DEFAULT_MSG) {
7
+ return function(target: any, prop?: string, descriptor?: PropertyDescriptor) {
8
+ console.warn(prop ? prop : Reflect.get(target, "name"), msg);
9
+ if (descriptor) {
10
+ const fn = descriptor.value;
11
+ descriptor.value = function(...args: any) {
12
+ console.warn(prop, msg);
13
+ return Reflect.apply(fn, this, args);
14
+ };
15
+ }
16
+ };
17
+ }
@@ -0,0 +1,5 @@
1
+ /***
2
+ * @version 1.0 不实现说明
3
+ *
4
+ */
5
+ export default function NotImplemented(target: any, name?: string, descriptor?: PropertyDescriptor) {}
@@ -0,0 +1,4 @@
1
+ /***
2
+ * @version 1.0 仅用于标记方法是否被重载
3
+ */
4
+ export default function Override(target: any, methodName: string, descriptor: PropertyDescriptor) {}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @version 1.0 用于标记只读 可作用于属性或者方法
3
+ */
4
+ export default function Readonly(target: any, methodName: string, descriptor?: PropertyDescriptor) {
5
+ if (!descriptor) {
6
+ Reflect.defineProperty(target, methodName, {
7
+ writable: false,
8
+ });
9
+ } else {
10
+ descriptor.writable = false;
11
+ }
12
+ }
@@ -0,0 +1,22 @@
1
+ import * as path from "path";
2
+ import * as fs from "fs";
3
+ import { FastCarMetaData } from "../../constant/FastCarMetaData";
4
+
5
+ export default function ComponentInjection(target: any, ...names: string[]) {
6
+ let ScanPathList = FastCarMetaData.ComponentScan;
7
+ let list: string[] = Reflect.get(target.prototype, ScanPathList) || [];
8
+
9
+ for (let name of names) {
10
+ //可支持绝对路径
11
+ let p = path.join(require.main?.path || "", name);
12
+ if (fs.existsSync(name)) {
13
+ p = name;
14
+ }
15
+
16
+ if (!list.includes(p)) {
17
+ list.push(p);
18
+ }
19
+ }
20
+
21
+ Reflect.set(target.prototype, ScanPathList, list);
22
+ }
@@ -0,0 +1,7 @@
1
+ import ComponentInjection from "./ComponentInjection";
2
+
3
+ export default function ComponentScan(...names: string[]) {
4
+ return function(target: any) {
5
+ ComponentInjection(target, ...names);
6
+ };
7
+ }
@@ -0,0 +1,26 @@
1
+ import "reflect-metadata";
2
+ import * as path from "path";
3
+ import * as fs from "fs";
4
+ import { FastCarMetaData } from "../../constant/FastCarMetaData";
5
+
6
+ //和本包的相对路径
7
+ export default function ComponentScanExclusion(...names: string[]) {
8
+ return function (target: any) {
9
+ let ScanPathList = FastCarMetaData.ComponentScanExclusion;
10
+ let list: string[] = Reflect.get(target.prototype, ScanPathList) || [];
11
+
12
+ for (let name of names) {
13
+ //可支持绝对路径
14
+ let p = path.join(require.main?.path || "", name);
15
+ if (fs.existsSync(name)) {
16
+ p = name;
17
+ }
18
+
19
+ if (!list.includes(p)) {
20
+ list.push(p);
21
+ }
22
+ }
23
+
24
+ Reflect.set(target.prototype, ScanPathList, list);
25
+ };
26
+ }
@@ -0,0 +1,6 @@
1
+ import "reflect-metadata";
2
+ import { FastCarMetaData } from "../..";
3
+
4
+ export default function Hotter(target: any) {
5
+ Reflect.defineMetadata(FastCarMetaData.Hotter, true, target.prototype);
6
+ }