@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.
- package/README.md +4 -0
- package/dist/bin/check.d.ts +4 -0
- package/dist/bin/check.js +41 -0
- package/dist/bin/entity.d.ts +8 -0
- package/dist/bin/entity.js +50 -0
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.js +49 -0
- package/dist/bin/obfuscate.d.ts +1 -0
- package/dist/bin/obfuscate.js +69 -0
- package/dist/cache/store.d.ts +18 -0
- package/dist/cache/store.js +38 -0
- package/dist/config/config.default.d.ts +8 -0
- package/dist/config/config.default.js +22 -0
- package/dist/configuration.d.ts +17 -0
- package/dist/configuration.js +106 -0
- package/dist/constant/global.d.ts +59 -0
- package/dist/constant/global.js +85 -0
- package/dist/controller/base.d.ts +107 -0
- package/dist/controller/base.js +236 -0
- package/dist/decorator/cache.d.ts +2 -0
- package/dist/decorator/cache.js +10 -0
- package/dist/decorator/controller.d.ts +71 -0
- package/dist/decorator/controller.js +169 -0
- package/dist/decorator/event.d.ts +21 -0
- package/dist/decorator/event.js +39 -0
- package/dist/decorator/index.d.ts +22 -0
- package/dist/decorator/index.js +120 -0
- package/dist/decorator/tag.d.ts +22 -0
- package/dist/decorator/tag.js +40 -0
- package/dist/decorator/transaction.d.ts +8 -0
- package/dist/decorator/transaction.js +10 -0
- package/dist/entity/base.d.ts +10 -0
- package/dist/entity/base.js +41 -0
- package/dist/entity/mongo.d.ts +10 -0
- package/dist/entity/mongo.js +35 -0
- package/dist/entity/typeorm.d.ts +3 -0
- package/dist/entity/typeorm.js +7 -0
- package/dist/event/index.d.ts +53 -0
- package/dist/event/index.js +188 -0
- package/dist/exception/base.d.ts +8 -0
- package/dist/exception/base.js +15 -0
- package/dist/exception/comm.d.ts +7 -0
- package/dist/exception/comm.js +15 -0
- package/dist/exception/core.d.ts +7 -0
- package/dist/exception/core.js +15 -0
- package/dist/exception/filter.d.ts +11 -0
- package/dist/exception/filter.js +42 -0
- package/dist/exception/validate.d.ts +7 -0
- package/dist/exception/validate.js +15 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +60 -0
- package/dist/interface.d.ts +432 -0
- package/dist/interface.js +24 -0
- package/dist/module/config.d.ts +21 -0
- package/dist/module/config.js +109 -0
- package/dist/module/import.d.ts +64 -0
- package/dist/module/import.js +244 -0
- package/dist/module/menu.d.ts +54 -0
- package/dist/module/menu.js +178 -0
- package/dist/rest/eps.d.ts +51 -0
- package/dist/rest/eps.js +269 -0
- package/dist/service/base.d.ts +167 -0
- package/dist/service/base.js +281 -0
- package/dist/service/mysql.d.ts +143 -0
- package/dist/service/mysql.js +524 -0
- package/dist/service/postgres.d.ts +160 -0
- package/dist/service/postgres.js +639 -0
- package/dist/service/sqlite.d.ts +142 -0
- package/dist/service/sqlite.js +560 -0
- package/dist/tag/data.d.ts +25 -0
- package/dist/tag/data.js +85 -0
- package/dist/util/func.d.ts +8 -0
- package/dist/util/func.js +44 -0
- package/dist/util/location.d.ts +26 -0
- package/dist/util/location.js +113 -0
- package/index.d.ts +10 -0
- package/package.json +68 -0
|
@@ -0,0 +1,524 @@
|
|
|
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.BaseMysqlService = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const core_2 = require("@midwayjs/core");
|
|
15
|
+
const validate_1 = require("../exception/validate");
|
|
16
|
+
const global_1 = require("../constant/global");
|
|
17
|
+
const SqlString = require("sqlstring");
|
|
18
|
+
const typeorm_1 = require("@midwayjs/typeorm");
|
|
19
|
+
const typeorm_2 = require("typeorm");
|
|
20
|
+
const _ = require("lodash");
|
|
21
|
+
const event_1 = require("../event");
|
|
22
|
+
/**
|
|
23
|
+
* 服务基类
|
|
24
|
+
*/
|
|
25
|
+
let BaseMysqlService = class BaseMysqlService {
|
|
26
|
+
// 设置模型
|
|
27
|
+
setEntity(entity) {
|
|
28
|
+
this.entity = entity;
|
|
29
|
+
}
|
|
30
|
+
// 设置请求上下文
|
|
31
|
+
setCtx(ctx) {
|
|
32
|
+
this.baseCtx = ctx;
|
|
33
|
+
}
|
|
34
|
+
// 设置应用对象
|
|
35
|
+
setApp(app) {
|
|
36
|
+
this.baseApp = app;
|
|
37
|
+
}
|
|
38
|
+
// 初始化
|
|
39
|
+
init() {
|
|
40
|
+
this.sqlParams = [];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 设置sql
|
|
44
|
+
* @param condition 条件是否成立
|
|
45
|
+
* @param sql sql语句
|
|
46
|
+
* @param params 参数
|
|
47
|
+
*/
|
|
48
|
+
setSql(condition, sql, params) {
|
|
49
|
+
let rSql = false;
|
|
50
|
+
if (condition || condition === 0) {
|
|
51
|
+
rSql = true;
|
|
52
|
+
this.sqlParams = this.sqlParams.concat(params);
|
|
53
|
+
}
|
|
54
|
+
return rSql ? sql : '';
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 获得查询个数的SQL
|
|
58
|
+
* @param sql
|
|
59
|
+
*/
|
|
60
|
+
getCountSql(sql) {
|
|
61
|
+
sql = sql
|
|
62
|
+
.replace(new RegExp('LIMIT', 'gm'), 'limit ')
|
|
63
|
+
.replace(new RegExp('\n', 'gm'), ' ');
|
|
64
|
+
if (sql.includes('limit')) {
|
|
65
|
+
const sqlArr = sql.split('limit ');
|
|
66
|
+
sqlArr.pop();
|
|
67
|
+
sql = sqlArr.join('limit ');
|
|
68
|
+
}
|
|
69
|
+
return `select count(*) as count from (${sql}) a`;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 参数安全性检查
|
|
73
|
+
* @param params
|
|
74
|
+
*/
|
|
75
|
+
async paramSafetyCheck(params) {
|
|
76
|
+
const lp = params.toLowerCase();
|
|
77
|
+
return !(lp.indexOf('update ') > -1 ||
|
|
78
|
+
lp.indexOf('select ') > -1 ||
|
|
79
|
+
lp.indexOf('delete ') > -1 ||
|
|
80
|
+
lp.indexOf('insert ') > -1);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 原生查询
|
|
84
|
+
* @param sql
|
|
85
|
+
* @param params
|
|
86
|
+
* @param connectionName
|
|
87
|
+
*/
|
|
88
|
+
async nativeQuery(sql, params, connectionName) {
|
|
89
|
+
if (_.isEmpty(params)) {
|
|
90
|
+
params = this.sqlParams;
|
|
91
|
+
}
|
|
92
|
+
let newParams = [];
|
|
93
|
+
newParams = newParams.concat(params);
|
|
94
|
+
this.sqlParams = [];
|
|
95
|
+
for (const param of newParams) {
|
|
96
|
+
SqlString.escape(param);
|
|
97
|
+
}
|
|
98
|
+
return await this.getOrmManager(connectionName).query(sql, newParams || []);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 获得ORM管理
|
|
102
|
+
* @param connectionName 连接名称
|
|
103
|
+
*/
|
|
104
|
+
getOrmManager(connectionName = 'default') {
|
|
105
|
+
return this.typeORMDataSourceManager.getDataSource(connectionName);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 操作entity获得分页数据,不用写sql
|
|
109
|
+
* @param find QueryBuilder
|
|
110
|
+
* @param query
|
|
111
|
+
* @param autoSort
|
|
112
|
+
* @param connectionName
|
|
113
|
+
*/
|
|
114
|
+
async entityRenderPage(find, query, autoSort = true) {
|
|
115
|
+
const { size = this._hlwConfig.crud.pageSize, page = 1, order = 'id', sort = 'desc', isExport = false, maxExportLimit, } = query;
|
|
116
|
+
const count = await find.getCount();
|
|
117
|
+
let dataFind;
|
|
118
|
+
if (isExport && maxExportLimit > 0) {
|
|
119
|
+
dataFind = find.limit(maxExportLimit);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
dataFind = find.offset((page - 1) * size).limit(size);
|
|
123
|
+
}
|
|
124
|
+
if (autoSort) {
|
|
125
|
+
find.addOrderBy(order, sort.toUpperCase());
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
list: await dataFind.getRawMany(),
|
|
129
|
+
pagination: {
|
|
130
|
+
page: parseInt(page),
|
|
131
|
+
size: parseInt(size),
|
|
132
|
+
total: count,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* 执行SQL并获得分页数据
|
|
138
|
+
* @param sql 执行的sql语句
|
|
139
|
+
* @param query 分页查询条件
|
|
140
|
+
* @param autoSort 是否自动排序
|
|
141
|
+
* @param connectionName 连接名称
|
|
142
|
+
*/
|
|
143
|
+
async sqlRenderPage(sql, query, autoSort = true, connectionName) {
|
|
144
|
+
const { size = this._hlwConfig.crud.pageSize, page = 1, order = 'id', sort = 'desc', isExport = false, maxExportLimit, } = query;
|
|
145
|
+
if (order && sort && autoSort) {
|
|
146
|
+
if (!(await this.paramSafetyCheck(order + sort))) {
|
|
147
|
+
throw new validate_1.HlwValidateException('非法传参~');
|
|
148
|
+
}
|
|
149
|
+
sql += ` ORDER BY ${SqlString.escapeId(order)} ${this.checkSort(sort)}`;
|
|
150
|
+
}
|
|
151
|
+
if (isExport && maxExportLimit > 0) {
|
|
152
|
+
this.sqlParams.push(parseInt(maxExportLimit));
|
|
153
|
+
sql += ' LIMIT ? ';
|
|
154
|
+
}
|
|
155
|
+
if (!isExport) {
|
|
156
|
+
this.sqlParams.push((page - 1) * size);
|
|
157
|
+
this.sqlParams.push(parseInt(size));
|
|
158
|
+
sql += ' LIMIT ?,? ';
|
|
159
|
+
}
|
|
160
|
+
let params = [];
|
|
161
|
+
params = params.concat(this.sqlParams);
|
|
162
|
+
const result = await this.nativeQuery(sql, params, connectionName);
|
|
163
|
+
const countResult = await this.nativeQuery(this.getCountSql(sql), params, connectionName);
|
|
164
|
+
return {
|
|
165
|
+
list: result,
|
|
166
|
+
pagination: {
|
|
167
|
+
page: parseInt(page),
|
|
168
|
+
size: parseInt(size),
|
|
169
|
+
total: parseInt(countResult[0] ? countResult[0].count : 0),
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 检查排序
|
|
175
|
+
* @param sort 排序
|
|
176
|
+
* @returns
|
|
177
|
+
*/
|
|
178
|
+
checkSort(sort) {
|
|
179
|
+
if (!['desc', 'asc'].includes(sort.toLowerCase())) {
|
|
180
|
+
throw new validate_1.HlwValidateException('sort 非法传参~');
|
|
181
|
+
}
|
|
182
|
+
return sort;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* camelCase 转 snake_case,用于前端传入排序字段映射数据库列名
|
|
186
|
+
*/
|
|
187
|
+
toSnakeCase(str) {
|
|
188
|
+
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* 将对象数组的 key 从 snake_case 转为 camelCase
|
|
192
|
+
*/
|
|
193
|
+
keysToCamel(list) {
|
|
194
|
+
return list.map(row => {
|
|
195
|
+
const obj = {};
|
|
196
|
+
for (const key of Object.keys(row)) {
|
|
197
|
+
const camel = key.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
198
|
+
obj[camel] = row[key];
|
|
199
|
+
}
|
|
200
|
+
return obj;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* 获得单个ID
|
|
205
|
+
* @param id ID
|
|
206
|
+
* @param infoIgnoreProperty 忽略返回属性
|
|
207
|
+
*/
|
|
208
|
+
async info(id, infoIgnoreProperty) {
|
|
209
|
+
if (!this.entity)
|
|
210
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.NOENTITY);
|
|
211
|
+
if (!id) {
|
|
212
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.NOID);
|
|
213
|
+
}
|
|
214
|
+
const info = await this.entity.findOneBy({ id: (0, typeorm_2.Equal)(id) });
|
|
215
|
+
if (info && infoIgnoreProperty) {
|
|
216
|
+
for (const property of infoIgnoreProperty) {
|
|
217
|
+
delete info[property];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return info;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* 删除
|
|
224
|
+
* @param ids 删除的ID集合 如:[1,2,3] 或者 1,2,3
|
|
225
|
+
*/
|
|
226
|
+
async delete(ids) {
|
|
227
|
+
var _a;
|
|
228
|
+
if (!this.entity)
|
|
229
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.NOENTITY);
|
|
230
|
+
if (ids instanceof String) {
|
|
231
|
+
ids = ids.split(',');
|
|
232
|
+
}
|
|
233
|
+
// 启动软删除发送事件
|
|
234
|
+
if ((_a = this._hlwConfig.crud) === null || _a === void 0 ? void 0 : _a.softDelete) {
|
|
235
|
+
this.softDelete(ids);
|
|
236
|
+
}
|
|
237
|
+
await this.entity.delete(ids);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* 软删除
|
|
241
|
+
* @param ids 删除的ID数组
|
|
242
|
+
* @param entity 实体
|
|
243
|
+
*/
|
|
244
|
+
async softDelete(ids, entity) {
|
|
245
|
+
const data = await this.entity.find({
|
|
246
|
+
where: {
|
|
247
|
+
id: (0, typeorm_2.In)(ids),
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
if (_.isEmpty(data))
|
|
251
|
+
return;
|
|
252
|
+
const _entity = entity ? entity : this.entity;
|
|
253
|
+
const params = {
|
|
254
|
+
data,
|
|
255
|
+
ctx: this.baseCtx,
|
|
256
|
+
entity: _entity,
|
|
257
|
+
};
|
|
258
|
+
if (data.length > 0) {
|
|
259
|
+
this.hlwEventManager.emit(global_1.EVENT.SOFT_DELETE, params);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* 新增|修改
|
|
264
|
+
* @param param 数据
|
|
265
|
+
*/
|
|
266
|
+
async addOrUpdate(param, type = 'add') {
|
|
267
|
+
var _a;
|
|
268
|
+
if (!this.entity)
|
|
269
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.NOENTITY);
|
|
270
|
+
delete param.createdAt;
|
|
271
|
+
// 判断是否是批量操作
|
|
272
|
+
if (param instanceof Array) {
|
|
273
|
+
param.forEach(item => {
|
|
274
|
+
item.updatedAt = new Date();
|
|
275
|
+
if (type == 'add') {
|
|
276
|
+
item.createdAt = new Date();
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
await this.entity.save(param);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
const upsert = ((_a = this._hlwConfig.crud) === null || _a === void 0 ? void 0 : _a.upsert) || 'normal';
|
|
283
|
+
if (type == 'update') {
|
|
284
|
+
if (upsert == 'save') {
|
|
285
|
+
const info = await this.entity.findOneBy({ id: (0, typeorm_2.Equal)(param.id) });
|
|
286
|
+
if (!info) {
|
|
287
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.NOTFOUND);
|
|
288
|
+
}
|
|
289
|
+
param = {
|
|
290
|
+
...info,
|
|
291
|
+
...param,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
param.updatedAt = new Date();
|
|
295
|
+
upsert == 'normal'
|
|
296
|
+
? await this.entity.update(param.id, param)
|
|
297
|
+
: await this.entity.save(param);
|
|
298
|
+
}
|
|
299
|
+
if (type == 'add') {
|
|
300
|
+
param.createdAt = new Date();
|
|
301
|
+
param.updatedAt = new Date();
|
|
302
|
+
upsert == 'normal'
|
|
303
|
+
? await this.entity.insert(param)
|
|
304
|
+
: await this.entity.save(param);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* 非分页查询
|
|
310
|
+
* @param query 查询条件
|
|
311
|
+
* @param option 查询配置
|
|
312
|
+
* @param connectionName 连接名
|
|
313
|
+
*/
|
|
314
|
+
async list(query, option, connectionName) {
|
|
315
|
+
if (!this.entity)
|
|
316
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.NOENTITY);
|
|
317
|
+
const sql = await this.getOptionFind(query, option);
|
|
318
|
+
return this.nativeQuery(sql, [], connectionName);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* 分页查询
|
|
322
|
+
* @param query 查询条件
|
|
323
|
+
* @param option 查询配置
|
|
324
|
+
* @param connectionName 连接名
|
|
325
|
+
*/
|
|
326
|
+
async page(query, option, connectionName) {
|
|
327
|
+
if (!this.entity)
|
|
328
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.NOENTITY);
|
|
329
|
+
const sql = await this.getOptionFind(query, option);
|
|
330
|
+
return this.sqlRenderPage(sql, query, false, connectionName);
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* 构建查询配置
|
|
334
|
+
* @param query 前端查询
|
|
335
|
+
* @param option
|
|
336
|
+
*/
|
|
337
|
+
async getOptionFind(query, option) {
|
|
338
|
+
let { order = 'id', sort = 'desc', keyWord = '' } = query;
|
|
339
|
+
const sqlArr = ['SELECT'];
|
|
340
|
+
const selects = ['a.*'];
|
|
341
|
+
const find = this.entity.createQueryBuilder('a');
|
|
342
|
+
if (option) {
|
|
343
|
+
if (typeof option === 'function') {
|
|
344
|
+
// @ts-ignore
|
|
345
|
+
option = await option(this.baseCtx, this.baseApp);
|
|
346
|
+
}
|
|
347
|
+
// 判断是否有关联查询,有的话取个别名
|
|
348
|
+
if (!_.isEmpty(option.join)) {
|
|
349
|
+
for (const item of option.join) {
|
|
350
|
+
selects.push(`${item.alias}.*`);
|
|
351
|
+
find[item.type || 'leftJoin'](item.entity, item.alias, item.condition);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// 默认条件
|
|
355
|
+
if (option.where) {
|
|
356
|
+
const wheres = typeof option.where === 'function'
|
|
357
|
+
? await option.where(this.baseCtx, this.baseApp)
|
|
358
|
+
: option.where;
|
|
359
|
+
if (!_.isEmpty(wheres)) {
|
|
360
|
+
for (const item of wheres) {
|
|
361
|
+
if (item.length == 2 ||
|
|
362
|
+
(item.length == 3 && (item[2] || item[2] === 0))) {
|
|
363
|
+
for (const key in item[1]) {
|
|
364
|
+
this.sqlParams.push(item[1][key]);
|
|
365
|
+
}
|
|
366
|
+
find.andWhere(item[0], item[1]);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
// 附加排序
|
|
372
|
+
if (!_.isEmpty(option.addOrderBy)) {
|
|
373
|
+
for (const key in option.addOrderBy) {
|
|
374
|
+
if (order && order == key) {
|
|
375
|
+
sort = option.addOrderBy[key].toUpperCase();
|
|
376
|
+
}
|
|
377
|
+
find.addOrderBy(SqlString.escapeId(key), this.checkSort(option.addOrderBy[key].toUpperCase()));
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
// 关键字模糊搜索
|
|
381
|
+
if (keyWord || keyWord === 0) {
|
|
382
|
+
keyWord = `%${keyWord}%`;
|
|
383
|
+
find.andWhere(new typeorm_2.Brackets(qb => {
|
|
384
|
+
var _a;
|
|
385
|
+
const keyWordLikeFields = option.keyWordLikeFields || [];
|
|
386
|
+
for (let i = 0; i < ((_a = option.keyWordLikeFields) === null || _a === void 0 ? void 0 : _a.length) || 0; i++) {
|
|
387
|
+
qb.orWhere(`${keyWordLikeFields[i]} like :keyWord`, {
|
|
388
|
+
keyWord,
|
|
389
|
+
});
|
|
390
|
+
this.sqlParams.push(keyWord);
|
|
391
|
+
}
|
|
392
|
+
}));
|
|
393
|
+
}
|
|
394
|
+
// 筛选字段
|
|
395
|
+
if (!_.isEmpty(option.select)) {
|
|
396
|
+
sqlArr.push(option.select.join(','));
|
|
397
|
+
find.select(option.select);
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
sqlArr.push(selects.join(','));
|
|
401
|
+
}
|
|
402
|
+
// 字段全匹配
|
|
403
|
+
if (!_.isEmpty(option.fieldEq)) {
|
|
404
|
+
for (let key of option.fieldEq) {
|
|
405
|
+
const c = {};
|
|
406
|
+
// 如果key有包含.的情况下操作
|
|
407
|
+
if (typeof key === 'string' && key.includes('.')) {
|
|
408
|
+
const keys = key.split('.');
|
|
409
|
+
const lastKey = keys.pop();
|
|
410
|
+
key = { requestParam: lastKey, column: key };
|
|
411
|
+
}
|
|
412
|
+
// 单表字段无别名的情况下操作
|
|
413
|
+
if (typeof key === 'string') {
|
|
414
|
+
if (query[key] || query[key] === 0) {
|
|
415
|
+
c[key] = query[key];
|
|
416
|
+
const eq = query[key] instanceof Array ? 'in' : '=';
|
|
417
|
+
if (eq === 'in') {
|
|
418
|
+
find.andWhere(`${key} ${eq} (:${key})`, c);
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
find.andWhere(`${key} ${eq} :${key}`, c);
|
|
422
|
+
}
|
|
423
|
+
this.sqlParams.push(query[key]);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
if (query[key.requestParam] || query[key.requestParam] === 0) {
|
|
428
|
+
c[key.column] = query[key.requestParam];
|
|
429
|
+
const eq = query[key.requestParam] instanceof Array ? 'in' : '=';
|
|
430
|
+
if (eq === 'in') {
|
|
431
|
+
find.andWhere(`${key.column} ${eq} (:${key.column})`, c);
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
find.andWhere(`${key.column} ${eq} :${key.column}`, c);
|
|
435
|
+
}
|
|
436
|
+
this.sqlParams.push(query[key.requestParam]);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
// 字段模糊查询
|
|
442
|
+
if (!_.isEmpty(option.fieldLike)) {
|
|
443
|
+
for (let key of option.fieldLike) {
|
|
444
|
+
// 如果key有包含.的情况下操作
|
|
445
|
+
if (typeof key === 'string' && key.includes('.')) {
|
|
446
|
+
const keys = key.split('.');
|
|
447
|
+
const lastKey = keys.pop();
|
|
448
|
+
key = { requestParam: lastKey, column: key };
|
|
449
|
+
}
|
|
450
|
+
// 单表字段无别名的情况下操作
|
|
451
|
+
if (typeof key === 'string') {
|
|
452
|
+
if (query[key] || query[key] === 0) {
|
|
453
|
+
find.andWhere(`${key} like :${key}`, {
|
|
454
|
+
[key]: `%${query[key]}%`,
|
|
455
|
+
});
|
|
456
|
+
this.sqlParams.push(`%${query[key]}%`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
if (query[key.requestParam] || query[key.requestParam] === 0) {
|
|
461
|
+
find.andWhere(`${key.column} like :${key.column}`, {
|
|
462
|
+
[key.column]: `%${query[key.requestParam]}%`,
|
|
463
|
+
});
|
|
464
|
+
this.sqlParams.push(`%${query[key.requestParam]}%`);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
sqlArr.push(selects.join(','));
|
|
472
|
+
}
|
|
473
|
+
// 接口请求的排序
|
|
474
|
+
if (sort && order) {
|
|
475
|
+
const sorts = sort.toUpperCase().split(',');
|
|
476
|
+
const orders = order.split(',');
|
|
477
|
+
if (sorts.length != orders.length) {
|
|
478
|
+
throw new validate_1.HlwValidateException(global_1.ERRINFO.SORTFIELD);
|
|
479
|
+
}
|
|
480
|
+
for (const i in sorts) {
|
|
481
|
+
find.addOrderBy(SqlString.escapeId(orders[i]), this.checkSort(sorts[i]));
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (option === null || option === void 0 ? void 0 : option.extend) {
|
|
485
|
+
await (option === null || option === void 0 ? void 0 : option.extend(find, this.baseCtx, this.baseApp));
|
|
486
|
+
}
|
|
487
|
+
const sqls = find.getSql().split('FROM');
|
|
488
|
+
sqlArr.push('FROM');
|
|
489
|
+
// 取sqls的最后一个
|
|
490
|
+
sqlArr.push(sqls[sqls.length - 1]);
|
|
491
|
+
return sqlArr.join(' ');
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
exports.BaseMysqlService = BaseMysqlService;
|
|
495
|
+
__decorate([
|
|
496
|
+
(0, core_1.Config)('hlw'),
|
|
497
|
+
__metadata("design:type", Object)
|
|
498
|
+
], BaseMysqlService.prototype, "_hlwConfig", void 0);
|
|
499
|
+
__decorate([
|
|
500
|
+
(0, core_1.Inject)(),
|
|
501
|
+
__metadata("design:type", typeorm_1.TypeORMDataSourceManager)
|
|
502
|
+
], BaseMysqlService.prototype, "typeORMDataSourceManager", void 0);
|
|
503
|
+
__decorate([
|
|
504
|
+
(0, core_1.Inject)(),
|
|
505
|
+
__metadata("design:type", event_1.HlwEventManager)
|
|
506
|
+
], BaseMysqlService.prototype, "hlwEventManager", void 0);
|
|
507
|
+
__decorate([
|
|
508
|
+
(0, core_1.App)(),
|
|
509
|
+
__metadata("design:type", Object)
|
|
510
|
+
], BaseMysqlService.prototype, "baseApp", void 0);
|
|
511
|
+
__decorate([
|
|
512
|
+
(0, core_1.Inject)('ctx'),
|
|
513
|
+
__metadata("design:type", Object)
|
|
514
|
+
], BaseMysqlService.prototype, "baseCtx", void 0);
|
|
515
|
+
__decorate([
|
|
516
|
+
(0, core_1.Init)(),
|
|
517
|
+
__metadata("design:type", Function),
|
|
518
|
+
__metadata("design:paramtypes", []),
|
|
519
|
+
__metadata("design:returntype", void 0)
|
|
520
|
+
], BaseMysqlService.prototype, "init", null);
|
|
521
|
+
exports.BaseMysqlService = BaseMysqlService = __decorate([
|
|
522
|
+
(0, core_1.Provide)(),
|
|
523
|
+
(0, core_2.Scope)(core_2.ScopeEnum.Request, { allowDowngrade: true })
|
|
524
|
+
], BaseMysqlService);
|
|
@@ -0,0 +1,160 @@
|
|
|
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 BasePgService {
|
|
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
|
+
* 将mysql语句转换为postgres语句
|
|
67
|
+
* @param sql
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
protected convertToPostgres(sql: any): any;
|
|
71
|
+
/**
|
|
72
|
+
* 查询sql中的参数个数
|
|
73
|
+
* @param sql
|
|
74
|
+
* @returns
|
|
75
|
+
*/
|
|
76
|
+
protected countDollarSigns(sql: any): any;
|
|
77
|
+
/**
|
|
78
|
+
* 执行SQL并获得分页数据
|
|
79
|
+
* @param sql 执行的sql语句
|
|
80
|
+
* @param query 分页查询条件
|
|
81
|
+
* @param autoSort 是否自动排序
|
|
82
|
+
* @param connectionName 连接名称
|
|
83
|
+
*/
|
|
84
|
+
sqlRenderPage(sql: any, query: any, autoSort?: boolean, connectionName?: any): Promise<{
|
|
85
|
+
list: any;
|
|
86
|
+
pagination: {
|
|
87
|
+
page: number;
|
|
88
|
+
size: number;
|
|
89
|
+
total: number;
|
|
90
|
+
};
|
|
91
|
+
}>;
|
|
92
|
+
/**
|
|
93
|
+
* 检查排序
|
|
94
|
+
* @param sort 排序
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
checkSort(sort: any): any;
|
|
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
|
+
addOrUpdate(param: any | any[], type?: 'add' | 'update'): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* 非分页查询
|
|
122
|
+
* @param query 查询条件
|
|
123
|
+
* @param option 查询配置
|
|
124
|
+
* @param connectionName 连接名
|
|
125
|
+
*/
|
|
126
|
+
list(query: any, option: any, connectionName?: any): Promise<any>;
|
|
127
|
+
/**
|
|
128
|
+
* 分页查询
|
|
129
|
+
* @param query 查询条件
|
|
130
|
+
* @param option 查询配置
|
|
131
|
+
* @param connectionName 连接名
|
|
132
|
+
*/
|
|
133
|
+
page(query: any, option: any, connectionName?: any): Promise<{
|
|
134
|
+
list: any;
|
|
135
|
+
pagination: {
|
|
136
|
+
page: number;
|
|
137
|
+
size: number;
|
|
138
|
+
total: number;
|
|
139
|
+
};
|
|
140
|
+
}>;
|
|
141
|
+
/**
|
|
142
|
+
* 构建查询配置
|
|
143
|
+
* @param query 前端查询
|
|
144
|
+
* @param option
|
|
145
|
+
*/
|
|
146
|
+
getOptionFind(query: any, option: QueryOp): Promise<string>;
|
|
147
|
+
/**
|
|
148
|
+
* 替换sql中的表别名
|
|
149
|
+
* @param sql
|
|
150
|
+
* @returns
|
|
151
|
+
*/
|
|
152
|
+
replaceOrderByPrefix(sql: any): any;
|
|
153
|
+
/**
|
|
154
|
+
* 筛选的字段匹配
|
|
155
|
+
* @param select 筛选的字段
|
|
156
|
+
* @param field 字段
|
|
157
|
+
* @returns 字段在哪个表中
|
|
158
|
+
*/
|
|
159
|
+
protected matchColumn(select: string[], field: string): string;
|
|
160
|
+
}
|