@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,281 @@
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.BaseService = void 0;
13
+ const core_1 = require("@midwayjs/core");
14
+ const core_2 = require("@midwayjs/core");
15
+ const mysql_1 = require("./mysql");
16
+ const postgres_1 = require("./postgres");
17
+ const validate_1 = require("../exception/validate");
18
+ const global_1 = require("../constant/global");
19
+ const typeorm_1 = require("@midwayjs/typeorm");
20
+ const event_1 = require("../event");
21
+ const core_3 = require("../exception/core");
22
+ const sqlite_1 = require("./sqlite");
23
+ /**
24
+ * 服务基类
25
+ */
26
+ let BaseService = class BaseService {
27
+ async init() {
28
+ const services = {
29
+ mysql: this.baseMysqlService,
30
+ mariadb: this.baseMysqlService,
31
+ postgres: this.basePgService,
32
+ sqlite: this.baseSqliteService,
33
+ };
34
+ this.service = services[this.ormType];
35
+ if (!this.service)
36
+ throw new core_3.HlwCoreException('暂不支持当前数据库类型');
37
+ this.sqlParams = this.service.sqlParams;
38
+ await this.service.init();
39
+ }
40
+ /**
41
+ * 获取用户ID
42
+ * @param type 类型
43
+ * @returns
44
+ */
45
+ /**
46
+ * 获取用户ID
47
+ * @param type 类型
48
+ * @returns
49
+ */
50
+ getUserId(type = 'admin') {
51
+ var _a, _b;
52
+ return type === 'admin'
53
+ ? (_a = this.baseCtx.admin) === null || _a === void 0 ? void 0 : _a.userId
54
+ : (_b = this.baseCtx.user) === null || _b === void 0 ? void 0 : _b.id;
55
+ }
56
+ // 设置模型
57
+ setEntity(entity) {
58
+ this.entity = entity;
59
+ this.service.setEntity(entity);
60
+ }
61
+ // 设置请求上下文
62
+ setCtx(ctx) {
63
+ this.baseCtx = ctx;
64
+ this.service.setCtx(ctx);
65
+ }
66
+ // 设置应用对象
67
+ setApp(app) {
68
+ this.baseApp = app;
69
+ this.service.setApp(app);
70
+ }
71
+ /**
72
+ * 设置sql
73
+ * @param condition 条件是否成立
74
+ * @param sql sql语句
75
+ * @param params 参数
76
+ */
77
+ setSql(condition, sql, params) {
78
+ return this.service.setSql(condition, sql, params);
79
+ }
80
+ /**
81
+ * 获得查询个数的SQL
82
+ * @param sql
83
+ */
84
+ getCountSql(sql) {
85
+ return this.service.getCountSql(sql);
86
+ }
87
+ /**
88
+ * 参数安全性检查
89
+ * @param params
90
+ */
91
+ async paramSafetyCheck(params) {
92
+ return await this.service.paramSafetyCheck(params);
93
+ }
94
+ /**
95
+ * 原生查询
96
+ * @param sql
97
+ * @param params
98
+ * @param connectionName
99
+ */
100
+ async nativeQuery(sql, params, connectionName) {
101
+ return await this.service.nativeQuery(sql, params, connectionName);
102
+ }
103
+ /**
104
+ * 获得ORM管理
105
+ * @param connectionName 连接名称
106
+ */
107
+ getOrmManager(connectionName = 'default') {
108
+ return this.service.getOrmManager(connectionName);
109
+ }
110
+ /**
111
+ * 操作entity获得分页数据,不用写sql
112
+ * @param find QueryBuilder
113
+ * @param query
114
+ * @param autoSort
115
+ * @param connectionName
116
+ */
117
+ async entityRenderPage(find, query, autoSort = true) {
118
+ return await this.service.entityRenderPage(find, query, autoSort);
119
+ }
120
+ /**
121
+ * 执行SQL并获得分页数据
122
+ * @param sql 执行的sql语句
123
+ * @param query 分页查询条件
124
+ * @param autoSort 是否自动排序
125
+ * @param connectionName 连接名称
126
+ */
127
+ async sqlRenderPage(sql, query = {}, autoSort = true, connectionName) {
128
+ return await this.service.sqlRenderPage(sql, query, autoSort, connectionName);
129
+ }
130
+ /**
131
+ * 获得单个ID
132
+ * @param id ID
133
+ * @param infoIgnoreProperty 忽略返回属性
134
+ */
135
+ async info(id, infoIgnoreProperty) {
136
+ this.service.setEntity(this.entity);
137
+ return await this.service.info(id, infoIgnoreProperty);
138
+ }
139
+ /**
140
+ * 删除
141
+ * @param ids 删除的ID集合 如:[1,2,3] 或者 1,2,3
142
+ */
143
+ async delete(ids) {
144
+ this.service.setEntity(this.entity);
145
+ await this.modifyBefore(ids, 'delete');
146
+ await this.service.delete(ids);
147
+ await this.modifyAfter(ids, 'delete');
148
+ }
149
+ /**
150
+ * 软删除
151
+ * @param ids 删除的ID数组
152
+ * @param entity 实体
153
+ */
154
+ async softDelete(ids, entity) {
155
+ this.service.setEntity(this.entity);
156
+ await this.service.softDelete(ids, entity);
157
+ }
158
+ /**
159
+ * 修改
160
+ * @param param 数据
161
+ */
162
+ async update(param) {
163
+ this.service.setEntity(this.entity);
164
+ if (!this.entity)
165
+ throw new validate_1.HlwValidateException(global_1.ERRINFO.NOENTITY);
166
+ if (!param.id && !(param instanceof Array))
167
+ throw new validate_1.HlwValidateException(global_1.ERRINFO.NOID);
168
+ await this.addOrUpdate(param, 'update');
169
+ }
170
+ /**
171
+ * 新增
172
+ * @param param 数据
173
+ */
174
+ async add(param) {
175
+ if (!this.entity)
176
+ throw new validate_1.HlwValidateException(global_1.ERRINFO.NOENTITY);
177
+ delete param.id;
178
+ await this.addOrUpdate(param, 'add');
179
+ return {
180
+ id: param instanceof Array
181
+ ? param.map(e => {
182
+ return e.id ? e.id : e._id;
183
+ })
184
+ : param.id
185
+ ? param.id
186
+ : param._id,
187
+ };
188
+ }
189
+ /**
190
+ * 新增|修改
191
+ * @param param 数据
192
+ */
193
+ async addOrUpdate(param, type = 'add') {
194
+ this.service.setEntity(this.entity);
195
+ await this.modifyBefore(param, type);
196
+ await this.service.addOrUpdate(param, type);
197
+ await this.modifyAfter(param, type);
198
+ }
199
+ /**
200
+ * 非分页查询
201
+ * @param query 查询条件
202
+ * @param option 查询配置
203
+ * @param connectionName 连接名
204
+ */
205
+ async list(query, option, connectionName) {
206
+ this.service.setEntity(this.entity);
207
+ return await this.service.list(query, option, connectionName);
208
+ }
209
+ /**
210
+ * 分页查询
211
+ * @param query 查询条件
212
+ * @param option 查询配置
213
+ * @param connectionName 连接名
214
+ */
215
+ async page(query, option, connectionName) {
216
+ this.service.setEntity(this.entity);
217
+ return await this.service.page(query, option, connectionName);
218
+ }
219
+ /**
220
+ * 构建查询配置
221
+ * @param query 前端查询
222
+ * @param option
223
+ */
224
+ async getOptionFind(query, option) {
225
+ this.service.setEntity(this.entity);
226
+ return await this.service.getOptionFind(query, option);
227
+ }
228
+ /**
229
+ * 新增|修改|删除 之后的操作
230
+ * @param data 对应数据
231
+ */
232
+ async modifyAfter(data, type) { }
233
+ /**
234
+ * 新增|修改|删除 之前的操作
235
+ * @param data 对应数据
236
+ */
237
+ async modifyBefore(data, type) { }
238
+ };
239
+ exports.BaseService = BaseService;
240
+ __decorate([
241
+ (0, core_1.Inject)(),
242
+ __metadata("design:type", mysql_1.BaseMysqlService)
243
+ ], BaseService.prototype, "baseMysqlService", void 0);
244
+ __decorate([
245
+ (0, core_1.Inject)(),
246
+ __metadata("design:type", postgres_1.BasePgService)
247
+ ], BaseService.prototype, "basePgService", void 0);
248
+ __decorate([
249
+ (0, core_1.Inject)(),
250
+ __metadata("design:type", sqlite_1.BaseSqliteService)
251
+ ], BaseService.prototype, "baseSqliteService", void 0);
252
+ __decorate([
253
+ (0, core_1.Config)('typeorm.dataSource.default.type'),
254
+ __metadata("design:type", Object)
255
+ ], BaseService.prototype, "ormType", void 0);
256
+ __decorate([
257
+ (0, core_1.Inject)(),
258
+ __metadata("design:type", typeorm_1.TypeORMDataSourceManager)
259
+ ], BaseService.prototype, "typeORMDataSourceManager", void 0);
260
+ __decorate([
261
+ (0, core_1.Inject)(),
262
+ __metadata("design:type", event_1.HlwEventManager)
263
+ ], BaseService.prototype, "hlwEventManager", void 0);
264
+ __decorate([
265
+ (0, core_1.Inject)('ctx'),
266
+ __metadata("design:type", Object)
267
+ ], BaseService.prototype, "baseCtx", void 0);
268
+ __decorate([
269
+ (0, core_1.App)(),
270
+ __metadata("design:type", Object)
271
+ ], BaseService.prototype, "baseApp", void 0);
272
+ __decorate([
273
+ (0, core_1.Init)(),
274
+ __metadata("design:type", Function),
275
+ __metadata("design:paramtypes", []),
276
+ __metadata("design:returntype", Promise)
277
+ ], BaseService.prototype, "init", null);
278
+ exports.BaseService = BaseService = __decorate([
279
+ (0, core_1.Provide)(),
280
+ (0, core_2.Scope)(core_2.ScopeEnum.Request, { allowDowngrade: true })
281
+ ], BaseService);
@@ -0,0 +1,143 @@
1
+ import { Application, Context } from '@midwayjs/koa';
2
+ import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
3
+ import { Repository, SelectQueryBuilder } from 'typeorm';
4
+ import { QueryOp } from '../decorator/controller';
5
+ import { HlwEventManager } from '../event';
6
+ /**
7
+ * 服务基类
8
+ */
9
+ export declare abstract class BaseMysqlService {
10
+ private _hlwConfig;
11
+ entity: Repository<any>;
12
+ sqlParams: any;
13
+ typeORMDataSourceManager: TypeORMDataSourceManager;
14
+ hlwEventManager: HlwEventManager;
15
+ setEntity(entity: any): void;
16
+ setCtx(ctx: Context): void;
17
+ baseApp: Application;
18
+ setApp(app: Application): void;
19
+ baseCtx: Context;
20
+ init(): void;
21
+ /**
22
+ * 设置sql
23
+ * @param condition 条件是否成立
24
+ * @param sql sql语句
25
+ * @param params 参数
26
+ */
27
+ setSql(condition: any, sql: any, params: any): any;
28
+ /**
29
+ * 获得查询个数的SQL
30
+ * @param sql
31
+ */
32
+ getCountSql(sql: any): string;
33
+ /**
34
+ * 参数安全性检查
35
+ * @param params
36
+ */
37
+ paramSafetyCheck(params: any): Promise<boolean>;
38
+ /**
39
+ * 原生查询
40
+ * @param sql
41
+ * @param params
42
+ * @param connectionName
43
+ */
44
+ nativeQuery(sql: any, params?: any, connectionName?: any): Promise<any>;
45
+ /**
46
+ * 获得ORM管理
47
+ * @param connectionName 连接名称
48
+ */
49
+ getOrmManager(connectionName?: string): import("typeorm").DataSource;
50
+ /**
51
+ * 操作entity获得分页数据,不用写sql
52
+ * @param find QueryBuilder
53
+ * @param query
54
+ * @param autoSort
55
+ * @param connectionName
56
+ */
57
+ entityRenderPage(find: SelectQueryBuilder<any>, query: any, autoSort?: boolean): Promise<{
58
+ list: any[];
59
+ pagination: {
60
+ page: number;
61
+ size: number;
62
+ total: number;
63
+ };
64
+ }>;
65
+ /**
66
+ * 执行SQL并获得分页数据
67
+ * @param sql 执行的sql语句
68
+ * @param query 分页查询条件
69
+ * @param autoSort 是否自动排序
70
+ * @param connectionName 连接名称
71
+ */
72
+ sqlRenderPage(sql: any, query: any, autoSort?: boolean, connectionName?: any): Promise<{
73
+ list: any;
74
+ pagination: {
75
+ page: number;
76
+ size: number;
77
+ total: number;
78
+ };
79
+ }>;
80
+ /**
81
+ * 检查排序
82
+ * @param sort 排序
83
+ * @returns
84
+ */
85
+ checkSort(sort: any): any;
86
+ /**
87
+ * camelCase 转 snake_case,用于前端传入排序字段映射数据库列名
88
+ */
89
+ toSnakeCase(str: string): string;
90
+ /**
91
+ * 将对象数组的 key 从 snake_case 转为 camelCase
92
+ */
93
+ keysToCamel(list: any[]): any[];
94
+ /**
95
+ * 获得单个ID
96
+ * @param id ID
97
+ * @param infoIgnoreProperty 忽略返回属性
98
+ */
99
+ info(id: any, infoIgnoreProperty?: string[]): Promise<any>;
100
+ /**
101
+ * 删除
102
+ * @param ids 删除的ID集合 如:[1,2,3] 或者 1,2,3
103
+ */
104
+ delete(ids: any): Promise<void>;
105
+ /**
106
+ * 软删除
107
+ * @param ids 删除的ID数组
108
+ * @param entity 实体
109
+ */
110
+ softDelete(ids: number[], entity?: Repository<any>): Promise<void>;
111
+ /**
112
+ * 新增|修改
113
+ * @param param 数据
114
+ */
115
+ addOrUpdate(param: any | any[], type?: 'add' | 'update'): Promise<void>;
116
+ /**
117
+ * 非分页查询
118
+ * @param query 查询条件
119
+ * @param option 查询配置
120
+ * @param connectionName 连接名
121
+ */
122
+ list(query: any, option: any, connectionName?: any): Promise<any>;
123
+ /**
124
+ * 分页查询
125
+ * @param query 查询条件
126
+ * @param option 查询配置
127
+ * @param connectionName 连接名
128
+ */
129
+ page(query: any, option: any, connectionName?: any): Promise<{
130
+ list: any;
131
+ pagination: {
132
+ page: number;
133
+ size: number;
134
+ total: number;
135
+ };
136
+ }>;
137
+ /**
138
+ * 构建查询配置
139
+ * @param query 前端查询
140
+ * @param option
141
+ */
142
+ getOptionFind(query: any, option: QueryOp): Promise<string>;
143
+ }