-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_wrapper.js
71 lines (64 loc) · 1.87 KB
/
query_wrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* 多条件查询属性名称前缀
*/
const IN_PREFIX = 'in'
const GT_PREFIX = 'gt'
const GE_PREFIX = 'ge'
const LT_PREFIX = 'lt'
const LE_PREFIX = 'le'
const NEQ_PREFIX = 'neq'
const NIN_PREFIX = 'nin'
// ------ OR -------------------------------
const O$IN_PREFIX = 'o$in'
const O$GT_PREFIX = 'o$gt'
const O$GE_PREFIX = 'o$ge'
const O$LT_PREFIX = 'o$lt'
const O$LE_PREFIX = 'o$le'
const O$NEQ_PREFIX = 'o$neq'
const O$NIN_PREFIX = 'o$nin'
function deepObjectMerge(FirstOBJ, SecondOBJ) {
// 深度合并对象
Reflect.ownKeys(SecondOBJ).forEach(key => {
FirstOBJ[key] = FirstOBJ[key];
if (!FirstOBJ[key] || FirstOBJ[key].toString() !== '[object Object]')
FirstOBJ[key] = SecondOBJ[key];
else
deepObjectMerge(FirstOBJ[key], SecondOBJ[key]);
});
return FirstOBJ;
}
const { Op } = require("sequelize");
const prefixMap = {
in: Op.in,
gt: Op.gt,
ge: Op.gte,
lt: Op.lt,
le: Op.lte,
neq: Op.ne,
nin: Op.notIn
};
/**
* 根据模型定义自动生成where过滤条件
* @param {ctx.query} query
* @param {Sequelize模型实例Model} model
* @return 返回符合Sequelize模型查找的where过滤条件
*/
function rule(query,model) {
let where = {};
for (let property in model.rawAttributes) {
if (!query[property]) continue
let key = model.rawAttributes[property].type.key
let value = query[property];
if (value.toString()[0] === "[")
value = JSON.parse(value);
//不是虚拟属性
if (key !== 'VIRTUAL') where[property] = value;
else if (property.indexOf('_') != -1) {
//虚拟属性中条件查询
const [prefix, attri] = property.split('_')
prefixMap[prefix] && deepObjectMerge(where, { [attri]: { [prefixMap[prefix]]: value } });
}
}
return where;
}
module.exports.rule = rule;