couchset 0.0.0 → 0.0.5
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/LICENSE +21 -0
- package/README.md +231 -0
- package/dist/automate/index.d.ts +4 -0
- package/dist/automate/index.js +17 -0
- package/dist/automate/index.js.map +1 -0
- package/dist/automate/middlewares/index.d.ts +1 -0
- package/dist/automate/middlewares/index.js +14 -0
- package/dist/automate/middlewares/index.js.map +1 -0
- package/dist/automate/middlewares/isAuth.d.ts +7 -0
- package/dist/automate/middlewares/isAuth.js +28 -0
- package/dist/automate/middlewares/isAuth.js.map +1 -0
- package/dist/automate/model/client.d.ts +12 -0
- package/dist/automate/model/client.js +33 -0
- package/dist/automate/model/client.js.map +1 -0
- package/dist/automate/model/index.d.ts +44 -0
- package/dist/automate/model/index.js +304 -0
- package/dist/automate/model/index.js.map +1 -0
- package/dist/automate/morpheus/index.d.ts +14 -0
- package/dist/automate/morpheus/index.js +171 -0
- package/dist/automate/morpheus/index.js.map +1 -0
- package/dist/automate/writer/index.d.ts +10 -0
- package/dist/automate/writer/index.js +72 -0
- package/dist/automate/writer/index.js.map +1 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -0
- package/dist/connection.d.ts +36 -0
- package/dist/connection.js +129 -0
- package/dist/connection.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/dist/model/index.d.ts +107 -0
- package/dist/model/index.js +329 -0
- package/dist/model/index.js.map +1 -0
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +14 -0
- package/dist/pagination/index.js.map +1 -0
- package/dist/pagination/pagination.d.ts +19 -0
- package/dist/pagination/pagination.js +97 -0
- package/dist/pagination/pagination.js.map +1 -0
- package/dist/query/base-query.d.ts +17 -0
- package/dist/query/base-query.js +15 -0
- package/dist/query/base-query.js.map +1 -0
- package/dist/query/exceptions.d.ts +24 -0
- package/dist/query/exceptions.js +90 -0
- package/dist/query/exceptions.js.map +1 -0
- package/dist/query/helpers/builders.d.ts +64 -0
- package/dist/query/helpers/builders.js +415 -0
- package/dist/query/helpers/builders.js.map +1 -0
- package/dist/query/helpers/dictionary.d.ts +45 -0
- package/dist/query/helpers/dictionary.js +105 -0
- package/dist/query/helpers/dictionary.js.map +1 -0
- package/dist/query/helpers/index.d.ts +3 -0
- package/dist/query/helpers/index.js +16 -0
- package/dist/query/helpers/index.js.map +1 -0
- package/dist/query/helpers/reservedWords.d.ts +1 -0
- package/dist/query/helpers/reservedWords.js +200 -0
- package/dist/query/helpers/reservedWords.js.map +1 -0
- package/dist/query/index.d.ts +5 -0
- package/dist/query/index.js +27 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/interface/query.types.d.ts +236 -0
- package/dist/query/interface/query.types.js +3 -0
- package/dist/query/interface/query.types.js.map +1 -0
- package/dist/query/query.cluster.d.ts +14 -0
- package/dist/query/query.cluster.js +80 -0
- package/dist/query/query.cluster.js.map +1 -0
- package/dist/query/query.d.ts +321 -0
- package/dist/query/query.js +457 -0
- package/dist/query/query.js.map +1 -0
- package/dist/query/utils.d.ts +8 -0
- package/dist/query/utils.js +56 -0
- package/dist/query/utils.js.map +1 -0
- package/dist/search/customQuery.d.ts +17 -0
- package/dist/search/customQuery.js +83 -0
- package/dist/search/customQuery.js.map +1 -0
- package/dist/search/index.d.ts +1 -0
- package/dist/search/index.js +14 -0
- package/dist/search/index.js.map +1 -0
- package/dist/shared/ContextType.d.ts +27 -0
- package/dist/shared/ContextType.js +55 -0
- package/dist/shared/ContextType.js.map +1 -0
- package/dist/shared/common.model.d.ts +37 -0
- package/dist/shared/common.model.js +148 -0
- package/dist/shared/common.model.js.map +1 -0
- package/dist/shared/index.d.ts +2 -0
- package/dist/shared/index.js +15 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/utils/awaitTo.d.ts +17 -0
- package/dist/utils/awaitTo.js +31 -0
- package/dist/utils/awaitTo.js.map +1 -0
- package/dist/utils/date.d.ts +11 -0
- package/dist/utils/date.js +21 -0
- package/dist/utils/date.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/log.d.ts +9 -0
- package/dist/utils/log.js +19 -0
- package/dist/utils/log.js.map +1 -0
- package/dist/utils/text.utils.d.ts +14 -0
- package/dist/utils/text.utils.js +45 -0
- package/dist/utils/text.utils.js.map +1 -0
- package/dist/utils/utils.schema.d.ts +6 -0
- package/dist/utils/utils.schema.js +33 -0
- package/dist/utils/utils.schema.js.map +1 -0
- package/dist/uuid.d.ts +1 -0
- package/dist/uuid.js +9 -0
- package/dist/uuid.js.map +1 -0
- package/docs/couch.png +0 -0
- package/docs/couchset.png +0 -0
- package/docs/couchset_logo.png +0 -0
- package/package.json +108 -30
- package/register/index.js +1 -0
- package/register/transpile-only.js +3 -0
- package/register/type-check.js +3 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
+
if (!m) return o;
|
|
20
|
+
var i = m.call(o), r, ar = [], e;
|
|
21
|
+
try {
|
|
22
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
+
}
|
|
24
|
+
catch (error) { e = { error: error }; }
|
|
25
|
+
finally {
|
|
26
|
+
try {
|
|
27
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
+
}
|
|
29
|
+
finally { if (e) throw e.error; }
|
|
30
|
+
}
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
33
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
34
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
35
|
+
if (ar || !(i in from)) {
|
|
36
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
37
|
+
ar[i] = from[i];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
41
|
+
};
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.QueryBuilder = void 0;
|
|
44
|
+
var base_query_1 = require("./base-query");
|
|
45
|
+
var exceptions_1 = require("./exceptions");
|
|
46
|
+
var helpers_1 = require("./helpers");
|
|
47
|
+
var QueryBuilder = /** @class */ (function (_super) {
|
|
48
|
+
__extends(QueryBuilder, _super);
|
|
49
|
+
/**
|
|
50
|
+
* @summary Create an instance of QueryBuilder.
|
|
51
|
+
* @name QueryBuilder
|
|
52
|
+
* @class
|
|
53
|
+
* @public
|
|
54
|
+
*
|
|
55
|
+
* @param conditions List of SELECT clause conditions
|
|
56
|
+
* @param collection Collection name
|
|
57
|
+
* @returns QueryBuilder
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const query = new QueryBuilder({$select: [{$field: 'address'}], $where: {$nill: [{ address: { $like: '%57-59%' } }, { free_breakfast: true }, { free_lunch: [1] }]}}, 'travel-sample');
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function QueryBuilder(conditions, collection) {
|
|
65
|
+
var _this = _super.call(this, conditions, collection) || this;
|
|
66
|
+
if (conditions) {
|
|
67
|
+
_this.compileFromConditions(conditions);
|
|
68
|
+
}
|
|
69
|
+
return _this;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Add result selectors to SELECT clause.
|
|
73
|
+
* @method
|
|
74
|
+
* @public
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
79
|
+
* const result = query.select([{$field: 'address'}]).build()
|
|
80
|
+
* console.log(result)
|
|
81
|
+
* ```
|
|
82
|
+
* > SELECT address FROM `travel-sample`
|
|
83
|
+
*/
|
|
84
|
+
QueryBuilder.prototype.select = function (value) {
|
|
85
|
+
if (this.queryType === undefined || this.queryType === 'SELECT') {
|
|
86
|
+
this.queryType = 'SELECT';
|
|
87
|
+
if (!value) {
|
|
88
|
+
this.selectExpr = '*';
|
|
89
|
+
}
|
|
90
|
+
else if (typeof value === 'string') {
|
|
91
|
+
this.selectExpr = value;
|
|
92
|
+
}
|
|
93
|
+
else if (typeof value === 'object') {
|
|
94
|
+
this.selectExpr = __spreadArray(__spreadArray([], __read((this.selectExpr || [])), false), __read(value), false);
|
|
95
|
+
}
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
throw new exceptions_1.MultipleQueryTypesException('SELECT', this.queryType);
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Add index type and name to INDEX clause.
|
|
102
|
+
* @method
|
|
103
|
+
* @public
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const result = new QueryBuilder({}, 'travel-sample').index('DROP', 'travel_sample_id_test').build();
|
|
108
|
+
* console.log(result)
|
|
109
|
+
* ```
|
|
110
|
+
* > DROP INDEX `travel-sample`.`travel_sample_id_test`
|
|
111
|
+
*/
|
|
112
|
+
QueryBuilder.prototype.index = function (type, name) {
|
|
113
|
+
if (this.queryType === undefined) {
|
|
114
|
+
if (name.search(/^[A-Za-z][A-Za-z0-9#_]*$/g) === -1) {
|
|
115
|
+
throw new Error('Valid GSI index names can contain any of the following characters: A-Z a-z 0-9 # _, and must start with a letter, [A-Z a-z]');
|
|
116
|
+
}
|
|
117
|
+
this.queryType = 'INDEX';
|
|
118
|
+
this.indexType = type;
|
|
119
|
+
this.indexName = name;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
throw new exceptions_1.MultipleQueryTypesException('INDEX', this.queryType);
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Add items to ON clause in INDEX clause.
|
|
126
|
+
* @method
|
|
127
|
+
* @public
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const on = [{ name: 'travel-sample.callsing', sort: 'ASC' }];
|
|
132
|
+
* const result = new QueryBuilder({}, 'travel-sample').index('CREATE', 'travel_sample_id_test').on(on).build();
|
|
133
|
+
* console.log(result)
|
|
134
|
+
* ```
|
|
135
|
+
* > CREATE INDEX `travel_sample_id_test` ON `travel-sample`(`travel-sample.callsing`['ASC'])
|
|
136
|
+
*/
|
|
137
|
+
QueryBuilder.prototype.on = function (value) {
|
|
138
|
+
if (this.queryType === 'INDEX' &&
|
|
139
|
+
['CREATE', 'CREATE PRIMARY', 'BUILD'].includes(this.indexType || '')) {
|
|
140
|
+
this.indexOn = value;
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
throw new exceptions_1.IndexParamsOnExceptions(['CREATE', 'CREATE PRIMARY', 'BUILD']);
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Create INDEX using General Secondary Index(GSI).
|
|
147
|
+
* @method
|
|
148
|
+
* @public
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* const result = new QueryBuilder({}, 'travel-sample').index('CREATE', 'travel_sample_id_test').usingGSI().build();
|
|
153
|
+
* console.log(result)
|
|
154
|
+
* ```
|
|
155
|
+
* > CREATE INDEX `travel_sample_id_test` USING GSI)
|
|
156
|
+
*/
|
|
157
|
+
QueryBuilder.prototype.usingGSI = function () {
|
|
158
|
+
if (this.queryType === 'INDEX') {
|
|
159
|
+
this.indexUsingGSI = true;
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
throw new exceptions_1.IndexParamsUsingGSIExceptions(['CREATE', 'CREATE PRIMARY', 'BUILD']);
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* Add items to WITH clause in INDEX clause.
|
|
166
|
+
* @method
|
|
167
|
+
* @public
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* const withExpr = {nodes: ['192.168.1.1:8078'],defer_build: true,num_replica: 2};
|
|
172
|
+
* const result = new QueryBuilder({}, 'travel-sample').index('CREATE', 'travel_sample_id_test').with(withExpr).build();
|
|
173
|
+
* console.log(result)
|
|
174
|
+
* ```
|
|
175
|
+
* > CREATE INDEX `travel_sample_id_test` WITH {'nodes': ['192.168.1.1:8078'],'defer_build': true,'num_replica': 2})
|
|
176
|
+
*/
|
|
177
|
+
QueryBuilder.prototype.with = function (value) {
|
|
178
|
+
if (this.queryType === 'INDEX') {
|
|
179
|
+
this.indexWith = value;
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
throw new Error('The WITH clause is only available for Indexes');
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Add WHERE expression to SELECT clause.
|
|
186
|
+
* @method
|
|
187
|
+
* @public
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* const expr_where = {$or: [{ address: { $like: '%57-59%' } }, { free_breakfast: true }]};
|
|
192
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
193
|
+
* const result = query.select([{$field: 'address'}]).where(expr_where).build()
|
|
194
|
+
* console.log(result)
|
|
195
|
+
* ```
|
|
196
|
+
* > SELECT address FROM `travel-sample WHERE (address LIKE '%57-59%' OR free_breakfast = true)`
|
|
197
|
+
*/
|
|
198
|
+
QueryBuilder.prototype.where = function (value) {
|
|
199
|
+
this.whereExpr = value;
|
|
200
|
+
return this;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Add JOIN expression to SELECT clause.
|
|
204
|
+
* @method
|
|
205
|
+
* @public
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```tS
|
|
209
|
+
* const query = new QueryBuilder({}, 'beer-sample brewery');
|
|
210
|
+
* const result = query.select([{$field: 'address'}]).plainJoin('JOIN `beer-sample` beer ON beer.brewery_id = LOWER(REPLACE(brewery.name, " ", "_"))').build()
|
|
211
|
+
* console.log(result)
|
|
212
|
+
* ```
|
|
213
|
+
* > SELECT address FROM `beer-sample brewery` JOIN `beer-sample` beer ON beer.brewery_id = LOWER(REPLACE(brewery.name, " ", "_")) LIMIT 1`
|
|
214
|
+
*/
|
|
215
|
+
QueryBuilder.prototype.plainJoin = function (value) {
|
|
216
|
+
this.plainJoinExpr = value;
|
|
217
|
+
return this;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Add ORDER BY expression to SELECT clause.
|
|
221
|
+
* @method
|
|
222
|
+
* @public
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
227
|
+
* const result = query.select([{$field: 'address'}]).orderBy({ size: 'DESC' }).build()
|
|
228
|
+
* console.log(result)
|
|
229
|
+
* ```
|
|
230
|
+
* > SELECT address FROM `travel-sample ORDER BY size = 'DESC'`
|
|
231
|
+
*/
|
|
232
|
+
QueryBuilder.prototype.orderBy = function (value) {
|
|
233
|
+
this.orderExpr = value;
|
|
234
|
+
return this;
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Add LIMIT expression to SELECT clause.
|
|
238
|
+
* @method
|
|
239
|
+
* @public
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
244
|
+
* const result = query.select([{$field: 'address'}]).limit(10).build()
|
|
245
|
+
* console.log(result)
|
|
246
|
+
* ```
|
|
247
|
+
* > SELECT address FROM `travel-sample LIMIT 10`
|
|
248
|
+
*/
|
|
249
|
+
QueryBuilder.prototype.limit = function (value) {
|
|
250
|
+
this.limitExpr = value;
|
|
251
|
+
return this;
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Add OFFSET expression to SELECT clause.
|
|
255
|
+
* @method
|
|
256
|
+
* @public
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
261
|
+
* const result = query.select([{$field: 'address'}]).offset(10).build()
|
|
262
|
+
* console.log(result)
|
|
263
|
+
* ```
|
|
264
|
+
* > SELECT address FROM `travel-sample OFFSET 10`
|
|
265
|
+
*/
|
|
266
|
+
QueryBuilder.prototype.offset = function (value) {
|
|
267
|
+
this.offSetExpr = value;
|
|
268
|
+
return this;
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Add LET expression to SELECT clause.
|
|
272
|
+
* @method
|
|
273
|
+
* @public
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* const letExpr = [{ key: 'amount_val', value: 10 }];
|
|
278
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
279
|
+
* const result = query.select([{$field: 'address'}]).let(letExpr).build()
|
|
280
|
+
* console.log(result)
|
|
281
|
+
* ```
|
|
282
|
+
* > SELECT address FROM `travel-sample LET amount_val = 10`
|
|
283
|
+
*/
|
|
284
|
+
QueryBuilder.prototype.let = function (value) {
|
|
285
|
+
this.letExpr = value;
|
|
286
|
+
return this;
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Add GROUP BY expression to GROUP BY clause.
|
|
290
|
+
* @method
|
|
291
|
+
* @public
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];
|
|
296
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
297
|
+
* const result = query.select([{$field: 'address'}]).groupBy(groupByExpr).build()
|
|
298
|
+
* console.log(result)
|
|
299
|
+
* ```
|
|
300
|
+
* > SELECT address FROM `travel-sample GROUP BY COUNT(amount) AS amount`
|
|
301
|
+
*/
|
|
302
|
+
QueryBuilder.prototype.groupBy = function (value) {
|
|
303
|
+
this.groupByExpr = value;
|
|
304
|
+
return this;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Add LETTING expression to GROUP BY clause.
|
|
308
|
+
* @method
|
|
309
|
+
* @public
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];
|
|
314
|
+
* const letExpr = [{ key: 'amount_val', value: 10 }];
|
|
315
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
316
|
+
* const result = query.select([{$field: 'address'}]).groupBy(groupByExpr).let(letExpr).build()
|
|
317
|
+
* console.log(result)
|
|
318
|
+
* ```
|
|
319
|
+
* > SELECT address FROM `travel-sample GROUP BY COUNT(amount) AS amount LETTING amount = 10`
|
|
320
|
+
*/
|
|
321
|
+
QueryBuilder.prototype.letting = function (value) {
|
|
322
|
+
this.lettingExpr = value;
|
|
323
|
+
return this;
|
|
324
|
+
};
|
|
325
|
+
/**
|
|
326
|
+
* Add HAVING expression to GROUP BY clause.
|
|
327
|
+
* @method
|
|
328
|
+
* @public
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* ```ts
|
|
332
|
+
* const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];
|
|
333
|
+
* const having = {address: {$like: '%58%'}};
|
|
334
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
335
|
+
* const result = query.select([{$field: 'address'}]).groupBy(groupByExpr).having(having).build()
|
|
336
|
+
* console.log(result)
|
|
337
|
+
* ```
|
|
338
|
+
* > SELECT address FROM `travel-sample GROUP BY COUNT(amount) AS amount HAVING address LIKE '%58%'`
|
|
339
|
+
*/
|
|
340
|
+
QueryBuilder.prototype.having = function (value) {
|
|
341
|
+
this.havingExpr = value;
|
|
342
|
+
return this;
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Add USE KEYS expression to SELECT clause.
|
|
346
|
+
* @method
|
|
347
|
+
* @public
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* ```ts
|
|
351
|
+
* const query = new QueryBuilder({}, 'travel-sample');
|
|
352
|
+
* const result = query.select([{$field: 'address'}]).useKeys(['airlineR_8093']).build()
|
|
353
|
+
* console.log(result)
|
|
354
|
+
* ```
|
|
355
|
+
* > SELECT address FROM `travel-sample USE KEYS ['airlineR_8093']`
|
|
356
|
+
*/
|
|
357
|
+
QueryBuilder.prototype.useKeys = function (value) {
|
|
358
|
+
this.useKeysExpr = value;
|
|
359
|
+
return this;
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* Converts the conditional parameters passed to the constructor to the properties of the N1QL QueryBuilder.
|
|
363
|
+
* @method
|
|
364
|
+
* @public
|
|
365
|
+
*
|
|
366
|
+
*/
|
|
367
|
+
QueryBuilder.prototype.compileFromConditions = function (conditionals) {
|
|
368
|
+
var _this = this;
|
|
369
|
+
Object.keys(conditionals).forEach(function (value) {
|
|
370
|
+
switch (value) {
|
|
371
|
+
case 'select':
|
|
372
|
+
_this.select(conditionals[value]);
|
|
373
|
+
break;
|
|
374
|
+
case 'let':
|
|
375
|
+
!!conditionals[value] && _this.let(conditionals[value]);
|
|
376
|
+
break;
|
|
377
|
+
case 'where':
|
|
378
|
+
!!conditionals[value] && _this.where(conditionals[value]);
|
|
379
|
+
break;
|
|
380
|
+
case 'plainJoin':
|
|
381
|
+
!!conditionals[value] && _this.plainJoin(conditionals[value]);
|
|
382
|
+
break;
|
|
383
|
+
case 'groupBy':
|
|
384
|
+
!!conditionals[value] && _this.groupBy(conditionals[value]);
|
|
385
|
+
break;
|
|
386
|
+
case 'letting':
|
|
387
|
+
!!conditionals[value] && _this.letting(conditionals[value]);
|
|
388
|
+
break;
|
|
389
|
+
case 'having':
|
|
390
|
+
!!conditionals[value] && _this.having(conditionals[value]);
|
|
391
|
+
break;
|
|
392
|
+
case 'orderBy':
|
|
393
|
+
!!conditionals[value] &&
|
|
394
|
+
_this.orderBy(conditionals[value]);
|
|
395
|
+
break;
|
|
396
|
+
case 'limit':
|
|
397
|
+
!!conditionals[value] && _this.limit(conditionals[value]);
|
|
398
|
+
break;
|
|
399
|
+
case 'offset':
|
|
400
|
+
!!conditionals[value] && _this.offset(conditionals[value]);
|
|
401
|
+
break;
|
|
402
|
+
case 'use':
|
|
403
|
+
!!conditionals[value] && _this.useKeys(conditionals[value]);
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
/**
|
|
409
|
+
* Build a n1ql query from the defined parameters.
|
|
410
|
+
* @method
|
|
411
|
+
* @public
|
|
412
|
+
*
|
|
413
|
+
*/
|
|
414
|
+
QueryBuilder.prototype.build = function () {
|
|
415
|
+
switch (this.queryType) {
|
|
416
|
+
case 'INDEX':
|
|
417
|
+
switch (this.indexType) {
|
|
418
|
+
case 'BUILD':
|
|
419
|
+
case 'CREATE':
|
|
420
|
+
case 'CREATE PRIMARY':
|
|
421
|
+
if (this.indexOn) {
|
|
422
|
+
return (0, helpers_1.buildIndexExpr)(this.collection, this.indexType, this.indexName || '', this.indexOn, this.whereExpr, this.indexUsingGSI, this.indexWith);
|
|
423
|
+
}
|
|
424
|
+
case 'DROP':
|
|
425
|
+
return (0, helpers_1.buildIndexExpr)(this.collection, this.indexType, this.indexName || '', undefined, undefined, this.indexUsingGSI);
|
|
426
|
+
}
|
|
427
|
+
case 'SELECT':
|
|
428
|
+
if (this.selectExpr) {
|
|
429
|
+
return (0, helpers_1.selectBuilder)(this.collection, this.selectExpr, this.letExpr, this.whereExpr, this.orderExpr, this.limitExpr, this.offSetExpr, this.useKeysExpr, this.groupByExpr, this.lettingExpr, this.havingExpr, this.plainJoinExpr);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return '';
|
|
433
|
+
};
|
|
434
|
+
Object.defineProperty(QueryBuilder.prototype, "conditions", {
|
|
435
|
+
get: function () {
|
|
436
|
+
return this._conditions;
|
|
437
|
+
},
|
|
438
|
+
set: function (value) {
|
|
439
|
+
this._conditions = value;
|
|
440
|
+
},
|
|
441
|
+
enumerable: false,
|
|
442
|
+
configurable: true
|
|
443
|
+
});
|
|
444
|
+
Object.defineProperty(QueryBuilder.prototype, "collection", {
|
|
445
|
+
get: function () {
|
|
446
|
+
return this._collection;
|
|
447
|
+
},
|
|
448
|
+
set: function (value) {
|
|
449
|
+
this._collection = value;
|
|
450
|
+
},
|
|
451
|
+
enumerable: false,
|
|
452
|
+
configurable: true
|
|
453
|
+
});
|
|
454
|
+
return QueryBuilder;
|
|
455
|
+
}(base_query_1.BaseQuery));
|
|
456
|
+
exports.QueryBuilder = QueryBuilder;
|
|
457
|
+
//# sourceMappingURL=query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/query/query.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAuC;AAYvC,2CAIsB;AACtB,qCAAwD;AAExD;IAAkC,gCAAS;IAsEvC;;;;;;;;;;;;;;OAcG;IACH,sBAAY,UAA0B,EAAE,UAAkB;QAA1D,YACI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CAAC,UAAU,EACf,IAAI,CAAC,aAAa,CACrB,CAAC;iBACL;SACR;QACD,OAAO,EAAE,CAAC;IACd,CAAC;IAED,sBAAI,oCAAU;aAAd;YACI,OAAO,IAAI,CAAC,WAAW,CAAC;QAC5B,CAAC;aAED,UAAe,KAAqB;YAChC,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QAC7B,CAAC;;;OAJA;IAMD,sBAAI,oCAAU;aAAd;YACI,OAAO,IAAI,CAAC,WAAW,CAAC;QAC5B,CAAC;aAED,UAAe,KAAa;YACxB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QAC7B,CAAC;;;OAJA;IAKL,mBAAC;AAAD,CAAC,AAtgBD,CAAkC,sBAAS,GAsgB1C;AAtgBY,oCAAY","sourcesContent":["import {BaseQuery} from './base-query';\nimport {\n IConditionExpr,\n IGroupBy,\n IIndexOnParams,\n IIndexWithParams,\n ILetExpr,\n IndexType,\n ISelectType,\n LogicalWhereExpr,\n SortType,\n} from './interface/query.types';\nimport {\n IndexParamsOnExceptions,\n IndexParamsUsingGSIExceptions,\n MultipleQueryTypesException,\n} from './exceptions';\nimport {buildIndexExpr, selectBuilder} from './helpers';\n\nexport class QueryBuilder extends BaseQuery {\n /**\n * SELECT Expression.\n */\n private selectExpr?: ISelectType[] | string;\n /**\n * WHERE Expression.\n */\n private whereExpr?: LogicalWhereExpr;\n /**\n * ORDER BY Expression.\n */\n private orderExpr?: Record<string, SortType>;\n /**\n * LIMIT Expression.\n */\n private limitExpr?: number;\n /**\n * OFFSET Expression.\n */\n private offSetExpr?: number;\n /**\n * LET Expression.\n */\n private letExpr?: ILetExpr[];\n /**\n * GROUP BY Expression.\n */\n private groupByExpr?: IGroupBy[];\n /**\n * LETTING Expression.\n */\n private lettingExpr?: ILetExpr[];\n /**\n * HAVING Expression.\n */\n private havingExpr?: LogicalWhereExpr;\n\n /**\n * Plain JOIN Expression.\n */\n private plainJoinExpr?: string;\n /**\n * USE Expression.\n */\n private useKeysExpr?: string[];\n /**\n * Available query types.\n */\n private queryType?: 'SELECT' | 'INDEX';\n /**\n * INDEX ON Expression.\n */\n private indexOn?: IIndexOnParams[];\n /**\n * Types of supported index statements.\n */\n private indexType?: IndexType;\n /**\n * Index name.\n */\n private indexName?: string;\n /**\n * INDEX USING GSI Expression.\n */\n private indexUsingGSI?: boolean;\n /**\n * INDEX USING GSI Expression.\n */\n private indexWith?: IIndexWithParams;\n /**\n * @summary Create an instance of QueryBuilder.\n * @name QueryBuilder\n * @class\n * @public\n *\n * @param conditions List of SELECT clause conditions\n * @param collection Collection name\n * @returns QueryBuilder\n *\n * @example\n * ```ts\n * const query = new QueryBuilder({$select: [{$field: 'address'}], $where: {$nill: [{ address: { $like: '%57-59%' } }, { free_breakfast: true }, { free_lunch: [1] }]}}, 'travel-sample');\n * ```\n */\n constructor(conditions: IConditionExpr, collection: string) {\n super(conditions, collection);\n if (conditions) {\n this.compileFromConditions(conditions);\n }\n }\n\n /**\n * Add result selectors to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample`\n */\n select(value?: ISelectType[] | string | undefined): QueryBuilder {\n if (this.queryType === undefined || this.queryType === 'SELECT') {\n this.queryType = 'SELECT';\n if (!value) {\n this.selectExpr = '*';\n } else if (typeof value === 'string') {\n this.selectExpr = value;\n } else if (typeof value === 'object') {\n this.selectExpr = [...(this.selectExpr || []), ...value] as ISelectType[];\n }\n\n return this;\n }\n throw new MultipleQueryTypesException('SELECT', this.queryType);\n }\n\n /**\n * Add index type and name to INDEX clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const result = new QueryBuilder({}, 'travel-sample').index('DROP', 'travel_sample_id_test').build();\n * console.log(result)\n * ```\n * > DROP INDEX `travel-sample`.`travel_sample_id_test`\n */\n index(type: IndexType, name: string): QueryBuilder {\n if (this.queryType === undefined) {\n if (name.search(/^[A-Za-z][A-Za-z0-9#_]*$/g) === -1) {\n throw new Error(\n 'Valid GSI index names can contain any of the following characters: A-Z a-z 0-9 # _, and must start with a letter, [A-Z a-z]'\n );\n }\n this.queryType = 'INDEX';\n this.indexType = type;\n this.indexName = name;\n return this;\n }\n throw new MultipleQueryTypesException('INDEX', this.queryType);\n }\n\n /**\n * Add items to ON clause in INDEX clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const on = [{ name: 'travel-sample.callsing', sort: 'ASC' }];\n * const result = new QueryBuilder({}, 'travel-sample').index('CREATE', 'travel_sample_id_test').on(on).build();\n * console.log(result)\n * ```\n * > CREATE INDEX `travel_sample_id_test` ON `travel-sample`(`travel-sample.callsing`['ASC'])\n */\n on(value: IIndexOnParams[]): QueryBuilder {\n if (\n this.queryType === 'INDEX' &&\n ['CREATE', 'CREATE PRIMARY', 'BUILD'].includes(this.indexType || '')\n ) {\n this.indexOn = value;\n return this;\n }\n throw new IndexParamsOnExceptions(['CREATE', 'CREATE PRIMARY', 'BUILD']);\n }\n\n /**\n * Create INDEX using General Secondary Index(GSI).\n * @method\n * @public\n *\n * @example\n * ```ts\n * const result = new QueryBuilder({}, 'travel-sample').index('CREATE', 'travel_sample_id_test').usingGSI().build();\n * console.log(result)\n * ```\n * > CREATE INDEX `travel_sample_id_test` USING GSI)\n */\n usingGSI(): QueryBuilder {\n if (this.queryType === 'INDEX') {\n this.indexUsingGSI = true;\n return this;\n }\n throw new IndexParamsUsingGSIExceptions(['CREATE', 'CREATE PRIMARY', 'BUILD']);\n }\n\n /**\n * Add items to WITH clause in INDEX clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const withExpr = {nodes: ['192.168.1.1:8078'],defer_build: true,num_replica: 2};\n * const result = new QueryBuilder({}, 'travel-sample').index('CREATE', 'travel_sample_id_test').with(withExpr).build();\n * console.log(result)\n * ```\n * > CREATE INDEX `travel_sample_id_test` WITH {'nodes': ['192.168.1.1:8078'],'defer_build': true,'num_replica': 2})\n */\n with(value: IIndexWithParams): QueryBuilder {\n if (this.queryType === 'INDEX') {\n this.indexWith = value;\n return this;\n }\n throw new Error('The WITH clause is only available for Indexes');\n }\n\n /**\n * Add WHERE expression to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const expr_where = {$or: [{ address: { $like: '%57-59%' } }, { free_breakfast: true }]};\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).where(expr_where).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample WHERE (address LIKE '%57-59%' OR free_breakfast = true)`\n */\n where(value: LogicalWhereExpr): QueryBuilder {\n this.whereExpr = value;\n return this;\n }\n\n /**\n * Add JOIN expression to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```tS\n * const query = new QueryBuilder({}, 'beer-sample brewery');\n * const result = query.select([{$field: 'address'}]).plainJoin('JOIN `beer-sample` beer ON beer.brewery_id = LOWER(REPLACE(brewery.name, \" \", \"_\"))').build()\n * console.log(result)\n * ```\n * > SELECT address FROM `beer-sample brewery` JOIN `beer-sample` beer ON beer.brewery_id = LOWER(REPLACE(brewery.name, \" \", \"_\")) LIMIT 1`\n */\n plainJoin(value: string): QueryBuilder {\n this.plainJoinExpr = value;\n return this;\n }\n\n /**\n * Add ORDER BY expression to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).orderBy({ size: 'DESC' }).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample ORDER BY size = 'DESC'`\n */\n orderBy(value: Record<string, SortType>): QueryBuilder {\n this.orderExpr = value;\n return this;\n }\n\n /**\n * Add LIMIT expression to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).limit(10).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample LIMIT 10`\n */\n limit(value: number): QueryBuilder {\n this.limitExpr = value;\n return this;\n }\n\n /**\n * Add OFFSET expression to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).offset(10).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample OFFSET 10`\n */\n offset(value: number): QueryBuilder {\n this.offSetExpr = value;\n return this;\n }\n\n /**\n * Add LET expression to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const letExpr = [{ key: 'amount_val', value: 10 }];\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).let(letExpr).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample LET amount_val = 10`\n */\n let(value: ILetExpr[]): QueryBuilder {\n this.letExpr = value;\n return this;\n }\n\n /**\n * Add GROUP BY expression to GROUP BY clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).groupBy(groupByExpr).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample GROUP BY COUNT(amount) AS amount`\n */\n groupBy(value: IGroupBy[]): QueryBuilder {\n this.groupByExpr = value;\n return this;\n }\n\n /**\n * Add LETTING expression to GROUP BY clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];\n * const letExpr = [{ key: 'amount_val', value: 10 }];\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).groupBy(groupByExpr).let(letExpr).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample GROUP BY COUNT(amount) AS amount LETTING amount = 10`\n */\n letting(value: ILetExpr[]): QueryBuilder {\n this.lettingExpr = value;\n return this;\n }\n\n /**\n * Add HAVING expression to GROUP BY clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];\n * const having = {address: {$like: '%58%'}};\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).groupBy(groupByExpr).having(having).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample GROUP BY COUNT(amount) AS amount HAVING address LIKE '%58%'`\n */\n having(value: LogicalWhereExpr): QueryBuilder {\n this.havingExpr = value;\n return this;\n }\n\n /**\n * Add USE KEYS expression to SELECT clause.\n * @method\n * @public\n *\n * @example\n * ```ts\n * const query = new QueryBuilder({}, 'travel-sample');\n * const result = query.select([{$field: 'address'}]).useKeys(['airlineR_8093']).build()\n * console.log(result)\n * ```\n * > SELECT address FROM `travel-sample USE KEYS ['airlineR_8093']`\n */\n useKeys(value: string[]): QueryBuilder {\n this.useKeysExpr = value;\n return this;\n }\n\n /**\n * Converts the conditional parameters passed to the constructor to the properties of the N1QL QueryBuilder.\n * @method\n * @public\n *\n */\n compileFromConditions(conditionals: IConditionExpr): void {\n Object.keys(conditionals).forEach((value: string) => {\n switch (value) {\n case 'select':\n this.select(conditionals[value]);\n break;\n case 'let':\n !!conditionals[value] && this.let(conditionals[value] as ILetExpr[]);\n break;\n case 'where':\n !!conditionals[value] && this.where(conditionals[value] as LogicalWhereExpr);\n break;\n case 'plainJoin':\n !!conditionals[value] && this.plainJoin(conditionals[value] as string);\n break;\n case 'groupBy':\n !!conditionals[value] && this.groupBy(conditionals[value] as IGroupBy[]);\n break;\n case 'letting':\n !!conditionals[value] && this.letting(conditionals[value] as ILetExpr[]);\n break;\n case 'having':\n !!conditionals[value] && this.having(conditionals[value] as LogicalWhereExpr);\n break;\n case 'orderBy':\n !!conditionals[value] &&\n this.orderBy(conditionals[value] as Record<string, SortType>);\n break;\n case 'limit':\n !!conditionals[value] && this.limit(conditionals[value] as number);\n break;\n case 'offset':\n !!conditionals[value] && this.offset(conditionals[value] as number);\n break;\n case 'use':\n !!conditionals[value] && this.useKeys(conditionals[value] as string[]);\n break;\n }\n });\n }\n\n /**\n * Build a n1ql query from the defined parameters.\n * @method\n * @public\n *\n */\n build(): string {\n switch (this.queryType) {\n case 'INDEX':\n switch (this.indexType) {\n case 'BUILD':\n case 'CREATE':\n case 'CREATE PRIMARY':\n if (this.indexOn) {\n return buildIndexExpr(\n this.collection,\n this.indexType,\n this.indexName || '',\n this.indexOn,\n this.whereExpr,\n this.indexUsingGSI,\n this.indexWith\n );\n }\n case 'DROP':\n return buildIndexExpr(\n this.collection,\n this.indexType,\n this.indexName || '',\n undefined,\n undefined,\n this.indexUsingGSI\n );\n }\n\n case 'SELECT':\n if (this.selectExpr) {\n return selectBuilder(\n this.collection,\n this.selectExpr,\n this.letExpr,\n this.whereExpr,\n this.orderExpr,\n this.limitExpr,\n this.offSetExpr,\n this.useKeysExpr,\n this.groupByExpr,\n this.lettingExpr,\n this.havingExpr,\n this.plainJoinExpr\n );\n }\n }\n return '';\n }\n\n get conditions(): IConditionExpr {\n return this._conditions;\n }\n\n set conditions(value: IConditionExpr) {\n this._conditions = value;\n }\n\n get collection(): string {\n return this._collection;\n }\n\n set collection(value: string) {\n this._collection = value;\n }\n}\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.escapeReservedWords = exports.parseStringSelectExpr = void 0;
|
|
4
|
+
var helpers_1 = require("./helpers");
|
|
5
|
+
var replaceList = ['ALL', 'DISTINCT', 'RAW', 'ELEMENT', 'VALUE'];
|
|
6
|
+
/**
|
|
7
|
+
* Convert select expression into an Array of selection keys
|
|
8
|
+
* */
|
|
9
|
+
var parseStringSelectExpr = function (expr) {
|
|
10
|
+
if (expr.indexOf(',') === -1 && expr.indexOf(' as ') === -1) {
|
|
11
|
+
return [expr];
|
|
12
|
+
}
|
|
13
|
+
var resultExpr = expr.replace(/[()]/g, '');
|
|
14
|
+
replaceList.forEach(function (value) {
|
|
15
|
+
resultExpr = resultExpr.replace(new RegExp("/[" + value + "]/", 'g'), '');
|
|
16
|
+
});
|
|
17
|
+
return resultExpr.split(',').map(function (v) {
|
|
18
|
+
return extractAsValue(v);
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
exports.parseStringSelectExpr = parseStringSelectExpr;
|
|
22
|
+
/**
|
|
23
|
+
* @ignore
|
|
24
|
+
* */
|
|
25
|
+
var extractAsValue = function (expr) {
|
|
26
|
+
var result = expr.toLowerCase().split(' as ');
|
|
27
|
+
if (result.length === 2) {
|
|
28
|
+
return result[1].trim();
|
|
29
|
+
}
|
|
30
|
+
return expr.trim();
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* @ignore
|
|
34
|
+
*/
|
|
35
|
+
var escapeReservedWords = function (field) {
|
|
36
|
+
if (helpers_1.n1qlReservedWords.includes(field.toUpperCase())) {
|
|
37
|
+
return "`" + field + "`";
|
|
38
|
+
}
|
|
39
|
+
if (field.match(/(\-)|(\.)|(\[\d+\])/g)) {
|
|
40
|
+
var expr = field;
|
|
41
|
+
expr = expr
|
|
42
|
+
.split('.')
|
|
43
|
+
.map(function (value) {
|
|
44
|
+
if (helpers_1.n1qlReservedWords.includes(value.toUpperCase())) {
|
|
45
|
+
return "`" + value + "`";
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
})
|
|
49
|
+
.join('.');
|
|
50
|
+
expr = expr.replace(/([a-z0-9]*\-[a-z0-9]*)/g, '`$&`');
|
|
51
|
+
return expr;
|
|
52
|
+
}
|
|
53
|
+
return field;
|
|
54
|
+
};
|
|
55
|
+
exports.escapeReservedWords = escapeReservedWords;
|
|
56
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/query/utils.ts"],"names":[],"mappings":";;;AAAA,qCAA4C;AAE5C,IAAM,WAAW,GAAG,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,SAA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AC,EAAE;QACrC,IAAI,IAAI,GAAG,KAAK,CAAC;QACjB,IAAI,GAAG,IAAI;aACN,KAAK,CAAC,GAAG,CAAC;aACV,GAAG,CAAC,UAAC,KAAK;YACP,IAAI,2BAAiB,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,EAAE;gBACjD,OAAO,MAAK,KAAK,MAAI,CAAC;aACzB;YACD,OAAO,KAAK,CAAC;QACjB,CAAC,CAAC;aACD,IAAI,CAAC,GAAG,CAAC,CAAC;QACf,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;QACvD,OAAO,IAAI,CAAC;KACf;IACD,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC;AAnBW,QAAA,mBAAmB,uBAmB9B","sourcesContent":["import {n1qlReservedWords} from './helpers';\n\nconst replaceList = ['ALL', 'DISTINCT', 'RAW', 'ELEMENT', 'VALUE'];\n\n/**\n * Convert select expression into an Array of selection keys\n * */\nexport const parseStringSelectExpr = (expr: string): string[] => {\n if (expr.indexOf(',') === -1 && expr.indexOf(' as ') === -1) {\n return [expr];\n }\n let resultExpr = expr.replace(/[()]/g, '');\n replaceList.forEach((value: string) => {\n resultExpr = resultExpr.replace(new RegExp(`/[${value}]/`, 'g'), '');\n });\n return resultExpr.split(',').map((v: string) => {\n return extractAsValue(v);\n });\n};\n\n/**\n * @ignore\n * */\nconst extractAsValue = (expr: string): string => {\n const result = expr.toLowerCase().split(' as ');\n if (result.length === 2) {\n return result[1].trim();\n }\n return expr.trim();\n};\n\n/**\n * @ignore\n */\nexport const escapeReservedWords = (field: string) => {\n if (n1qlReservedWords.includes(field.toUpperCase())) {\n return `\\`${field}\\``;\n }\n if (field.match(/(\\-)|(\\.)|(\\[\\d+\\])/g)) {\n let expr = field;\n expr = expr\n .split('.')\n .map((value) => {\n if (n1qlReservedWords.includes(value.toUpperCase())) {\n return `\\`${value}\\``;\n }\n return value;\n })\n .join('.');\n expr = expr.replace(/([a-z0-9]*\\-[a-z0-9]*)/g, '`$&`');\n return expr;\n }\n return field;\n};\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface CustomQueryArgs {
|
|
2
|
+
query: any;
|
|
3
|
+
limit: number;
|
|
4
|
+
params: any;
|
|
5
|
+
}
|
|
6
|
+
export interface CustomQueryPagination {
|
|
7
|
+
hasNext: boolean;
|
|
8
|
+
params: any;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Common pagination
|
|
12
|
+
* query,
|
|
13
|
+
bucketName = "",
|
|
14
|
+
select = ["id", "owner"] || "*"
|
|
15
|
+
* @param args PaginationArgs
|
|
16
|
+
*/
|
|
17
|
+
export declare const CustomQuery: <T>(args: CustomQueryArgs) => Promise<[T[], CustomQueryPagination]>;
|