query-core 0.1.31 → 0.2.1
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/lib/query.js +84 -85
- package/lib/search.js +15 -2
- package/package.json +1 -1
- package/src/SearchBuilder.ts +134 -101
- package/src/batch.ts +149 -106
- package/src/build.ts +426 -382
- package/src/client.ts +139 -107
- package/src/health.ts +38 -38
- package/src/index.ts +74 -69
- package/src/map.ts +27 -27
- package/src/metadata.ts +74 -57
- package/src/query.ts +198 -189
- package/src/search.ts +95 -67
- package/src/services.ts +474 -423
package/lib/query.js
CHANGED
|
@@ -3,28 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
var build_1 = require("./build");
|
|
4
4
|
function buildSort(sort, map) {
|
|
5
5
|
if (!sort || sort.length === 0) {
|
|
6
|
-
return
|
|
6
|
+
return "";
|
|
7
7
|
}
|
|
8
8
|
var sort2 = [];
|
|
9
9
|
if (sort && sort.length > 0) {
|
|
10
|
-
var sorts = sort.split(
|
|
10
|
+
var sorts = sort.split(",");
|
|
11
11
|
for (var _i = 0, sorts_1 = sorts; _i < sorts_1.length; _i++) {
|
|
12
12
|
var st = sorts_1[_i];
|
|
13
13
|
if (st.length > 0) {
|
|
14
14
|
var field = st;
|
|
15
15
|
var tp = st.charAt(0);
|
|
16
|
-
if (tp ===
|
|
17
|
-
field = st.
|
|
16
|
+
if (tp === "-" || tp === "+") {
|
|
17
|
+
field = st.substring(1);
|
|
18
18
|
}
|
|
19
|
-
var sortType =
|
|
19
|
+
var sortType = tp === "-" ? " desc" : "";
|
|
20
20
|
sort2.push(getField(field.trim(), map) + sortType);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
if (sort2.length === 0) {
|
|
25
|
-
return
|
|
25
|
+
return "";
|
|
26
26
|
}
|
|
27
|
-
return sort2.join(
|
|
27
|
+
return sort2.join(",");
|
|
28
28
|
}
|
|
29
29
|
exports.buildSort = buildSort;
|
|
30
30
|
function getField(name, map) {
|
|
@@ -35,7 +35,7 @@ function getField(name, map) {
|
|
|
35
35
|
if (!x) {
|
|
36
36
|
return name;
|
|
37
37
|
}
|
|
38
|
-
if (typeof x ===
|
|
38
|
+
if (typeof x === "string") {
|
|
39
39
|
return x;
|
|
40
40
|
}
|
|
41
41
|
if (x.column) {
|
|
@@ -45,15 +45,15 @@ function getField(name, map) {
|
|
|
45
45
|
}
|
|
46
46
|
exports.getField = getField;
|
|
47
47
|
function buildMsSQLParam(i) {
|
|
48
|
-
return
|
|
48
|
+
return "@" + i;
|
|
49
49
|
}
|
|
50
50
|
exports.buildMsSQLParam = buildMsSQLParam;
|
|
51
51
|
function buildOracleParam(i) {
|
|
52
|
-
return
|
|
52
|
+
return ":" + i;
|
|
53
53
|
}
|
|
54
54
|
exports.buildOracleParam = buildOracleParam;
|
|
55
55
|
function buildDollarParam(i) {
|
|
56
|
-
return
|
|
56
|
+
return "$" + i;
|
|
57
57
|
}
|
|
58
58
|
exports.buildDollarParam = buildDollarParam;
|
|
59
59
|
function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq, strExcluding) {
|
|
@@ -61,10 +61,10 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
61
61
|
return undefined;
|
|
62
62
|
}
|
|
63
63
|
var s = filter;
|
|
64
|
-
var like =
|
|
64
|
+
var like = "like";
|
|
65
65
|
var param;
|
|
66
|
-
if (typeof bparam ===
|
|
67
|
-
if (bparam ===
|
|
66
|
+
if (typeof bparam === "string") {
|
|
67
|
+
if (bparam === "ilike") {
|
|
68
68
|
like = bparam;
|
|
69
69
|
}
|
|
70
70
|
param = buildDollarParam;
|
|
@@ -73,7 +73,7 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
73
73
|
param = bparam;
|
|
74
74
|
}
|
|
75
75
|
if (!like) {
|
|
76
|
-
like =
|
|
76
|
+
like = "like";
|
|
77
77
|
}
|
|
78
78
|
var filters = [];
|
|
79
79
|
var q;
|
|
@@ -82,15 +82,15 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
82
82
|
if (sq && sq.length > 0) {
|
|
83
83
|
q = s[sq];
|
|
84
84
|
delete s[sq];
|
|
85
|
-
if (q ===
|
|
85
|
+
if (q === "") {
|
|
86
86
|
q = undefined;
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
if (strExcluding && strExcluding.length > 0) {
|
|
90
90
|
excluding = s[strExcluding];
|
|
91
91
|
delete s[strExcluding];
|
|
92
|
-
if (typeof excluding ===
|
|
93
|
-
excluding = excluding.split(
|
|
92
|
+
if (typeof excluding === "string") {
|
|
93
|
+
excluding = excluding.split(",");
|
|
94
94
|
}
|
|
95
95
|
if (excluding && excluding.length === 0) {
|
|
96
96
|
excluding = undefined;
|
|
@@ -106,28 +106,28 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
106
106
|
if (v !== undefined && v != null) {
|
|
107
107
|
var attr = attrs[key];
|
|
108
108
|
if (attr) {
|
|
109
|
-
field =
|
|
110
|
-
if (typeof v ===
|
|
109
|
+
field = attr.column ? attr.column : key;
|
|
110
|
+
if (typeof v === "string") {
|
|
111
111
|
if (v.length !== 0) {
|
|
112
112
|
if (attr.q) {
|
|
113
113
|
ex.push(key);
|
|
114
114
|
}
|
|
115
|
-
if (attr.match ===
|
|
115
|
+
if (attr.match === "equal") {
|
|
116
116
|
filters.push(field + " = " + param(i++));
|
|
117
117
|
args.push(v);
|
|
118
118
|
}
|
|
119
|
-
else if (attr.match ===
|
|
119
|
+
else if (attr.match === "prefix") {
|
|
120
120
|
filters.push(field + " " + like + " " + param(i++));
|
|
121
|
-
args.push(v +
|
|
121
|
+
args.push(v + "%");
|
|
122
122
|
}
|
|
123
123
|
else {
|
|
124
124
|
filters.push(field + " " + like + " " + param(i++));
|
|
125
|
-
args.push(
|
|
125
|
+
args.push("%" + v + "%");
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
else if (v instanceof Date) {
|
|
130
|
-
if (attr.match ===
|
|
130
|
+
if (attr.match === "max") {
|
|
131
131
|
filters.push(field + " <= " + param(i++));
|
|
132
132
|
args.push(v);
|
|
133
133
|
}
|
|
@@ -136,19 +136,19 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
136
136
|
args.push(v);
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
-
else if (typeof v ===
|
|
140
|
-
if (attr.match ===
|
|
139
|
+
else if (typeof v === "number") {
|
|
140
|
+
if (attr.match === "max") {
|
|
141
141
|
filters.push(field + " <= " + v);
|
|
142
142
|
}
|
|
143
143
|
else {
|
|
144
144
|
filters.push(field + " >= " + v);
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
|
-
else if (attr.type ===
|
|
147
|
+
else if (attr.type === "ObjectId") {
|
|
148
148
|
filters.push(field + " = " + param(i++));
|
|
149
149
|
args.push(v);
|
|
150
150
|
}
|
|
151
|
-
else if (typeof v ===
|
|
151
|
+
else if (typeof v === "object") {
|
|
152
152
|
if (Array.isArray(v)) {
|
|
153
153
|
if (v.length > 0) {
|
|
154
154
|
var ps = build_1.params(v.length, param, i - 1);
|
|
@@ -157,65 +157,65 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
157
157
|
var sv = v_1[_a];
|
|
158
158
|
args.push(sv);
|
|
159
159
|
}
|
|
160
|
-
filters.push(field + " in (" + ps.join(
|
|
160
|
+
filters.push(field + " in (" + ps.join(",") + ")");
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
-
else if (attr.type ===
|
|
163
|
+
else if (attr.type === "date" || attr.type === "datetime") {
|
|
164
164
|
if (isDateRange(v)) {
|
|
165
|
-
if (v[
|
|
165
|
+
if (v["max"]) {
|
|
166
166
|
filters.push(field + " <= " + param(i++));
|
|
167
|
-
args.push(v[
|
|
167
|
+
args.push(v["max"]);
|
|
168
168
|
}
|
|
169
|
-
else if (v[
|
|
169
|
+
else if (v["top"]) {
|
|
170
170
|
filters.push(field + " < " + param(i++));
|
|
171
|
-
args.push(v[
|
|
171
|
+
args.push(v["top"]);
|
|
172
172
|
}
|
|
173
|
-
else if (v[
|
|
173
|
+
else if (v["endDate"]) {
|
|
174
174
|
filters.push(field + " <= " + param(i++));
|
|
175
|
-
args.push(v[
|
|
175
|
+
args.push(v["endDate"]);
|
|
176
176
|
}
|
|
177
|
-
else if (v[
|
|
177
|
+
else if (v["upper"]) {
|
|
178
178
|
filters.push(field + " < " + param(i++));
|
|
179
|
-
args.push(v[
|
|
179
|
+
args.push(v["upper"]);
|
|
180
180
|
}
|
|
181
|
-
else if (v[
|
|
181
|
+
else if (v["endTime"]) {
|
|
182
182
|
filters.push(field + " < " + param(i++));
|
|
183
|
-
args.push(v[
|
|
183
|
+
args.push(v["endTime"]);
|
|
184
184
|
}
|
|
185
|
-
if (v[
|
|
185
|
+
if (v["min"]) {
|
|
186
186
|
filters.push(field + " >= " + param(i++));
|
|
187
|
-
args.push(v[
|
|
187
|
+
args.push(v["min"]);
|
|
188
188
|
}
|
|
189
|
-
else if (v[
|
|
189
|
+
else if (v["startTime"]) {
|
|
190
190
|
filters.push(field + " >= " + param(i++));
|
|
191
|
-
args.push(v[
|
|
191
|
+
args.push(v["startTime"]);
|
|
192
192
|
}
|
|
193
|
-
else if (v[
|
|
193
|
+
else if (v["startDate"]) {
|
|
194
194
|
filters.push(field + " >= " + param(i++));
|
|
195
|
-
args.push(v[
|
|
195
|
+
args.push(v["startDate"]);
|
|
196
196
|
}
|
|
197
|
-
else if (v[
|
|
197
|
+
else if (v["lower"]) {
|
|
198
198
|
filters.push(field + " > " + param(i++));
|
|
199
|
-
args.push(v[
|
|
199
|
+
args.push(v["lower"]);
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
|
-
else if (attr.type ===
|
|
203
|
+
else if (attr.type === "number" || attr.type === "integer") {
|
|
204
204
|
if (isNumberRange(v)) {
|
|
205
|
-
if (v[
|
|
206
|
-
filters.push(field + " <= " + v[
|
|
205
|
+
if (v["max"]) {
|
|
206
|
+
filters.push(field + " <= " + v["max"]);
|
|
207
207
|
}
|
|
208
|
-
else if (v[
|
|
209
|
-
filters.push(field + " < " + v[
|
|
208
|
+
else if (v["top"]) {
|
|
209
|
+
filters.push(field + " < " + v["top"]);
|
|
210
210
|
}
|
|
211
|
-
else if (v[
|
|
212
|
-
filters.push(field + " < " + v[
|
|
211
|
+
else if (v["upper"]) {
|
|
212
|
+
filters.push(field + " < " + v["upper"]);
|
|
213
213
|
}
|
|
214
|
-
if (v[
|
|
215
|
-
filters.push(field + " >= " + v[
|
|
214
|
+
if (v["min"]) {
|
|
215
|
+
filters.push(field + " >= " + v["min"]);
|
|
216
216
|
}
|
|
217
|
-
else if (v[
|
|
218
|
-
filters.push(field + " > " + v[
|
|
217
|
+
else if (v["lower"]) {
|
|
218
|
+
filters.push(field + " > " + v["lower"]);
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
}
|
|
@@ -224,14 +224,13 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
var idField = getId(attrs);
|
|
227
|
-
var c = [];
|
|
228
227
|
if (idField && excluding && excluding.length > 0) {
|
|
229
228
|
var l = excluding.length;
|
|
230
229
|
var ps = [];
|
|
231
230
|
for (var _b = 0, excluding_1 = excluding; _b < excluding_1.length; _b++) {
|
|
232
231
|
var k = excluding_1[_b];
|
|
233
232
|
if (k != null && k !== undefined) {
|
|
234
|
-
if (typeof k ===
|
|
233
|
+
if (typeof k === "number") {
|
|
235
234
|
ps.push(k.toString());
|
|
236
235
|
}
|
|
237
236
|
else {
|
|
@@ -240,7 +239,7 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
240
239
|
}
|
|
241
240
|
}
|
|
242
241
|
}
|
|
243
|
-
filters.push(idField + " not in (" + ps.join(
|
|
242
|
+
filters.push(idField + " not in (" + ps.join(",") + ")");
|
|
244
243
|
}
|
|
245
244
|
if (q && attrs) {
|
|
246
245
|
var qkeys = Object.keys(attrs);
|
|
@@ -248,35 +247,35 @@ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq,
|
|
|
248
247
|
for (var _c = 0, qkeys_1 = qkeys; _c < qkeys_1.length; _c++) {
|
|
249
248
|
var field = qkeys_1[_c];
|
|
250
249
|
var attr = attrs[field];
|
|
251
|
-
if (attr.q && (attr.type === undefined || attr.type ===
|
|
252
|
-
|
|
253
|
-
|
|
250
|
+
if (attr.q && (attr.type === undefined || attr.type === "string") && !ex.includes(field)) {
|
|
251
|
+
var column = attr.column ? attr.column : field;
|
|
252
|
+
if (attr.match === "equal") {
|
|
253
|
+
qfilters.push(column + " = " + param(i++));
|
|
254
254
|
args.push(q);
|
|
255
255
|
}
|
|
256
|
-
else if (attr.match ===
|
|
257
|
-
qfilters.push(
|
|
258
|
-
args.push(q +
|
|
256
|
+
else if (attr.match === "prefix") {
|
|
257
|
+
qfilters.push(column + " " + like + " " + param(i++));
|
|
258
|
+
args.push(q + "%");
|
|
259
259
|
}
|
|
260
260
|
else {
|
|
261
|
-
qfilters.push(
|
|
262
|
-
args.push(
|
|
261
|
+
qfilters.push(column + " " + like + " " + param(i++));
|
|
262
|
+
args.push("%" + q + "%");
|
|
263
263
|
}
|
|
264
|
-
c.push(buildQ(field, q, attr.match));
|
|
265
264
|
}
|
|
266
265
|
}
|
|
267
266
|
if (qfilters.length > 0) {
|
|
268
|
-
filters.push("(" + qfilters.join(
|
|
267
|
+
filters.push("(" + qfilters.join(" or ") + ")");
|
|
269
268
|
}
|
|
270
269
|
}
|
|
271
270
|
var buildS = buildSort3 ? buildSort3 : buildSort;
|
|
272
271
|
var sSort = buildS(sort, attrs);
|
|
273
|
-
var sOrderBy =
|
|
272
|
+
var sOrderBy = sSort.length > 0 ? " order by " + sSort : "";
|
|
274
273
|
if (filters.length === 0) {
|
|
275
274
|
var sql = "select " + buildFieldsByAttributes(attrs, fields) + " from " + table + sOrderBy;
|
|
276
275
|
return { query: sql, params: args };
|
|
277
276
|
}
|
|
278
277
|
else {
|
|
279
|
-
var sql = "select " + buildFieldsByAttributes(attrs, fields) + " from " + table + " where " + filters.join(
|
|
278
|
+
var sql = "select " + buildFieldsByAttributes(attrs, fields) + " from " + table + " where " + filters.join(" and ") + sOrderBy;
|
|
280
279
|
return { query: sql, params: args };
|
|
281
280
|
}
|
|
282
281
|
}
|
|
@@ -287,7 +286,7 @@ function getId(attrs) {
|
|
|
287
286
|
var key = qkeys_2[_i];
|
|
288
287
|
var attr = attrs[key];
|
|
289
288
|
if (attr.key) {
|
|
290
|
-
var field =
|
|
289
|
+
var field = attr.column ? attr.column : key;
|
|
291
290
|
return field;
|
|
292
291
|
}
|
|
293
292
|
}
|
|
@@ -296,22 +295,22 @@ function getId(attrs) {
|
|
|
296
295
|
exports.getId = getId;
|
|
297
296
|
function buildFieldsByAttributes(attrs, fields) {
|
|
298
297
|
if (!fields || fields.length === 0) {
|
|
299
|
-
return
|
|
298
|
+
return "*";
|
|
300
299
|
}
|
|
301
300
|
var cols = [];
|
|
302
301
|
for (var _i = 0, fields_1 = fields; _i < fields_1.length; _i++) {
|
|
303
302
|
var f = fields_1[_i];
|
|
304
303
|
var attr = attrs[f];
|
|
305
304
|
if (attr) {
|
|
306
|
-
var field =
|
|
305
|
+
var field = attr.column ? attr.column : f;
|
|
307
306
|
cols.push(field);
|
|
308
307
|
}
|
|
309
308
|
}
|
|
310
309
|
if (cols.length === 0) {
|
|
311
|
-
return
|
|
310
|
+
return "*";
|
|
312
311
|
}
|
|
313
312
|
else {
|
|
314
|
-
return cols.join(
|
|
313
|
+
return cols.join(",");
|
|
315
314
|
}
|
|
316
315
|
}
|
|
317
316
|
exports.buildFieldsByAttributes = buildFieldsByAttributes;
|
|
@@ -321,10 +320,10 @@ function isEmpty(s) {
|
|
|
321
320
|
exports.isEmpty = isEmpty;
|
|
322
321
|
function buildQ(field, q, match) {
|
|
323
322
|
var o = {};
|
|
324
|
-
if (match ===
|
|
323
|
+
if (match === "equal") {
|
|
325
324
|
o[field] = q;
|
|
326
325
|
}
|
|
327
|
-
else if (match ===
|
|
326
|
+
else if (match === "prefix") {
|
|
328
327
|
o[field] = new RegExp("^" + q);
|
|
329
328
|
}
|
|
330
329
|
else {
|
|
@@ -334,10 +333,10 @@ function buildQ(field, q, match) {
|
|
|
334
333
|
}
|
|
335
334
|
exports.buildQ = buildQ;
|
|
336
335
|
function buildMatch(v, match) {
|
|
337
|
-
if (match ===
|
|
336
|
+
if (match === "equal") {
|
|
338
337
|
return v;
|
|
339
338
|
}
|
|
340
|
-
else if (match ===
|
|
339
|
+
else if (match === "prefix") {
|
|
341
340
|
return new RegExp("^" + v);
|
|
342
341
|
}
|
|
343
342
|
else {
|
|
@@ -362,7 +361,7 @@ function isNumberRange(obj) {
|
|
|
362
361
|
for (var _i = 0, keys_3 = keys; _i < keys_3.length; _i++) {
|
|
363
362
|
var key = keys_3[_i];
|
|
364
363
|
var v = obj[key];
|
|
365
|
-
if (typeof v !==
|
|
364
|
+
if (typeof v !== "number") {
|
|
366
365
|
return false;
|
|
367
366
|
}
|
|
368
367
|
}
|
package/lib/search.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function
|
|
3
|
+
function getOffset(limit, page, ifirstPageSize) {
|
|
4
|
+
if (ifirstPageSize && ifirstPageSize > 0) {
|
|
5
|
+
var offset = limit * (page - 2) + ifirstPageSize;
|
|
6
|
+
return offset < 0 ? 0 : offset;
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
var offset = limit * (page - 1);
|
|
10
|
+
return offset < 0 ? 0 : offset;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.getOffset = getOffset;
|
|
14
|
+
function buildFromQuery(query, sql, params, limit, page, mp, bools, provider, totalCol) {
|
|
4
15
|
if (!limit || limit <= 0) {
|
|
5
16
|
return query(sql, params, mp, bools).then(function (list) {
|
|
6
17
|
var total = (list ? list.length : undefined);
|
|
@@ -8,6 +19,8 @@ function buildFromQuery(query, sql, params, limit, offset, mp, bools, provider,
|
|
|
8
19
|
});
|
|
9
20
|
}
|
|
10
21
|
else {
|
|
22
|
+
var ipage = (!page || page <= 0 ? 1 : page);
|
|
23
|
+
var offset = getOffset(limit, ipage);
|
|
11
24
|
if (provider === exports.oracle) {
|
|
12
25
|
if (!totalCol || totalCol.length === 0) {
|
|
13
26
|
totalCol = 'total';
|
|
@@ -100,7 +113,7 @@ function buildPagingQueryForOracle(sql, limit, offset, total) {
|
|
|
100
113
|
i = sql.indexOf(S);
|
|
101
114
|
}
|
|
102
115
|
if (i >= 0) {
|
|
103
|
-
return sql.
|
|
116
|
+
return sql.substring(0, l) + " count(*) over() as " + total + ", " + sql.substring(l) + " offset " + offset + " rows fetch next " + limit + " rows only";
|
|
104
117
|
}
|
|
105
118
|
else {
|
|
106
119
|
return sql + " offset " + offset + " rows fetch next " + limit + " rows only";
|