-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
196 lines (147 loc) · 5.14 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(function () {
var QueryBuilder = (function () {
var QueryBuilder = function () {
var self = this;
var q = {page: 1, size: 1000, where: []};
var group = {
"ANDOR": "AND",
"GROUP": []
};
var OP = ["in", "IN", "not in", "NOT IN", "is", "IS", "is not", "IS NOT", "<>", "!=", "=", "<", "<=", ">=", ">", "like", "LIKE"];
self.setDomain = function (domainId) {
q.domain = domainId;
return self;
}
self.setModel = function (modelName) {
q.row_model = modelName;
return self;
}
self.setPage = function (page) {
q.page = page;
return self;
}
self.setPageSize = function (pageSize) {
q.size = pageSize;
return self;
}
self.orderBy = function (field, asc) {
if (!asc) {
asc = false;
}
q.order_by = {};
q.order_by.ASC = asc;
q.order_by.FIELD = field
return self;
}
self.fetchModels = function (arrayOfModelNames) {
q.fetch = arrayOfModelNames;
return self;
}
self.addObjectArray = function (array) {
// prevent non array items to be addded.
if (Array && !Array.isArray(array)) {
return;
}
q.where = null;
if (!q.rows) {
q.rows = [];
}
q.rows = q.rows.concat(array);
return self;
}
self.addObject = function (object) {
q.where = null;
if (!q.rows) {
q.rows = [];
}
q.rows.push(object);
return self;
}
self.whereWithKeys = function (keysArray) {
q.where = {keys: keysArray};
return self;
}
self.newGroup = function (connectorANDorOR) {
q.rows = null;
// override array where
if (!Array.isArray(q.where)) {
q.where = [];
}
if (group.GROUP.length > 0) {
q.where.push(group);
}
group = {
"ANDOR": connectorANDorOR,
"GROUP": []
};
return self;
}
self.setReferenceJoin = function (operator, filter_field, reference_field, model, value) {
q.reference_join = {
"row_model": model,
"filter": {
"OP": operator,
"VAL": value,
"FIELD": filter_field
},
"reference_field": reference_field
}
return self;
}
self.addCondition = function (connectorANDorOR, fieldName, operator, value) {
// override array where
if (!Array.isArray(q.where)) {
q.where = [];
}
// first connector is ALWAYS AND
if (group.GROUP.length < 1) {
connectorANDorOR = "AND";
}
// allow only letters and '.' in the fields.
if (/^[a-zA-Z0-9\._-]+$/.test(fieldName) == false) {
throw new Error("Invalid FIELD NAME: " + fieldName)
}
// check if the user is using valid operators.
if (OP.indexOf(operator) < 0) {
throw new Error("Invalid operator: " + operator)
}
if (value === undefined) {
throw new Error("Invalid value: " + value);
}
group.GROUP.push({
"ANDOR": connectorANDorOR,
"FIELD": fieldName,
"OP": operator,
"VAL": value
});
return self;
}
self.compile = function () {
if (q.where) {
delete q.rows;
if (Array.isArray(q.where) && group.GROUP.length > 0) {
q.where.push(group);
}
} else if (q.rows) {
delete q.where;
if (q.order_by) {
delete q.order_by;
}
}
return q;
}
};
return QueryBuilder;
})();
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
module.exports = QueryBuilder;
else {
var global;
try {
global = Function('return this')() || (42, eval)('this');
} catch (e) {
global = window;
}
global.QueryBuilder = QueryBuilder;
}
})();