jcc-express-mvc 1.7.2 → 1.7.3
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/Jcc-eloquent/lib/Builder.js +767 -742
- package/package.json +1 -1
|
@@ -4,748 +4,773 @@ exports.Builder = void 0;
|
|
|
4
4
|
const Database_1 = require("./Database");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
6
|
class Builder {
|
|
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
|
-
from(tableName)
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
applyJoin(
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
7
|
+
constructor() {
|
|
8
|
+
this.tableName = "";
|
|
9
|
+
this.queryBuilder = null;
|
|
10
|
+
this.model = null;
|
|
11
|
+
this.eagerLoad = [];
|
|
12
|
+
this.softDelete = true; // Default to true (filtering out deleted), set to false to include deleted
|
|
13
|
+
this.db = Database_1.Database.connect();
|
|
14
|
+
}
|
|
15
|
+
withTrashed() {
|
|
16
|
+
this.softDelete = false;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
onlyTrashed() {
|
|
20
|
+
this.softDelete = false;
|
|
21
|
+
this.whereNotNull("deleted_at");
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
with(relations) {
|
|
25
|
+
if (typeof relations === "string") {
|
|
26
|
+
this.eagerLoad = [relations];
|
|
27
|
+
} else {
|
|
28
|
+
this.eagerLoad = relations;
|
|
29
|
+
}
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
setModel(model) {
|
|
33
|
+
this.model = model;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
has(relation, operator = ">=", count = 1, boolean = "and", callback) {
|
|
37
|
+
if (!this.model) {
|
|
38
|
+
throw new Error("Model not set for builder");
|
|
39
|
+
}
|
|
40
|
+
const instance = new this.model();
|
|
41
|
+
const relationObject = instance[relation]();
|
|
42
|
+
const query = relationObject.getRelationExistenceQuery(
|
|
43
|
+
relationObject.getQuery(),
|
|
44
|
+
this.getQueryBuilder()
|
|
45
|
+
);
|
|
46
|
+
if (callback) {
|
|
47
|
+
callback(query);
|
|
48
|
+
}
|
|
49
|
+
const { sql, bindings } = query.toSQL();
|
|
50
|
+
if (operator === ">=" && count === 1) {
|
|
51
|
+
const method = boolean === "or" ? "orWhereRaw" : "whereRaw";
|
|
52
|
+
this[method](`exists (${sql})`, bindings);
|
|
53
|
+
} else {
|
|
54
|
+
query.clearSelect().count("*");
|
|
55
|
+
const { sql: countSql, bindings: countBindings } = query.toSQL();
|
|
56
|
+
const method = boolean === "or" ? "orWhereRaw" : "whereRaw";
|
|
57
|
+
this[method](`(${countSql}) ${operator} ?`, [...countBindings, count]);
|
|
58
|
+
}
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
whereHas(relation, callback, operator = ">=", count = 1) {
|
|
62
|
+
return this.has(relation, operator, count, "and", callback);
|
|
63
|
+
}
|
|
64
|
+
orWhereHas(relation, callback, operator = ">=", count = 1) {
|
|
65
|
+
return this.has(relation, operator, count, "or", callback);
|
|
66
|
+
}
|
|
67
|
+
getQueryBuilder() {
|
|
68
|
+
if (!this.queryBuilder) {
|
|
69
|
+
this.queryBuilder = this.db.queryBuilder().from(this.tableName);
|
|
70
|
+
}
|
|
71
|
+
return this.queryBuilder;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Set the table for the query and reset the queryBuilder
|
|
75
|
+
*/
|
|
76
|
+
from(tableName) {
|
|
77
|
+
this.tableName = tableName;
|
|
78
|
+
this.queryBuilder = this.db.queryBuilder().from(this.tableName);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* @param columnName
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
as(columnName) {
|
|
87
|
+
this.getQueryBuilder().as(columnName);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
fromRaw(query) {
|
|
91
|
+
this.queryBuilder?.fromRaw(query);
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Alias for from()
|
|
96
|
+
*/
|
|
97
|
+
table(tableName) {
|
|
98
|
+
return this.from(tableName);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Select specific columns for the query.
|
|
102
|
+
* @param
|
|
103
|
+
*/
|
|
104
|
+
select(...columns) {
|
|
105
|
+
this.queryBuilder = this.getQueryBuilder().select(
|
|
106
|
+
columns.length ? columns : "*"
|
|
107
|
+
);
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
whereJson(path, operator, value) {
|
|
111
|
+
const [jsonColumn, ...pathParts] = path.split(".");
|
|
112
|
+
const jsonPath = `$.${pathParts.join(".")}`;
|
|
113
|
+
if (!value) {
|
|
114
|
+
this.getQueryBuilder().whereRaw(`JSON_UNQUOTE(JSON_EXTRACT(??, ?)) = ?`, [
|
|
115
|
+
jsonColumn,
|
|
116
|
+
jsonPath,
|
|
117
|
+
operator,
|
|
118
|
+
]);
|
|
119
|
+
} else {
|
|
120
|
+
this.getQueryBuilder().whereRaw(
|
|
121
|
+
`JSON_UNQUOTE(JSON_EXTRACT(??, ?)) ${operator} ?`,
|
|
122
|
+
[jsonColumn, jsonPath, operator]
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Add a where clause to the query - Laravel style
|
|
129
|
+
*
|
|
130
|
+
* Supports multiple signatures:
|
|
131
|
+
* 1. where(callback) - Grouped conditions
|
|
132
|
+
* 2. where([...conditions]) - Array of conditions
|
|
133
|
+
* 3. where({key: value}) - Object syntax
|
|
134
|
+
* 4. where(column, value) - Two-argument form (assumes = operator)
|
|
135
|
+
* 5. where(column, operator, value) - Three-argument form
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* // Closure for grouped conditions
|
|
139
|
+
* query.where((q) => {
|
|
140
|
+
* q.where('age', '>', 18).orWhere('vip', true);
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* // Array of conditions
|
|
144
|
+
* query.where([
|
|
145
|
+
* ['status', 'active'],
|
|
146
|
+
* ['role', '=', 'admin']
|
|
147
|
+
* ]);
|
|
148
|
+
*
|
|
149
|
+
* // Object syntax
|
|
150
|
+
* query.where({ status: 'active', role: 'admin' });
|
|
151
|
+
*
|
|
152
|
+
* // Two-argument form
|
|
153
|
+
* query.where('status', 'active');
|
|
154
|
+
*
|
|
155
|
+
* // Three-argument form
|
|
156
|
+
* query.where('age', '>', 18);
|
|
157
|
+
*/
|
|
158
|
+
where(column, operator, value) {
|
|
159
|
+
if (typeof column === "function") {
|
|
160
|
+
this.getQueryBuilder().where((qb) => {
|
|
161
|
+
const subQuery = new this.constructor();
|
|
162
|
+
subQuery.queryBuilder = qb;
|
|
163
|
+
column(subQuery);
|
|
164
|
+
});
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(column)) {
|
|
168
|
+
column.forEach((condition) => {
|
|
169
|
+
if (condition.length === 2) {
|
|
170
|
+
this.where(condition[0], "=", condition[1]);
|
|
171
|
+
} else if (condition.length === 3) {
|
|
172
|
+
this.where(condition[0], condition[1], condition[2]);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
if (
|
|
178
|
+
typeof column === "object" &&
|
|
179
|
+
column !== null &&
|
|
180
|
+
!Array.isArray(column)
|
|
181
|
+
) {
|
|
182
|
+
Object.entries(column).forEach(([key, val]) => {
|
|
183
|
+
this.where(key, "=", val);
|
|
184
|
+
});
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
// Case 4 & 5: String column name
|
|
188
|
+
const isJson = column.includes(".") && !column.includes(" ");
|
|
189
|
+
// Two-argument form: where(column, value)
|
|
190
|
+
if (value === undefined && operator !== undefined && !isJson) {
|
|
191
|
+
this.getQueryBuilder().where(column, "=", operator);
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
// JSON path handling
|
|
195
|
+
if (isJson) {
|
|
196
|
+
this.whereJson(column, operator, value || "");
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
// Three-argument form: where(column, operator, value)
|
|
200
|
+
this.getQueryBuilder().where(column, operator, value || "");
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
whereNull(column) {
|
|
204
|
+
this.getQueryBuilder().whereNull(column);
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
whereNot(column, operator, value) {
|
|
208
|
+
if (value === undefined) {
|
|
209
|
+
this.getQueryBuilder().whereNot(column, operator);
|
|
210
|
+
} else {
|
|
211
|
+
this.getQueryBuilder().whereNot(column, operator, value);
|
|
212
|
+
}
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
orWhereNot(column, operator, value) {
|
|
216
|
+
if (value === undefined) {
|
|
217
|
+
this.getQueryBuilder().orWhereNot(column, operator);
|
|
218
|
+
} else {
|
|
219
|
+
this.getQueryBuilder().orWhereNot(column, operator, value);
|
|
220
|
+
}
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
whereNotNull(column) {
|
|
224
|
+
this.getQueryBuilder().whereNotNull(column);
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
whereExists(callback) {
|
|
228
|
+
return this.applyExists("whereRaw", callback);
|
|
229
|
+
}
|
|
230
|
+
orWhereExists(callback) {
|
|
231
|
+
return this.applyExists("orWhereRaw", callback);
|
|
232
|
+
}
|
|
233
|
+
whereBetween(column, range) {
|
|
234
|
+
this.getQueryBuilder().whereBetween(column, range);
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
orWhereBetween(column, range) {
|
|
238
|
+
this.getQueryBuilder().orWhereBetween(column, range);
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
whereNotBetween(column, range) {
|
|
242
|
+
this.getQueryBuilder().whereNotBetween(column, range);
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
orWhereNotBetween(column, range) {
|
|
246
|
+
this.getQueryBuilder().orWhereNotBetween(column, range);
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
orderBy(column, direction = "asc") {
|
|
250
|
+
this.getQueryBuilder().orderBy(column, direction);
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
orderByRaw(query, bindings) {
|
|
254
|
+
this.getQueryBuilder().orderByRaw(query, bindings);
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
latest(column = "id") {
|
|
258
|
+
this.getQueryBuilder().orderBy(column, "desc");
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
older(column = "id") {
|
|
262
|
+
this.getQueryBuilder().orderBy(column, "asc");
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
having(column, operator, value) {
|
|
266
|
+
if (!value) {
|
|
267
|
+
this.getQueryBuilder().having(column, "=", operator);
|
|
268
|
+
} else {
|
|
269
|
+
this.getQueryBuilder().having(column, operator, value);
|
|
270
|
+
}
|
|
271
|
+
return this;
|
|
272
|
+
}
|
|
273
|
+
havingIn(column, values) {
|
|
274
|
+
this.getQueryBuilder().havingIn(column, values);
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
havingNotIn(column, values) {
|
|
278
|
+
this.getQueryBuilder().havingIn(column, values);
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
havingNull(column) {
|
|
282
|
+
this.getQueryBuilder().havingNull(column);
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
havingNotNull(column) {
|
|
286
|
+
this.getQueryBuilder().havingNotNull(column);
|
|
287
|
+
return this;
|
|
288
|
+
}
|
|
289
|
+
havingBetween(column, values) {
|
|
290
|
+
this.getQueryBuilder().havingBetween(column, values);
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
havingNotBetween(column, values) {
|
|
294
|
+
this.getQueryBuilder().havingNotBetween(column, values);
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
havingRaw(column, values) {
|
|
298
|
+
this.getQueryBuilder().havingRaw(column, values);
|
|
299
|
+
return this;
|
|
300
|
+
}
|
|
301
|
+
orWhere(column, operator, value) {
|
|
302
|
+
if (value === undefined) {
|
|
303
|
+
this.getQueryBuilder().orWhere(column, operator);
|
|
304
|
+
} else {
|
|
305
|
+
this.getQueryBuilder().orWhere(column, operator, value);
|
|
306
|
+
}
|
|
307
|
+
return this;
|
|
308
|
+
}
|
|
309
|
+
whereIn(column, values) {
|
|
310
|
+
if (typeof values === "function") {
|
|
311
|
+
this.getQueryBuilder().whereIn(column, function () {
|
|
312
|
+
values(this);
|
|
313
|
+
});
|
|
314
|
+
} else {
|
|
315
|
+
this.getQueryBuilder().whereIn(column, values);
|
|
316
|
+
}
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
whereLike(column, value) {
|
|
320
|
+
this.getQueryBuilder().whereLike(column, `%${value}%`);
|
|
321
|
+
return this;
|
|
322
|
+
}
|
|
323
|
+
andWhereLike(column, value) {
|
|
324
|
+
this.getQueryBuilder().andWhereLike(column, `%${value}%`);
|
|
325
|
+
return this;
|
|
326
|
+
}
|
|
327
|
+
orWhereLike(column, value) {
|
|
328
|
+
this.getQueryBuilder().orWhereLike(column, `%${value}%`);
|
|
329
|
+
return this;
|
|
330
|
+
}
|
|
331
|
+
joinRaw(sql, bindings) {
|
|
332
|
+
this.getQueryBuilder().joinRaw(sql, bindings);
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
join(table, columnOrCallback, operator, column2) {
|
|
336
|
+
return this.applyJoin("join", table, columnOrCallback, operator, column2);
|
|
337
|
+
}
|
|
338
|
+
innerJoin(table, columnOrCallback, operator, column2) {
|
|
339
|
+
return this.applyJoin(
|
|
340
|
+
"innerJoin",
|
|
341
|
+
table,
|
|
342
|
+
columnOrCallback,
|
|
343
|
+
operator,
|
|
344
|
+
column2
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
toSQL() {
|
|
348
|
+
return this.getQueryBuilder().toSQL();
|
|
349
|
+
}
|
|
350
|
+
joinSub(subQuery, alias, callback) {
|
|
351
|
+
return this._joinSubWithType("INNER", subQuery, alias, callback);
|
|
352
|
+
}
|
|
353
|
+
leftJoin(table, columnOrCallback, operator, column2) {
|
|
354
|
+
return this.applyJoin(
|
|
355
|
+
"leftJoin",
|
|
356
|
+
table,
|
|
357
|
+
columnOrCallback,
|
|
358
|
+
operator,
|
|
359
|
+
column2
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
rightJoin(table, columnOrCallback, operator, column2) {
|
|
363
|
+
return this.applyJoin(
|
|
364
|
+
"rightJoin",
|
|
365
|
+
table,
|
|
366
|
+
columnOrCallback,
|
|
367
|
+
operator,
|
|
368
|
+
column2
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
leftOuterJoin(table, columnOrCallback, operator, column2) {
|
|
372
|
+
return this.applyJoin(
|
|
373
|
+
"leftOuterJoin",
|
|
374
|
+
table,
|
|
375
|
+
columnOrCallback,
|
|
376
|
+
operator,
|
|
377
|
+
column2
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
rightOuterJoin(table, columnOrCallback, operator, column2) {
|
|
381
|
+
return this.applyJoin(
|
|
382
|
+
"rightOuterJoin",
|
|
383
|
+
table,
|
|
384
|
+
columnOrCallback,
|
|
385
|
+
operator,
|
|
386
|
+
column2
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
fullOuterJoin(table, columnOrCallback, operator, column2) {
|
|
390
|
+
return this.applyJoin(
|
|
391
|
+
"fullOuterJoin",
|
|
392
|
+
table,
|
|
393
|
+
columnOrCallback,
|
|
394
|
+
operator,
|
|
395
|
+
column2
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
_joinSubWithType(type, subQuery, alias, callback) {
|
|
399
|
+
const { sql, bindings } = subQuery.toSQL();
|
|
400
|
+
const clauses = [];
|
|
401
|
+
const onBuilder = {
|
|
402
|
+
on(left, op, right) {
|
|
403
|
+
clauses.push(`${left} ${op} ${right}`);
|
|
404
|
+
},
|
|
405
|
+
onRaw(raw) {
|
|
406
|
+
clauses.push(raw);
|
|
407
|
+
},
|
|
408
|
+
};
|
|
409
|
+
callback(onBuilder);
|
|
410
|
+
const onClause = clauses.join(" ");
|
|
411
|
+
clauses.length = 0;
|
|
412
|
+
this.joinRaw(
|
|
413
|
+
`${type} JOIN (${this.raw(sql, bindings)}) AS ${alias} ${onClause}`
|
|
414
|
+
);
|
|
415
|
+
return this;
|
|
416
|
+
}
|
|
417
|
+
applyJoin(type, table, columnOrCallback, operator, column2) {
|
|
418
|
+
const qb = this.getQueryBuilder();
|
|
419
|
+
const self = this;
|
|
420
|
+
//
|
|
421
|
+
if (typeof columnOrCallback === "function") {
|
|
422
|
+
qb.join(table, function () {
|
|
423
|
+
columnOrCallback(self.applyJoinHelper(this));
|
|
424
|
+
});
|
|
425
|
+
} else if (operator && column2) {
|
|
426
|
+
qb.join(table, columnOrCallback, operator, column2);
|
|
427
|
+
} else {
|
|
428
|
+
throw new Error(`Invalid ${type} parameters`);
|
|
429
|
+
}
|
|
430
|
+
return this;
|
|
431
|
+
}
|
|
432
|
+
crossJoinSub(subQuery, alias) {
|
|
433
|
+
const { sql, bindings } = subQuery.toSQL();
|
|
434
|
+
this.joinRaw(`CROSS JOIN (${this.raw(sql, bindings)}) AS ${alias}`);
|
|
435
|
+
return this;
|
|
436
|
+
}
|
|
437
|
+
leftJoinSub(subQuery, alias, callback) {
|
|
438
|
+
return this._joinSubWithType("LEFT", subQuery, alias, callback);
|
|
439
|
+
}
|
|
440
|
+
rightJoinSub(subQuery, alias, callback) {
|
|
441
|
+
return this._joinSubWithType("RIGHT", subQuery, alias, callback);
|
|
442
|
+
}
|
|
443
|
+
clearSelect() {
|
|
444
|
+
this.getQueryBuilder().clearSelect();
|
|
445
|
+
return this;
|
|
446
|
+
}
|
|
447
|
+
clearGroup() {
|
|
448
|
+
this.getQueryBuilder().clearGroup();
|
|
449
|
+
return this;
|
|
450
|
+
}
|
|
451
|
+
clearHaving() {
|
|
452
|
+
this.getQueryBuilder().clearHaving();
|
|
453
|
+
return this;
|
|
454
|
+
}
|
|
455
|
+
clearWhere() {
|
|
456
|
+
this.getQueryBuilder().clearWhere();
|
|
457
|
+
return this;
|
|
458
|
+
}
|
|
459
|
+
distinct(...columns) {
|
|
460
|
+
this.getQueryBuilder().distinct(columns);
|
|
461
|
+
return this;
|
|
462
|
+
}
|
|
463
|
+
groupBy(...columns) {
|
|
464
|
+
this.getQueryBuilder().groupBy(columns);
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
467
|
+
groupByRaw(query, bindings) {
|
|
468
|
+
this.getQueryBuilder().groupByRaw(query, bindings);
|
|
469
|
+
return this;
|
|
470
|
+
}
|
|
471
|
+
async get() {
|
|
472
|
+
const result = this.getQueryBuilder().clone();
|
|
473
|
+
this.reset();
|
|
474
|
+
return result;
|
|
475
|
+
}
|
|
476
|
+
async first() {
|
|
477
|
+
const result = await this.limit(1).get();
|
|
478
|
+
this.reset();
|
|
479
|
+
return result.length ? result[0] : null;
|
|
480
|
+
}
|
|
481
|
+
clone() {
|
|
482
|
+
const clone = new Builder();
|
|
483
|
+
clone.queryBuilder = this.getQueryBuilder().clone();
|
|
484
|
+
clone.tableName = this.tableName;
|
|
485
|
+
clone.model = this.model;
|
|
486
|
+
clone.db = this.db;
|
|
487
|
+
clone.eagerLoad = [...this.eagerLoad];
|
|
488
|
+
clone.softDelete = this.softDelete;
|
|
489
|
+
return clone;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Check if any record exists for the current query.
|
|
493
|
+
*/
|
|
494
|
+
async exists() {
|
|
495
|
+
const result = await this.first();
|
|
496
|
+
this.reset();
|
|
497
|
+
return !!result;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Check if no record exists for the current query.
|
|
501
|
+
*/
|
|
502
|
+
async doesntExist() {
|
|
503
|
+
return !(await this.exists());
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Get a single column's value from the first result.
|
|
507
|
+
*/
|
|
508
|
+
async value(column) {
|
|
509
|
+
const row = await this.first();
|
|
510
|
+
return row ? row[column] : null;
|
|
511
|
+
}
|
|
512
|
+
async insert(data) {
|
|
513
|
+
const result = await this.db(this.tableName).insert(data);
|
|
514
|
+
this.reset();
|
|
515
|
+
return result && Array.isArray(result) ? result[0] : result;
|
|
516
|
+
}
|
|
517
|
+
async update(data) {
|
|
518
|
+
let result = false;
|
|
519
|
+
for (const key in data) {
|
|
520
|
+
if (key.includes(".")) {
|
|
521
|
+
result = await this.updateJson(key, data[key]);
|
|
522
|
+
delete data[key];
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (Object.keys(data).length > 0) {
|
|
526
|
+
result = await this.getQueryBuilder().update(data);
|
|
527
|
+
}
|
|
528
|
+
this.reset();
|
|
529
|
+
return result ? true : false;
|
|
530
|
+
}
|
|
531
|
+
async delete() {
|
|
532
|
+
const result = await this.getQueryBuilder().delete();
|
|
533
|
+
this.reset();
|
|
534
|
+
return result;
|
|
535
|
+
}
|
|
536
|
+
raw(query, bindings = []) {
|
|
537
|
+
return this.db.raw(query, bindings);
|
|
538
|
+
}
|
|
539
|
+
whereRaw(query, bindings = []) {
|
|
540
|
+
this.getQueryBuilder().whereRaw(query, bindings);
|
|
541
|
+
return this;
|
|
542
|
+
}
|
|
543
|
+
orWhereRaw(query, bindings = []) {
|
|
544
|
+
this.getQueryBuilder().orWhereRaw(query, bindings);
|
|
545
|
+
return this;
|
|
546
|
+
}
|
|
547
|
+
async runQuery(query, bindings = []) {
|
|
548
|
+
const res = await this.db.raw(query, bindings);
|
|
549
|
+
return res?.rows ? res?.rows : res;
|
|
550
|
+
}
|
|
551
|
+
returning(columns) {
|
|
552
|
+
this.getQueryBuilder().returning(columns);
|
|
553
|
+
return this;
|
|
554
|
+
}
|
|
555
|
+
offset(value) {
|
|
556
|
+
this.getQueryBuilder().offset(value);
|
|
557
|
+
return this;
|
|
558
|
+
}
|
|
559
|
+
limit(value) {
|
|
560
|
+
this.getQueryBuilder().limit(value);
|
|
561
|
+
return this;
|
|
562
|
+
}
|
|
563
|
+
union(queries) {
|
|
564
|
+
return this.applyUnion("union", queries);
|
|
565
|
+
}
|
|
566
|
+
unionAll(queries) {
|
|
567
|
+
return this.applyUnion("unionAll", queries);
|
|
568
|
+
}
|
|
569
|
+
toSqlCode() {
|
|
570
|
+
return this.queryBuilder.toSQL().sql;
|
|
571
|
+
}
|
|
572
|
+
max(columns) {
|
|
573
|
+
return this.applyAggregate("max", columns);
|
|
574
|
+
}
|
|
575
|
+
async count(columns = "id") {
|
|
576
|
+
this.applyAggregate("count", columns);
|
|
577
|
+
return utils_1.Utils.countValue(await this.first());
|
|
578
|
+
}
|
|
579
|
+
min(columns) {
|
|
580
|
+
return this.applyAggregate("min", columns);
|
|
581
|
+
}
|
|
582
|
+
sum(columns) {
|
|
583
|
+
return this.applyAggregate("sum", columns);
|
|
584
|
+
}
|
|
585
|
+
avg(columns) {
|
|
586
|
+
return this.applyAggregate("avg", columns);
|
|
587
|
+
}
|
|
588
|
+
increment(column, amount) {
|
|
589
|
+
if (typeof column === "string") {
|
|
590
|
+
this.queryBuilder.increment(column, amount ?? 1);
|
|
591
|
+
} else {
|
|
592
|
+
this.queryBuilder.increment(column); // object form { col1: value1, col2: value2 }
|
|
593
|
+
}
|
|
594
|
+
return this;
|
|
595
|
+
}
|
|
596
|
+
truncate() {
|
|
597
|
+
this.queryBuilder.truncate();
|
|
598
|
+
return this;
|
|
599
|
+
}
|
|
600
|
+
async paginate(request, perPage = 15) {
|
|
601
|
+
const page = Math.max(1, Number(request.query.page) || 1);
|
|
602
|
+
const offset = (page - 1) * Number(perPage);
|
|
603
|
+
const count = await this.clone().count();
|
|
604
|
+
const total = count;
|
|
605
|
+
const data = await this.clone().offset(offset).limit(Number(perPage)).get();
|
|
606
|
+
const totalPages = Math.ceil(total / Number(perPage));
|
|
607
|
+
const path = `${request.protocol}://${request.get("host")}${request.path}`;
|
|
608
|
+
return {
|
|
609
|
+
data: data,
|
|
610
|
+
meta: {
|
|
611
|
+
current_page: page,
|
|
612
|
+
per_page: perPage,
|
|
613
|
+
total: total,
|
|
614
|
+
total_pages: totalPages,
|
|
615
|
+
has_more: page < totalPages,
|
|
616
|
+
links: utils_1.Utils.generatePaginationLinks(path, page, totalPages),
|
|
617
|
+
},
|
|
618
|
+
links: {
|
|
619
|
+
first: `${path}?page=1`,
|
|
620
|
+
last: `${path}?page=${totalPages}`,
|
|
621
|
+
},
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
plunk(column) {
|
|
625
|
+
this.getQueryBuilder().pluck(column);
|
|
626
|
+
return this;
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Get an array of values for a single column (alias like Laravel pluck).
|
|
630
|
+
*/
|
|
631
|
+
async pluck(column) {
|
|
632
|
+
const result = await this.queryBuilder.pluck(column);
|
|
633
|
+
this.reset();
|
|
634
|
+
return result;
|
|
635
|
+
}
|
|
636
|
+
decrement(column, amount) {
|
|
637
|
+
if (typeof column === "string") {
|
|
638
|
+
this.getQueryBuilder().decrement(column, amount ?? 1);
|
|
639
|
+
} else {
|
|
640
|
+
this.getQueryBuilder().decrement(column); // object form
|
|
641
|
+
}
|
|
642
|
+
return this;
|
|
643
|
+
}
|
|
644
|
+
async updateJson(path, value) {
|
|
645
|
+
const [column, ...jsonPathParts] = path.split(".");
|
|
646
|
+
const jsonPath = "$." + jsonPathParts.join(".");
|
|
647
|
+
const result = await this.getQueryBuilder().update({
|
|
648
|
+
[column]: this.db.raw(`JSON_SET(??, ?, ?)`, [column, jsonPath, value]),
|
|
649
|
+
});
|
|
650
|
+
this.reset();
|
|
651
|
+
return result ? true : false;
|
|
652
|
+
}
|
|
653
|
+
applyJoinHelper(joinBuilder) {
|
|
654
|
+
const db = this.db; // or however you access knex
|
|
655
|
+
const helper = {
|
|
656
|
+
on: (c1, op, c2) => {
|
|
657
|
+
joinBuilder.on(c1, op, c2);
|
|
639
658
|
return helper;
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
}
|
|
659
|
+
},
|
|
660
|
+
orOn: (c1, op, c2) => {
|
|
661
|
+
joinBuilder.orOn(c1, op, c2);
|
|
662
|
+
return helper;
|
|
663
|
+
},
|
|
664
|
+
raw: (sql, bindings = []) => {
|
|
665
|
+
joinBuilder.on(db.raw(sql, bindings));
|
|
666
|
+
return helper;
|
|
667
|
+
},
|
|
668
|
+
};
|
|
669
|
+
return helper;
|
|
670
|
+
}
|
|
671
|
+
whereColumn(column, operator, value) {
|
|
672
|
+
return this.applyColumn("whereColumn", column, operator, value);
|
|
673
|
+
}
|
|
674
|
+
orWhereColumn(column, operator, value) {
|
|
675
|
+
return this.applyColumn("orWhereColumn", column, operator, value);
|
|
676
|
+
}
|
|
677
|
+
whereNotColumn(column, operator, value) {
|
|
678
|
+
return this.applyColumn("whereNotColumn", column, operator, value);
|
|
679
|
+
}
|
|
680
|
+
applyExists(method, callback) {
|
|
681
|
+
const sub = this.getQueryBuilder();
|
|
682
|
+
callback(sub);
|
|
683
|
+
const { sql, bindings } = sub.clone().toSQL();
|
|
684
|
+
this.getQueryBuilder()[method](`exists (${sql})`, bindings);
|
|
685
|
+
return this;
|
|
686
|
+
}
|
|
687
|
+
applyColumn(type, column, operator, value) {
|
|
688
|
+
if (!value) {
|
|
689
|
+
this.getQueryBuilder()[type](column, operator);
|
|
690
|
+
} else {
|
|
691
|
+
this.getQueryBuilder()[type](column, operator, value);
|
|
692
|
+
}
|
|
693
|
+
return this;
|
|
694
|
+
}
|
|
695
|
+
applyAggregate(type, columns) {
|
|
696
|
+
const qb = this.getQueryBuilder();
|
|
697
|
+
if (typeof columns === "string") {
|
|
698
|
+
switch (type) {
|
|
699
|
+
case "max":
|
|
700
|
+
qb.max(columns);
|
|
701
|
+
break;
|
|
702
|
+
case "min":
|
|
703
|
+
qb.min(columns);
|
|
704
|
+
break;
|
|
705
|
+
case "sum":
|
|
706
|
+
qb.sum(columns);
|
|
707
|
+
break;
|
|
708
|
+
case "avg":
|
|
709
|
+
qb.avg(columns);
|
|
710
|
+
break;
|
|
711
|
+
case "count":
|
|
712
|
+
qb.count(columns);
|
|
713
|
+
break;
|
|
714
|
+
}
|
|
715
|
+
} else if (Array.isArray(columns)) {
|
|
716
|
+
columns.forEach((col) => {
|
|
717
|
+
switch (type) {
|
|
718
|
+
case "max":
|
|
719
|
+
qb.max(col);
|
|
720
|
+
break;
|
|
721
|
+
case "min":
|
|
722
|
+
qb.min(col);
|
|
723
|
+
break;
|
|
724
|
+
case "sum":
|
|
725
|
+
qb.sum(col);
|
|
726
|
+
break;
|
|
727
|
+
case "avg":
|
|
728
|
+
qb.avg(col);
|
|
729
|
+
break;
|
|
730
|
+
case "count":
|
|
731
|
+
qb.count(col);
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
} else {
|
|
736
|
+
Object.entries(columns).forEach(([alias, col]) => {
|
|
737
|
+
const expr = `${col} as ${alias}`;
|
|
738
|
+
switch (type) {
|
|
739
|
+
case "max":
|
|
740
|
+
qb.max(expr);
|
|
741
|
+
break;
|
|
742
|
+
case "min":
|
|
743
|
+
qb.min(expr);
|
|
744
|
+
break;
|
|
745
|
+
case "sum":
|
|
746
|
+
qb.sum(expr);
|
|
747
|
+
break;
|
|
748
|
+
case "avg":
|
|
749
|
+
qb.avg(expr);
|
|
750
|
+
break;
|
|
751
|
+
case "count":
|
|
752
|
+
qb.count(expr);
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
return this;
|
|
758
|
+
}
|
|
759
|
+
applyUnion(type, queries) {
|
|
760
|
+
const qb = this.getQueryBuilder();
|
|
761
|
+
if (typeof queries === "function") {
|
|
762
|
+
qb[type](queries);
|
|
763
|
+
} else if (Array.isArray(queries)) {
|
|
764
|
+
const queryBuilders = queries.map((q) => q.getQueryBuilder());
|
|
765
|
+
qb[type](queryBuilders);
|
|
766
|
+
} else {
|
|
767
|
+
qb[type](queries);
|
|
768
|
+
}
|
|
769
|
+
return this;
|
|
770
|
+
}
|
|
771
|
+
reset() {
|
|
772
|
+
this.queryBuilder = null;
|
|
773
|
+
// this.tableName = "";
|
|
774
|
+
}
|
|
750
775
|
}
|
|
751
776
|
exports.Builder = Builder;
|