@hlw-midway/core 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 (77) hide show
  1. package/README.md +4 -0
  2. package/dist/bin/check.d.ts +4 -0
  3. package/dist/bin/check.js +41 -0
  4. package/dist/bin/entity.d.ts +8 -0
  5. package/dist/bin/entity.js +50 -0
  6. package/dist/bin/index.d.ts +2 -0
  7. package/dist/bin/index.js +49 -0
  8. package/dist/bin/obfuscate.d.ts +1 -0
  9. package/dist/bin/obfuscate.js +69 -0
  10. package/dist/cache/store.d.ts +18 -0
  11. package/dist/cache/store.js +38 -0
  12. package/dist/config/config.default.d.ts +8 -0
  13. package/dist/config/config.default.js +22 -0
  14. package/dist/configuration.d.ts +17 -0
  15. package/dist/configuration.js +106 -0
  16. package/dist/constant/global.d.ts +59 -0
  17. package/dist/constant/global.js +85 -0
  18. package/dist/controller/base.d.ts +107 -0
  19. package/dist/controller/base.js +236 -0
  20. package/dist/decorator/cache.d.ts +2 -0
  21. package/dist/decorator/cache.js +10 -0
  22. package/dist/decorator/controller.d.ts +71 -0
  23. package/dist/decorator/controller.js +169 -0
  24. package/dist/decorator/event.d.ts +21 -0
  25. package/dist/decorator/event.js +39 -0
  26. package/dist/decorator/index.d.ts +22 -0
  27. package/dist/decorator/index.js +120 -0
  28. package/dist/decorator/tag.d.ts +22 -0
  29. package/dist/decorator/tag.js +40 -0
  30. package/dist/decorator/transaction.d.ts +8 -0
  31. package/dist/decorator/transaction.js +10 -0
  32. package/dist/entity/base.d.ts +10 -0
  33. package/dist/entity/base.js +41 -0
  34. package/dist/entity/mongo.d.ts +10 -0
  35. package/dist/entity/mongo.js +35 -0
  36. package/dist/entity/typeorm.d.ts +3 -0
  37. package/dist/entity/typeorm.js +7 -0
  38. package/dist/event/index.d.ts +53 -0
  39. package/dist/event/index.js +188 -0
  40. package/dist/exception/base.d.ts +8 -0
  41. package/dist/exception/base.js +15 -0
  42. package/dist/exception/comm.d.ts +7 -0
  43. package/dist/exception/comm.js +15 -0
  44. package/dist/exception/core.d.ts +7 -0
  45. package/dist/exception/core.js +15 -0
  46. package/dist/exception/filter.d.ts +11 -0
  47. package/dist/exception/filter.js +42 -0
  48. package/dist/exception/validate.d.ts +7 -0
  49. package/dist/exception/validate.js +15 -0
  50. package/dist/index.d.ts +31 -0
  51. package/dist/index.js +60 -0
  52. package/dist/interface.d.ts +432 -0
  53. package/dist/interface.js +24 -0
  54. package/dist/module/config.d.ts +21 -0
  55. package/dist/module/config.js +109 -0
  56. package/dist/module/import.d.ts +64 -0
  57. package/dist/module/import.js +244 -0
  58. package/dist/module/menu.d.ts +54 -0
  59. package/dist/module/menu.js +178 -0
  60. package/dist/rest/eps.d.ts +51 -0
  61. package/dist/rest/eps.js +269 -0
  62. package/dist/service/base.d.ts +167 -0
  63. package/dist/service/base.js +281 -0
  64. package/dist/service/mysql.d.ts +143 -0
  65. package/dist/service/mysql.js +524 -0
  66. package/dist/service/postgres.d.ts +160 -0
  67. package/dist/service/postgres.js +639 -0
  68. package/dist/service/sqlite.d.ts +142 -0
  69. package/dist/service/sqlite.js +560 -0
  70. package/dist/tag/data.d.ts +25 -0
  71. package/dist/tag/data.js +85 -0
  72. package/dist/util/func.d.ts +8 -0
  73. package/dist/util/func.js +44 -0
  74. package/dist/util/location.d.ts +26 -0
  75. package/dist/util/location.js +113 -0
  76. package/index.d.ts +10 -0
  77. package/package.json +68 -0
