laravel-query-builder-js 1.0.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/LICENSE +21 -0
- package/README.md +388 -0
- package/dist/index.cjs +676 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +143 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.js +672 -0
- package/dist/index.js.map +1 -0
- package/package.json +74 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lodash = require('lodash');
|
|
4
|
+
|
|
5
|
+
var __create = Object.create;
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
8
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __esm = (fn, res) => function __init() {
|
|
12
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
13
|
+
};
|
|
14
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
15
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
+
};
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
+
__defProp(target, "default", { value: mod, enumerable: true }) ,
|
|
35
|
+
mod
|
|
36
|
+
));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
|
|
39
|
+
// node_modules/map-obj/index.js
|
|
40
|
+
var require_map_obj = __commonJS({
|
|
41
|
+
"node_modules/map-obj/index.js"(exports, module) {
|
|
42
|
+
var isObject = (value) => typeof value === "object" && value !== null;
|
|
43
|
+
var mapObjectSkip = /* @__PURE__ */ Symbol("skip");
|
|
44
|
+
var isObjectCustom = (value) => isObject(value) && !(value instanceof RegExp) && !(value instanceof Error) && !(value instanceof Date);
|
|
45
|
+
var mapObject = (object, mapper, options, isSeen = /* @__PURE__ */ new WeakMap()) => {
|
|
46
|
+
options = {
|
|
47
|
+
deep: false,
|
|
48
|
+
target: {},
|
|
49
|
+
...options
|
|
50
|
+
};
|
|
51
|
+
if (isSeen.has(object)) {
|
|
52
|
+
return isSeen.get(object);
|
|
53
|
+
}
|
|
54
|
+
isSeen.set(object, options.target);
|
|
55
|
+
const { target } = options;
|
|
56
|
+
delete options.target;
|
|
57
|
+
const mapArray = (array) => array.map((element) => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element);
|
|
58
|
+
if (Array.isArray(object)) {
|
|
59
|
+
return mapArray(object);
|
|
60
|
+
}
|
|
61
|
+
for (const [key, value] of Object.entries(object)) {
|
|
62
|
+
const mapResult = mapper(key, value, object);
|
|
63
|
+
if (mapResult === mapObjectSkip) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
let [newKey, newValue, { shouldRecurse = true } = {}] = mapResult;
|
|
67
|
+
if (newKey === "__proto__") {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
|
|
71
|
+
newValue = Array.isArray(newValue) ? mapArray(newValue) : mapObject(newValue, mapper, options, isSeen);
|
|
72
|
+
}
|
|
73
|
+
target[newKey] = newValue;
|
|
74
|
+
}
|
|
75
|
+
return target;
|
|
76
|
+
};
|
|
77
|
+
module.exports = (object, mapper, options) => {
|
|
78
|
+
if (!isObject(object)) {
|
|
79
|
+
throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
|
|
80
|
+
}
|
|
81
|
+
return mapObject(object, mapper, options);
|
|
82
|
+
};
|
|
83
|
+
module.exports.mapObjectSkip = mapObjectSkip;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// node_modules/tslib/tslib.es6.mjs
|
|
88
|
+
var __assign;
|
|
89
|
+
var init_tslib_es6 = __esm({
|
|
90
|
+
"node_modules/tslib/tslib.es6.mjs"() {
|
|
91
|
+
__assign = function() {
|
|
92
|
+
__assign = Object.assign || function __assign2(t) {
|
|
93
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
94
|
+
s = arguments[i];
|
|
95
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
96
|
+
}
|
|
97
|
+
return t;
|
|
98
|
+
};
|
|
99
|
+
return __assign.apply(this, arguments);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// node_modules/lower-case/dist.es2015/index.js
|
|
105
|
+
function lowerCase(str) {
|
|
106
|
+
return str.toLowerCase();
|
|
107
|
+
}
|
|
108
|
+
var init_dist = __esm({
|
|
109
|
+
"node_modules/lower-case/dist.es2015/index.js"() {
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// node_modules/no-case/dist.es2015/index.js
|
|
114
|
+
function noCase(input, options) {
|
|
115
|
+
if (options === void 0) {
|
|
116
|
+
options = {};
|
|
117
|
+
}
|
|
118
|
+
var _a = options.splitRegexp, splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a, _b = options.stripRegexp, stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b, _c = options.transform, transform = _c === void 0 ? lowerCase : _c, _d = options.delimiter, delimiter = _d === void 0 ? " " : _d;
|
|
119
|
+
var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
|
|
120
|
+
var start = 0;
|
|
121
|
+
var end = result.length;
|
|
122
|
+
while (result.charAt(start) === "\0")
|
|
123
|
+
start++;
|
|
124
|
+
while (result.charAt(end - 1) === "\0")
|
|
125
|
+
end--;
|
|
126
|
+
return result.slice(start, end).split("\0").map(transform).join(delimiter);
|
|
127
|
+
}
|
|
128
|
+
function replace(input, re, value) {
|
|
129
|
+
if (re instanceof RegExp)
|
|
130
|
+
return input.replace(re, value);
|
|
131
|
+
return re.reduce(function(input2, re2) {
|
|
132
|
+
return input2.replace(re2, value);
|
|
133
|
+
}, input);
|
|
134
|
+
}
|
|
135
|
+
var DEFAULT_SPLIT_REGEXP, DEFAULT_STRIP_REGEXP;
|
|
136
|
+
var init_dist2 = __esm({
|
|
137
|
+
"node_modules/no-case/dist.es2015/index.js"() {
|
|
138
|
+
init_dist();
|
|
139
|
+
DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
|
|
140
|
+
DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// node_modules/dot-case/dist.es2015/index.js
|
|
145
|
+
function dotCase(input, options) {
|
|
146
|
+
if (options === void 0) {
|
|
147
|
+
options = {};
|
|
148
|
+
}
|
|
149
|
+
return noCase(input, __assign({ delimiter: "." }, options));
|
|
150
|
+
}
|
|
151
|
+
var init_dist3 = __esm({
|
|
152
|
+
"node_modules/dot-case/dist.es2015/index.js"() {
|
|
153
|
+
init_tslib_es6();
|
|
154
|
+
init_dist2();
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// node_modules/snake-case/dist.es2015/index.js
|
|
159
|
+
var dist_exports = {};
|
|
160
|
+
__export(dist_exports, {
|
|
161
|
+
snakeCase: () => snakeCase2
|
|
162
|
+
});
|
|
163
|
+
function snakeCase2(input, options) {
|
|
164
|
+
if (options === void 0) {
|
|
165
|
+
options = {};
|
|
166
|
+
}
|
|
167
|
+
return dotCase(input, __assign({ delimiter: "_" }, options));
|
|
168
|
+
}
|
|
169
|
+
var init_dist4 = __esm({
|
|
170
|
+
"node_modules/snake-case/dist.es2015/index.js"() {
|
|
171
|
+
init_tslib_es6();
|
|
172
|
+
init_dist3();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// node_modules/snakecase-keys/index.js
|
|
177
|
+
var require_snakecase_keys = __commonJS({
|
|
178
|
+
"node_modules/snakecase-keys/index.js"(exports, module) {
|
|
179
|
+
var map3 = require_map_obj();
|
|
180
|
+
var { snakeCase: snakeCase3 } = (init_dist4(), __toCommonJS(dist_exports));
|
|
181
|
+
var PlainObjectConstructor = {}.constructor;
|
|
182
|
+
module.exports = function(obj, options) {
|
|
183
|
+
if (Array.isArray(obj)) {
|
|
184
|
+
if (obj.some((item) => item.constructor !== PlainObjectConstructor)) {
|
|
185
|
+
throw new Error("obj must be array of plain objects");
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
if (obj.constructor !== PlainObjectConstructor) {
|
|
189
|
+
throw new Error("obj must be an plain object");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
options = Object.assign({ deep: true, exclude: [], parsingOptions: {} }, options);
|
|
193
|
+
const convertCase = options.snakeCase || ((key) => snakeCase3(key, options.parsingOptions));
|
|
194
|
+
return map3(obj, function(key, val) {
|
|
195
|
+
return [
|
|
196
|
+
matches(options.exclude, key) ? key : convertCase(key),
|
|
197
|
+
val,
|
|
198
|
+
mapperOptions(key, val, options)
|
|
199
|
+
];
|
|
200
|
+
}, options);
|
|
201
|
+
};
|
|
202
|
+
function matches(patterns, value) {
|
|
203
|
+
return patterns.some(function(pattern) {
|
|
204
|
+
return typeof pattern === "string" ? pattern === value : pattern.test(value);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
function mapperOptions(key, val, options) {
|
|
208
|
+
return options.shouldRecurse ? { shouldRecurse: options.shouldRecurse(key, val) } : void 0;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
var BaseQueryBuilder = class _BaseQueryBuilder {
|
|
213
|
+
constructor(name = "") {
|
|
214
|
+
this.query = {
|
|
215
|
+
name: "",
|
|
216
|
+
select: [],
|
|
217
|
+
filter: {},
|
|
218
|
+
sort: {},
|
|
219
|
+
include: [],
|
|
220
|
+
page: null,
|
|
221
|
+
limit: null
|
|
222
|
+
};
|
|
223
|
+
this.query.name = name;
|
|
224
|
+
}
|
|
225
|
+
select(fields) {
|
|
226
|
+
if (typeof fields === "string") {
|
|
227
|
+
return this.select(lodash.split(fields, ","));
|
|
228
|
+
}
|
|
229
|
+
this.query.select = fields.map((str) => lodash.snakeCase(str));
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
resetSelect() {
|
|
233
|
+
this.query.select = [];
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
addSelect(fields) {
|
|
237
|
+
if (typeof fields === "string") {
|
|
238
|
+
this.addSelect(lodash.split(fields, ","));
|
|
239
|
+
}
|
|
240
|
+
if (Array.isArray(fields)) {
|
|
241
|
+
this.query.select.push(...fields);
|
|
242
|
+
}
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
clearClauses(condition, field, op) {
|
|
246
|
+
let index = -1;
|
|
247
|
+
if (typeof field === "string") {
|
|
248
|
+
index = lodash.get(this.query, `filter.${condition}`, []).findIndex(
|
|
249
|
+
(f) => {
|
|
250
|
+
if (!op) {
|
|
251
|
+
return lodash.get(f, "field") === field;
|
|
252
|
+
}
|
|
253
|
+
return lodash.get(f, "field") === field && lodash.get(f, "op") === op;
|
|
254
|
+
}
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
if (Array.isArray(field)) {
|
|
258
|
+
index = lodash.get(this.query, `filter.${condition}`, []).findIndex(
|
|
259
|
+
(item) => Array.isArray(item.field) && item.field.length === field.length && item.field.every((_field, i) => _field == field[i])
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
if (index !== -1) {
|
|
263
|
+
lodash.pullAt(this.query.filter[condition], index);
|
|
264
|
+
if (lodash.isEmpty(this.query.filter[condition])) delete this.query.filter[condition];
|
|
265
|
+
}
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
clearOrWhere(field, op) {
|
|
269
|
+
if (lodash.isFunction(field) && typeof field === "function") {
|
|
270
|
+
const nestedBuilder = new _BaseQueryBuilder(this.query.name);
|
|
271
|
+
field(nestedBuilder);
|
|
272
|
+
const nestedFilter = nestedBuilder.getQuery().filter;
|
|
273
|
+
this.removeFunctionBaseWhereClauses(this.query.filter, nestedFilter);
|
|
274
|
+
} else this.clearClauses("or", field, op);
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
removeFunctionBaseWhereClauses(baseFilter, nestedFilter) {
|
|
278
|
+
const areFiltersEqual = (filter1, filter2) => {
|
|
279
|
+
if (filter1.field !== filter2.field || filter1.op !== filter2.op) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
if (filter1.value && filter2.value) {
|
|
283
|
+
if (Array.isArray(filter1.value) && Array.isArray(filter2.value)) {
|
|
284
|
+
if (filter1.value.length !== filter2.value.length) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
for (let i = 0; i < filter1.value.length; i++) {
|
|
288
|
+
if (!areFiltersEqual(filter1.value[i], filter2.value[i])) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
} else if (JSON.stringify(filter1.value) !== JSON.stringify(filter2.value)) {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
};
|
|
298
|
+
const iterateAndRemove = (filters, filterToRemove) => {
|
|
299
|
+
return filters.filter((filter) => {
|
|
300
|
+
if (areFiltersEqual(filter, filterToRemove)) {
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
if (filter.and) {
|
|
304
|
+
filter.and = iterateAndRemove(filter.and, filterToRemove);
|
|
305
|
+
}
|
|
306
|
+
if (filter.or) {
|
|
307
|
+
filter.or = iterateAndRemove(filter.or, filterToRemove);
|
|
308
|
+
}
|
|
309
|
+
return true;
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
if (baseFilter.and) {
|
|
313
|
+
baseFilter.and = iterateAndRemove(baseFilter.and, nestedFilter);
|
|
314
|
+
if (lodash.isEmpty(baseFilter.and)) delete baseFilter.and;
|
|
315
|
+
}
|
|
316
|
+
if (baseFilter.or) {
|
|
317
|
+
baseFilter.or = iterateAndRemove(baseFilter.or, nestedFilter);
|
|
318
|
+
if (lodash.isEmpty(baseFilter.or)) delete baseFilter.or;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
clearWhere(field, op) {
|
|
322
|
+
if (lodash.isFunction(field) && typeof field === "function") {
|
|
323
|
+
const nestedBuilder = new _BaseQueryBuilder(this.query.name);
|
|
324
|
+
field(nestedBuilder);
|
|
325
|
+
const nestedFilter = nestedBuilder.getQuery().filter;
|
|
326
|
+
this.removeFunctionBaseWhereClauses(this.query.filter, nestedFilter);
|
|
327
|
+
} else {
|
|
328
|
+
this.clearClauses("and", field, op);
|
|
329
|
+
}
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
addFilter(condition, filter) {
|
|
333
|
+
const _filter = filter;
|
|
334
|
+
if (lodash.get(_filter, "field") && lodash.get(_filter, "op")) {
|
|
335
|
+
this.clearClauses(condition, _filter.field, _filter.op);
|
|
336
|
+
}
|
|
337
|
+
if (!lodash.has(this.query.filter, condition)) {
|
|
338
|
+
lodash.set(this.query.filter, condition, []);
|
|
339
|
+
}
|
|
340
|
+
this.query.filter[condition].push(filter);
|
|
341
|
+
return this;
|
|
342
|
+
}
|
|
343
|
+
filter(filter) {
|
|
344
|
+
this.query.filter = filter;
|
|
345
|
+
return this;
|
|
346
|
+
}
|
|
347
|
+
resetFilter() {
|
|
348
|
+
this.query.filter = {};
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
whereNotNull(field) {
|
|
352
|
+
return this.where(field, "not_null");
|
|
353
|
+
}
|
|
354
|
+
whereNull(field) {
|
|
355
|
+
return this.where(field, "null");
|
|
356
|
+
}
|
|
357
|
+
where(field, op, value) {
|
|
358
|
+
if (lodash.isFunction(value) && typeof value === "function" && !lodash.isFunction(field) && typeof field != "function") {
|
|
359
|
+
const nestedBuilder = new _BaseQueryBuilder(this.query.name);
|
|
360
|
+
value(nestedBuilder);
|
|
361
|
+
const nestedFilter = nestedBuilder.getQuery().filter;
|
|
362
|
+
return this.addFilter("and", { field, op, value: nestedFilter });
|
|
363
|
+
}
|
|
364
|
+
if (lodash.isFunction(field) && typeof field === "function") {
|
|
365
|
+
const nestedBuilder = new _BaseQueryBuilder(this.query.name);
|
|
366
|
+
field(nestedBuilder);
|
|
367
|
+
const nestedFilter = nestedBuilder.getQuery().filter;
|
|
368
|
+
return this.addFilter("and", nestedFilter);
|
|
369
|
+
}
|
|
370
|
+
if ((typeof field === "string" || Array.isArray(field)) && op !== void 0 && value !== void 0) {
|
|
371
|
+
const _value = value;
|
|
372
|
+
return this.addFilter("and", { field, op, value: _value });
|
|
373
|
+
}
|
|
374
|
+
if (typeof field === "string" && op !== void 0 && value === void 0) {
|
|
375
|
+
return this.addFilter("and", { field, op });
|
|
376
|
+
}
|
|
377
|
+
return this;
|
|
378
|
+
}
|
|
379
|
+
whereIn(field, value) {
|
|
380
|
+
return this.where(field, "in", value);
|
|
381
|
+
}
|
|
382
|
+
orWhere(field, op, value) {
|
|
383
|
+
if (lodash.isFunction(value) && typeof value === "function" && !lodash.isFunction(field) && typeof field != "function") {
|
|
384
|
+
const nestedBuilder = new _BaseQueryBuilder(this.query.name);
|
|
385
|
+
value(nestedBuilder);
|
|
386
|
+
const nestedFilter = nestedBuilder.getQuery().filter;
|
|
387
|
+
return this.addFilter("or", { field, op, value: nestedFilter });
|
|
388
|
+
}
|
|
389
|
+
if (lodash.isFunction(field) && typeof field === "function") {
|
|
390
|
+
const nestedBuilder = new _BaseQueryBuilder(this.query.name);
|
|
391
|
+
field(nestedBuilder);
|
|
392
|
+
const nestedFilter = nestedBuilder.getQuery().filter;
|
|
393
|
+
return this.addFilter("or", nestedFilter);
|
|
394
|
+
}
|
|
395
|
+
if ((typeof field === "string" || Array.isArray(field)) && op !== void 0 && value !== void 0) {
|
|
396
|
+
const _value = value;
|
|
397
|
+
return this.addFilter("or", { field, op, value: _value });
|
|
398
|
+
}
|
|
399
|
+
if (typeof field === "string" && op !== void 0 && value === void 0) {
|
|
400
|
+
return this.addFilter("or", { field, op });
|
|
401
|
+
}
|
|
402
|
+
if (Array.isArray(field)) {
|
|
403
|
+
field.forEach((singleField) => this.orWhere(singleField, op, value));
|
|
404
|
+
return this;
|
|
405
|
+
}
|
|
406
|
+
return this;
|
|
407
|
+
}
|
|
408
|
+
sort(sort) {
|
|
409
|
+
this.query.sort = sort;
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
addSort(sort) {
|
|
413
|
+
this.query.sort = lodash.merge({}, this.query.sort, sort);
|
|
414
|
+
return this;
|
|
415
|
+
}
|
|
416
|
+
resetSort() {
|
|
417
|
+
this.query.sort = {};
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
addInclude(include) {
|
|
421
|
+
if (!Array.isArray(include)) {
|
|
422
|
+
return this.addInclude([include]);
|
|
423
|
+
}
|
|
424
|
+
this.query.include.push(...include);
|
|
425
|
+
return this;
|
|
426
|
+
}
|
|
427
|
+
include(include) {
|
|
428
|
+
if (!Array.isArray(include)) {
|
|
429
|
+
return this.include([include]);
|
|
430
|
+
}
|
|
431
|
+
this.query.include = include;
|
|
432
|
+
return this;
|
|
433
|
+
}
|
|
434
|
+
resetInclude() {
|
|
435
|
+
this.query.include = [];
|
|
436
|
+
return this;
|
|
437
|
+
}
|
|
438
|
+
paginate(page = 1, limit = 15) {
|
|
439
|
+
this.fromPage(page);
|
|
440
|
+
this.query.limit = limit;
|
|
441
|
+
return this;
|
|
442
|
+
}
|
|
443
|
+
fromPage(page = 1) {
|
|
444
|
+
this.query.page = page;
|
|
445
|
+
return this;
|
|
446
|
+
}
|
|
447
|
+
resetQuery() {
|
|
448
|
+
this.resetSelect();
|
|
449
|
+
this.resetFilter();
|
|
450
|
+
this.resetSort();
|
|
451
|
+
this.resetInclude();
|
|
452
|
+
this.paginate();
|
|
453
|
+
return this;
|
|
454
|
+
}
|
|
455
|
+
addQueryBuilder(queryBuilder) {
|
|
456
|
+
const otherQuery = queryBuilder.getQuery();
|
|
457
|
+
this.query.select = [.../* @__PURE__ */ new Set([...this.query.select, ...otherQuery.select])];
|
|
458
|
+
this.query.filter = lodash.mergeWith(this.query.filter, otherQuery.filter, (objValue, srcValue) => {
|
|
459
|
+
if (Array.isArray(objValue)) {
|
|
460
|
+
return objValue.concat(srcValue);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
this.query.sort = { ...this.query.sort, ...otherQuery.sort };
|
|
464
|
+
this.query.include = [.../* @__PURE__ */ new Set([...this.query.include, ...otherQuery.include])];
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
467
|
+
getQuery() {
|
|
468
|
+
return this.query;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
// src/include-query-builder.ts
|
|
473
|
+
var import_snakecase_keys = __toESM(require_snakecase_keys());
|
|
474
|
+
var withRelation = class _withRelation extends BaseQueryBuilder {
|
|
475
|
+
constructor(name) {
|
|
476
|
+
super(name);
|
|
477
|
+
this.query = {
|
|
478
|
+
name: "",
|
|
479
|
+
select: [],
|
|
480
|
+
aggregate: "",
|
|
481
|
+
filter: {},
|
|
482
|
+
sort: {},
|
|
483
|
+
include: []
|
|
484
|
+
};
|
|
485
|
+
this.query.name = name;
|
|
486
|
+
}
|
|
487
|
+
static create(name) {
|
|
488
|
+
return new _withRelation(name);
|
|
489
|
+
}
|
|
490
|
+
static new(name) {
|
|
491
|
+
return _withRelation.create(name);
|
|
492
|
+
}
|
|
493
|
+
aggregate(value) {
|
|
494
|
+
this.query.aggregate = value;
|
|
495
|
+
return this;
|
|
496
|
+
}
|
|
497
|
+
new(name) {
|
|
498
|
+
return _withRelation.create(name);
|
|
499
|
+
}
|
|
500
|
+
getQuery() {
|
|
501
|
+
return this.query;
|
|
502
|
+
}
|
|
503
|
+
clone() {
|
|
504
|
+
const clone = new _withRelation(this.query.name);
|
|
505
|
+
clone.query = { ...this.query };
|
|
506
|
+
return clone;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Serialise this relation (and any nested relations) into the plain
|
|
510
|
+
* object shape the backend expects.
|
|
511
|
+
*/
|
|
512
|
+
toQueryObject() {
|
|
513
|
+
const query = {};
|
|
514
|
+
query.name = this.query.name;
|
|
515
|
+
if (!lodash.isEmpty(this.query.select)) {
|
|
516
|
+
query.select = this.query.select;
|
|
517
|
+
}
|
|
518
|
+
if (!lodash.isEmpty(this.query.filter)) {
|
|
519
|
+
query.filter = this.query.filter;
|
|
520
|
+
}
|
|
521
|
+
if (!lodash.isEmpty(this.query.sort)) {
|
|
522
|
+
query.sort = (0, import_snakecase_keys.default)(this.query.sort);
|
|
523
|
+
}
|
|
524
|
+
if (!lodash.isEmpty(this.query.aggregate)) {
|
|
525
|
+
query.aggregate = this.query.aggregate;
|
|
526
|
+
}
|
|
527
|
+
if (!lodash.isEmpty(this.query.include)) {
|
|
528
|
+
query.include = lodash.map(this.query.include, (_include) => {
|
|
529
|
+
if (_include instanceof _withRelation) {
|
|
530
|
+
return _include.toQueryObject();
|
|
531
|
+
}
|
|
532
|
+
return _include;
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
return query;
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
// src/query-builder.ts
|
|
540
|
+
var import_snakecase_keys2 = __toESM(require_snakecase_keys());
|
|
541
|
+
var QueryBuilder = class _QueryBuilder extends BaseQueryBuilder {
|
|
542
|
+
constructor(name) {
|
|
543
|
+
super(name);
|
|
544
|
+
this.query = {
|
|
545
|
+
name: "",
|
|
546
|
+
select: [],
|
|
547
|
+
filter: {},
|
|
548
|
+
sort: {},
|
|
549
|
+
include: [],
|
|
550
|
+
page: null,
|
|
551
|
+
limit: null
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
get limit() {
|
|
555
|
+
return this.query.limit;
|
|
556
|
+
}
|
|
557
|
+
set limit(value) {
|
|
558
|
+
this.query.limit = value;
|
|
559
|
+
}
|
|
560
|
+
get page() {
|
|
561
|
+
return this.query.page;
|
|
562
|
+
}
|
|
563
|
+
set page(value) {
|
|
564
|
+
this.query.page = value;
|
|
565
|
+
}
|
|
566
|
+
static create(name) {
|
|
567
|
+
return new _QueryBuilder(name);
|
|
568
|
+
}
|
|
569
|
+
static new(name) {
|
|
570
|
+
return _QueryBuilder.create(name);
|
|
571
|
+
}
|
|
572
|
+
new(name) {
|
|
573
|
+
return _QueryBuilder.create(name);
|
|
574
|
+
}
|
|
575
|
+
static withRelation(name) {
|
|
576
|
+
return withRelation.create(name);
|
|
577
|
+
}
|
|
578
|
+
withRelation(name) {
|
|
579
|
+
return withRelation.create(name);
|
|
580
|
+
}
|
|
581
|
+
getQuery() {
|
|
582
|
+
return this.query;
|
|
583
|
+
}
|
|
584
|
+
clone() {
|
|
585
|
+
const clone = new _QueryBuilder();
|
|
586
|
+
clone.query = lodash.cloneDeep(this.query);
|
|
587
|
+
return clone;
|
|
588
|
+
}
|
|
589
|
+
nextPage() {
|
|
590
|
+
this.query.page = (this.query.page || 1) + 1;
|
|
591
|
+
return this;
|
|
592
|
+
}
|
|
593
|
+
previousPage() {
|
|
594
|
+
this.query.page = (this.query.page || 2) - 1;
|
|
595
|
+
return this;
|
|
596
|
+
}
|
|
597
|
+
resetPage() {
|
|
598
|
+
this.query.page = 1;
|
|
599
|
+
return this;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Serialise into a `Record<string, string>` of query params. Filter,
|
|
603
|
+
* select, sort and include values are JSON-stringified.
|
|
604
|
+
*/
|
|
605
|
+
toParams() {
|
|
606
|
+
const query = {};
|
|
607
|
+
if (!lodash.isEmpty(this.query.select)) {
|
|
608
|
+
query.select = JSON.stringify(this.query.select);
|
|
609
|
+
}
|
|
610
|
+
if (!lodash.isEmpty(this.query.filter)) {
|
|
611
|
+
query.filter = JSON.stringify(this.query.filter);
|
|
612
|
+
}
|
|
613
|
+
if (!lodash.isEmpty(this.query.sort)) {
|
|
614
|
+
query.sort = JSON.stringify((0, import_snakecase_keys2.default)(this.query.sort));
|
|
615
|
+
}
|
|
616
|
+
if (!lodash.isEmpty(this.query.sort)) {
|
|
617
|
+
query.aggregate = JSON.stringify(this.query);
|
|
618
|
+
}
|
|
619
|
+
if (!lodash.isEmpty(this.query.include)) {
|
|
620
|
+
const include = lodash.map(this.query.include, (_include) => {
|
|
621
|
+
if (_include instanceof withRelation) {
|
|
622
|
+
return _include.toQueryObject();
|
|
623
|
+
}
|
|
624
|
+
return _include;
|
|
625
|
+
});
|
|
626
|
+
query.include = JSON.stringify(include);
|
|
627
|
+
}
|
|
628
|
+
if (this.query.page) {
|
|
629
|
+
query.page = this.query.page;
|
|
630
|
+
}
|
|
631
|
+
if (this.query.limit) {
|
|
632
|
+
query.limit = this.query.limit;
|
|
633
|
+
}
|
|
634
|
+
return query;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Serialise into a URL-encoded query string, e.g.
|
|
638
|
+
* `filter=...&sort=...&page=1&limit=25`.
|
|
639
|
+
*/
|
|
640
|
+
toQueryParams() {
|
|
641
|
+
const queryParams = [];
|
|
642
|
+
if (!lodash.isEmpty(this.query.select)) {
|
|
643
|
+
queryParams.push(`select=${encodeURIComponent(JSON.stringify(this.query.select))}`);
|
|
644
|
+
}
|
|
645
|
+
if (!lodash.isEmpty(this.query.filter)) {
|
|
646
|
+
queryParams.push(`filter=${encodeURIComponent(JSON.stringify(this.query.filter))}`);
|
|
647
|
+
}
|
|
648
|
+
if (!lodash.isEmpty(this.query.sort)) {
|
|
649
|
+
queryParams.push(
|
|
650
|
+
`sort=${encodeURIComponent(JSON.stringify((0, import_snakecase_keys2.default)(this.query.sort)))}`
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
if (!lodash.isEmpty(this.query.include)) {
|
|
654
|
+
const include = lodash.map(this.query.include, (_include) => {
|
|
655
|
+
if (_include instanceof withRelation) {
|
|
656
|
+
return _include.toQueryObject();
|
|
657
|
+
}
|
|
658
|
+
return _include;
|
|
659
|
+
});
|
|
660
|
+
queryParams.push(`include=${encodeURIComponent(JSON.stringify(include))}`);
|
|
661
|
+
}
|
|
662
|
+
if (this.query.page) {
|
|
663
|
+
queryParams.push(`page=${this.query.page}`);
|
|
664
|
+
}
|
|
665
|
+
if (this.query.limit) {
|
|
666
|
+
queryParams.push(`limit=${this.query.limit}`);
|
|
667
|
+
}
|
|
668
|
+
return queryParams.join("&");
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
exports.BaseQueryBuilder = BaseQueryBuilder;
|
|
673
|
+
exports.QueryBuilder = QueryBuilder;
|
|
674
|
+
exports.withRelation = withRelation;
|
|
675
|
+
//# sourceMappingURL=index.cjs.map
|
|
676
|
+
//# sourceMappingURL=index.cjs.map
|