@@ -0,0 +1,51 @@
1
+ import { MidwayWebRouterService } from '@midwayjs/core';
2
+ import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
3
+ import { HlwUrlTagData } from '../tag/data';
4
+ import { CurdOption, QueryOp } from '../decorator/controller';
5
+ /**
6
+ * 实体路径
7
+ */
8
+ export declare class HlwEps {
9
+ admin: {};
10
+ app: {};
11
+ module: {};
12
+ midwayWebRouterService: MidwayWebRouterService;
13
+ typeORMDataSourceManager: TypeORMDataSourceManager;
14
+ epsConfig: boolean;
15
+ moduleConfig: any;
16
+ hlwUrlTagData: HlwUrlTagData;
17
+ init(): Promise<void>;
18
+ /**
19
+ * 获取分页查询配置
20
+ * @param curdOption
21
+ * @returns
22
+ */
23
+ getPageOp(curdOption: CurdOption): Promise<QueryOp>;
24
+ /**
25
+ * 处理列
26
+ * @param entitys
27
+ * @param entityColumns
28
+ * @param curdOption
29
+ */
30
+ pageColumns(entitys: Record<string, any[]>, curdOption: CurdOption): Promise<any[]>;
31
+ /**
32
+ * 模块信息
33
+ * @param module
34
+ */
35
+ modules(module?: string): Promise<any>;
36
+ /**
37
+ * 所有controller
38
+ * @returns
39
+ */
40
+ controller(): Promise<any[]>;
41
+ /**
42
+ * 所有路由
43
+ * @returns
44
+ */
45
+ router(): Promise<any>;
46
+ /**
47
+ * 所有实体
48
+ * @returns
49
+ */
50
+ entity(): Promise<{}>;
51
+ }
@@ -0,0 +1,269 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.HlwEps = void 0;
13
+ const core_1 = require("@midwayjs/core");
14
+ const _ = require("lodash");
15
+ const core_2 = require("@midwayjs/core");
16
+ const typeorm_1 = require("@midwayjs/typeorm");
17
+ const data_1 = require("../tag/data");
18
+ const tag_1 = require("../decorator/tag");
19
+ /**
20
+ * 实体路径
21
+ */
22
+ let HlwEps = class HlwEps {
23
+ constructor() {
24
+ this.admin = {};
25
+ this.app = {};
26
+ this.module = {};
27
+ }
28
+ // @Init()
29
+ async init() {
30
+ var _a, _b, _c, _d;
31
+ if (!this.epsConfig)
32
+ return;
33
+ const entitys = await this.entity();
34
+ const controllers = await this.controller();
35
+ const routers = await this.router();
36
+ await this.modules();
37
+ const adminArr = [];
38
+ const appArr = [];
39
+ for (const controller of controllers) {
40
+ const { prefix, module, curdOption, routerOptions } = controller;
41
+ const pageQueryOp = await this.getPageOp(curdOption);
42
+ const name = (_a = curdOption === null || curdOption === void 0 ? void 0 : curdOption.entity) === null || _a === void 0 ? void 0 : _a.name;
43
+ (_.startsWith(prefix, '/admin/') ? adminArr : appArr).push({
44
+ module,
45
+ info: {
46
+ type: {
47
+ name: prefix.split('/').pop(),
48
+ description: (routerOptions === null || routerOptions === void 0 ? void 0 : routerOptions.description) || '',
49
+ },
50
+ },
51
+ api: routers[prefix],
52
+ name,
53
+ columns: entitys[name] || [],
54
+ pageQueryOp: {
55
+ keyWordLikeFields: ((_b = pageQueryOp === null || pageQueryOp === void 0 ? void 0 : pageQueryOp.keyWordLikeFields) === null || _b === void 0 ? void 0 : _b.map(field => field.includes('.') ? field : `a.${field}`)) || [],
56
+ fieldEq: ((_c = pageQueryOp === null || pageQueryOp === void 0 ? void 0 : pageQueryOp.fieldEq) === null || _c === void 0 ? void 0 : _c.map(field => typeof field === 'string'
57
+ ? field.includes('.')
58
+ ? field
59
+ : `a.${field}`
60
+ : field)) || [],
61
+ fieldLike: ((_d = pageQueryOp === null || pageQueryOp === void 0 ? void 0 : pageQueryOp.fieldLike) === null || _d === void 0 ? void 0 : _d.map(field => typeof field === 'string'
62
+ ? field.includes('.')
63
+ ? field
64
+ : `a.${field}`
65
+ : field)) || [],
66
+ },
67
+ pageColumns: await this.pageColumns(entitys, curdOption),
68
+ prefix,
69
+ });
70
+ }
71
+ this.admin = _.groupBy(adminArr, 'module');
72
+ this.app = _.groupBy(appArr, 'module');
73
+ }
74
+ /**
75
+ * 获取分页查询配置
76
+ * @param curdOption
77
+ * @returns
78
+ */
79
+ async getPageOp(curdOption) {
80
+ let pageQueryOp = curdOption === null || curdOption === void 0 ? void 0 : curdOption.pageQueryOp;
81
+ if (typeof pageQueryOp === 'function') {
82
+ pageQueryOp = await pageQueryOp();
83
+ }
84
+ return pageQueryOp;
85
+ }
86
+ /**
87
+ * 处理列
88
+ * @param entitys
89
+ * @param entityColumns
90
+ * @param curdOption
91
+ */
92
+ async pageColumns(entitys, curdOption) {
93
+ var _a;
94
+ const pageQueryOp = await this.getPageOp(curdOption);
95
+ // 检查 pageQueryOp 是否为对象且具有 select 属性
96
+ if (pageQueryOp &&
97
+ typeof pageQueryOp === 'object' &&
98
+ 'select' in pageQueryOp &&
99
+ ((_a = curdOption === null || curdOption === void 0 ? void 0 : curdOption.entity) === null || _a === void 0 ? void 0 : _a.name)) {
100
+ const select = pageQueryOp.select;
101
+ const join = pageQueryOp.join || [];
102
+ // 所有的关联entitys
103
+ const joinEntitys = [{ name: curdOption.entity.name, alias: 'a' }];
104
+ if (join.length > 0) {
105
+ joinEntitys.push(...join.map(item => {
106
+ return { name: item.entity.name, alias: item.alias };
107
+ }));
108
+ }
109
+ // 处理 select
110
+ const result = [];
111
+ for (const selectItem of select) {
112
+ // 处理 'a.*' 这种情况
113
+ if (selectItem.endsWith('.*')) {
114
+ const alias = selectItem.split('.')[0];
115
+ const entity = joinEntitys.find(e => e.alias === alias);
116
+ if (entity) {
117
+ const entityColumns = entitys[entity.name] || [];
118
+ result.push(...entityColumns.map(e => {
119
+ return {
120
+ ...e,
121
+ source: `${alias}.${e.propertyName}`,
122
+ };
123
+ }));
124
+ }
125
+ continue;
126
+ }
127
+ // 处理单个字段,如 'b.name' 或 'b.name as userName'
128
+ const asRegex = /\s+as\s+/i;
129
+ const [field, asName] = selectItem.split(asRegex).map(s => s.trim());
130
+ const [alias, fieldName] = field.split('.');
131
+ const entity = joinEntitys.find(e => e.alias === alias);
132
+ if (entity) {
133
+ const entityColumns = entitys[entity.name] || [];
134
+ const column = entityColumns.find(col => col.propertyName === fieldName);
135
+ if (column) {
136
+ result.push({
137
+ ...column,
138
+ propertyName: asName || column.propertyName,
139
+ source: `${alias}.${column.propertyName}`,
140
+ });
141
+ }
142
+ }
143
+ }
144
+ // 将 createdAt 和 updatedAt 移到末尾
145
+ const finalResult = [...result];
146
+ const timeFields = ['createdAt', 'updatedAt'];
147
+ const timeColumns = [];
148
+ // 先找出并删除所有时间字段
149
+ for (let i = finalResult.length - 1; i >= 0; i--) {
150
+ if (timeFields.includes(finalResult[i].propertyName)) {
151
+ timeColumns.unshift(finalResult.splice(i, 1)[0]);
152
+ }
153
+ }
154
+ // 将时间字段添加到末尾
155
+ finalResult.push(...timeColumns);
156
+ return finalResult;
157
+ }
158
+ return [];
159
+ }
160
+ /**
161
+ * 模块信息
162
+ * @param module
163
+ */
164
+ async modules(module) {
165
+ for (const key in this.moduleConfig) {
166
+ const config = this.moduleConfig[key];
167
+ this.module[key] = {
168
+ name: config.name,
169
+ description: config.description,
170
+ };
171
+ }
172
+ return module ? this.module[module] : this.module;
173
+ }
174
+ /**
175
+ * 所有controller
176
+ * @returns
177
+ */
178
+ async controller() {
179
+ const result = [];
180
+ const controllers = (0, core_1.listModule)(core_1.CONTROLLER_KEY);
181
+ for (const controller of controllers) {
182
+ result.push((0, core_1.getClassMetadata)(core_1.CONTROLLER_KEY, controller));
183
+ }
184
+ return result;
185
+ }
186
+ /**
187
+ * 所有路由
188
+ * @returns
189
+ */
190
+ async router() {
191
+ let ignoreUrls = this.hlwUrlTagData.byKey(tag_1.TagTypes.IGNORE_TOKEN);
192
+ if (_.isEmpty(ignoreUrls)) {
193
+ ignoreUrls = [];
194
+ }
195
+ return _.groupBy((await this.midwayWebRouterService.getFlattenRouterTable()).map(item => {
196
+ return {
197
+ method: item.requestMethod,
198
+ path: item.url,
199
+ summary: item.summary,
200
+ dts: {},
201
+ tag: '',
202
+ prefix: item.prefix,
203
+ ignoreToken: ignoreUrls.includes(item.prefix + item.url),
204
+ };
205
+ }), 'prefix');
206
+ }
207
+ /**
208
+ * 所有实体
209
+ * @returns
210
+ */
211
+ async entity() {
212
+ const result = {};
213
+ const dataSourceNames = this.typeORMDataSourceManager.getDataSourceNames();
214
+ for (const dataSourceName of dataSourceNames) {
215
+ const entityMetadatas = await this.typeORMDataSourceManager.getDataSource(dataSourceName).entityMetadatas;
216
+ for (const entityMetadata of entityMetadatas) {
217
+ const commColums = [];
218
+ let columns = entityMetadata.columns;
219
+ if (entityMetadata.tableType != 'regular')
220
+ continue;
221
+ columns = _.filter(columns.map(e => {
222
+ return {
223
+ propertyName: e.propertyName,
224
+ type: typeof e.type === 'string' ? e.type : e.type.name.toLowerCase(),
225
+ length: e.length,
226
+ comment: e.comment,
227
+ nullable: e.isNullable,
228
+ defaultValue: e.default,
229
+ dict: e['dict'],
230
+ source: `a.${e.propertyName}`,
231
+ };
232
+ }), o => {
233
+ if (['createdAt', 'updatedAt'].includes(o.propertyName)) {
234
+ commColums.push(o);
235
+ }
236
+ return (o &&
237
+ !['createdAt', 'updatedAt', 'tenantId'].includes(o.propertyName));
238
+ }).concat(commColums);
239
+ result[entityMetadata.name] = columns;
240
+ }
241
+ }
242
+ return result;
243
+ }
244
+ };
245
+ exports.HlwEps = HlwEps;
246
+ __decorate([
247
+ (0, core_2.Inject)(),
248
+ __metadata("design:type", core_2.MidwayWebRouterService)
249
+ ], HlwEps.prototype, "midwayWebRouterService", void 0);
250
+ __decorate([
251
+ (0, core_2.Inject)(),
252
+ __metadata("design:type", typeorm_1.TypeORMDataSourceManager)
253
+ ], HlwEps.prototype, "typeORMDataSourceManager", void 0);
254
+ __decorate([
255
+ (0, core_2.Config)('hlw.eps'),
256
+ __metadata("design:type", Boolean)
257
+ ], HlwEps.prototype, "epsConfig", void 0);
258
+ __decorate([
259
+ (0, core_2.Config)('module'),
260
+ __metadata("design:type", Object)
261
+ ], HlwEps.prototype, "moduleConfig", void 0);
262
+ __decorate([
263
+ (0, core_2.Inject)(),
264
+ __metadata("design:type", data_1.HlwUrlTagData)
265
+ ], HlwEps.prototype, "hlwUrlTagData", void 0);
266
+ exports.HlwEps = HlwEps = __decorate([
267
+ (0, core_1.Provide)(),
268
+ (0, core_2.Scope)(core_2.ScopeEnum.Singleton)
269
+ ], HlwEps);
@@ -0,0 +1,167 @@
1
+ import { BaseMysqlService } from './mysql';
2
+ import { BasePgService } from './postgres';
3
+ import { Application, Context } from '@midwayjs/koa';
4
+ import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
5
+ import { Repository } from 'typeorm';
6
+ import { QueryOp } from '../decorator/controller';
7
+ import { HlwEventManager } from '../event';
8
+ import { BaseSqliteService } from './sqlite';
9
+ /**
10
+ * 服务基类
11
+ */
12
+ export declare abstract class BaseService {
13
+ baseMysqlService: BaseMysqlService;
14
+ basePgService: BasePgService;
15
+ baseSqliteService: BaseSqliteService;
16
+ ormType: any;
17
+ service: BaseMysqlService | BasePgService | BaseSqliteService;
18
+ protected entity: Repository<any>;
19
+ protected sqlParams: any;
20
+ typeORMDataSourceManager: TypeORMDataSourceManager;
21
+ hlwEventManager: HlwEventManager;
22
+ baseCtx: Context;
23
+ baseApp: Application;
24
+ init(): Promise<void>;
25
+ /**
26
+ * 获取用户ID
27
+ * @param type 类型
28
+ * @returns
29
+ */
30
+ /**
31
+ * 获取用户ID
32
+ * @param type 类型
33
+ * @returns
34
+ */
35
+ protected getUserId(type?: 'admin' | 'app'): any;
36
+ setEntity(entity: any): void;
37
+ setCtx(ctx: Context): void;
38
+ setApp(app: Application): void;
39
+ /**
40
+ * 设置sql
41
+ * @param condition 条件是否成立
42
+ * @param sql sql语句
43
+ * @param params 参数
44
+ */
45
+ setSql(condition: any, sql: any, params: any): any;
46
+ /**
47
+ * 获得查询个数的SQL
48
+ * @param sql
49
+ */
50
+ getCountSql(sql: any): string;
51
+ /**
52
+ * 参数安全性检查
53
+ * @param params
54
+ */
55
+ paramSafetyCheck(params: any): Promise<boolean>;
56
+ /**
57
+ * 原生查询
58
+ * @param sql
59
+ * @param params
60
+ * @param connectionName
61
+ */
62
+ nativeQuery(sql: any, params?: any, connectionName?: any): Promise<any>;
63
+ /**
64
+ * 获得ORM管理
65
+ * @param connectionName 连接名称
66
+ */
67
+ getOrmManager(connectionName?: string): import("typeorm").DataSource;
68
+ /**
69
+ * 操作entity获得分页数据,不用写sql
70
+ * @param find QueryBuilder
71
+ * @param query
72
+ * @param autoSort
73
+ * @param connectionName
74
+ */
75
+ entityRenderPage(find: any, query: any, autoSort?: boolean): Promise<{
76
+ list: any[];
77
+ pagination: {
78
+ page: number;
79
+ size: number;
80
+ total: number;
81
+ };
82
+ }>;
83
+ /**
84
+ * 执行SQL并获得分页数据
85
+ * @param sql 执行的sql语句
86
+ * @param query 分页查询条件
87
+ * @param autoSort 是否自动排序
88
+ * @param connectionName 连接名称
89
+ */
90
+ sqlRenderPage(sql: any, query?: {}, autoSort?: boolean, connectionName?: any): Promise<{
91
+ list: any;
92
+ pagination: {
93
+ page: number;
94
+ size: number;
95
+ total: number;
96
+ };
97
+ }>;
98
+ /**
99
+ * 获得单个ID
100
+ * @param id ID
101
+ * @param infoIgnoreProperty 忽略返回属性
102
+ */
103
+ info(id: any, infoIgnoreProperty?: string[]): Promise<any>;
104
+ /**
105
+ * 删除
106
+ * @param ids 删除的ID集合 如:[1,2,3] 或者 1,2,3
107
+ */
108
+ delete(ids: any): Promise<void>;
109
+ /**
110
+ * 软删除
111
+ * @param ids 删除的ID数组
112
+ * @param entity 实体
113
+ */
114
+ softDelete(ids: number[], entity?: Repository<any>): Promise<void>;
115
+ /**
116
+ * 修改
117
+ * @param param 数据
118
+ */
119
+ update(param: any): Promise<void>;
120
+ /**
121
+ * 新增
122
+ * @param param 数据
123
+ */
124
+ add(param: any | any[]): Promise<Object>;
125
+ /**
126
+ * 新增|修改
127
+ * @param param 数据
128
+ */
129
+ addOrUpdate(param: any | any[], type?: 'add' | 'update'): Promise<void>;
130
+ /**
131
+ * 非分页查询
132
+ * @param query 查询条件
133
+ * @param option 查询配置
134
+ * @param connectionName 连接名
135
+ */
136
+ list(query: any, option: any, connectionName?: any): Promise<any>;
137
+ /**
138
+ * 分页查询
139
+ * @param query 查询条件
140
+ * @param option 查询配置
141
+ * @param connectionName 连接名
142
+ */
143
+ page(query: any, option: any, connectionName?: any): Promise<{
144
+ list: any;
145
+ pagination: {
146
+ page: number;
147
+ size: number;
148
+ total: number;
149
+ };
150
+ }>;
151
+ /**
152
+ * 构建查询配置
153
+ * @param query 前端查询
154
+ * @param option
155
+ */
156
+ getOptionFind(query: any, option: QueryOp): Promise<string>;
157
+ /**
158
+ * 新增|修改|删除 之后的操作
159
+ * @param data 对应数据
160
+ */
161
+ modifyAfter(data: any, type: 'delete' | 'update' | 'add'): Promise<void>;
162
+ /**
163
+ * 新增|修改|删除 之前的操作
164
+ * @param data 对应数据
165
+ */
166
+ modifyBefore(data: any, type: 'delete' | 'update' | 'add'): Promise<void>;
167
+ }