@dbcube/query-builder 3.0.27 → 3.0.29

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/dist/index.js CHANGED
@@ -175,30 +175,33 @@ var Table = class _Table {
175
175
  * console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
176
176
  */
177
177
  select(fields = []) {
178
- this.dml.type = "select";
179
- this.dml.columns = fields.length > 0 ? fields : ["*"];
180
- return this;
178
+ const clone = this.clone();
179
+ clone.dml.type = "select";
180
+ clone.dml.columns = fields.length > 0 ? fields : ["*"];
181
+ return clone;
181
182
  }
182
183
  where(column, operator, value) {
183
- this.dml.where.push({
184
+ const clone = this.clone();
185
+ clone.dml.where.push({
184
186
  column,
185
187
  operator,
186
188
  value,
187
- type: this.nextType,
189
+ type: clone.nextType,
188
190
  isGroup: false
189
191
  });
190
- this.nextType = "AND";
191
- return this;
192
+ clone.nextType = "AND";
193
+ return clone;
192
194
  }
193
195
  orWhere(column, operator, value) {
194
- this.dml.where.push({
196
+ const clone = this.clone();
197
+ clone.dml.where.push({
195
198
  column,
196
199
  operator,
197
200
  value,
198
201
  type: "OR",
199
202
  isGroup: false
200
203
  });
201
- return this;
204
+ return clone;
202
205
  }
203
206
  /**
204
207
  * Adds a grouped WHERE condition to the query.
@@ -213,23 +216,26 @@ var Table = class _Table {
213
216
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
214
217
  */
215
218
  whereGroup(callback) {
216
- const groupQuery = new _Table(this.dml.database, this.dml.table, this.engine);
219
+ const clone = this.clone();
220
+ const groupQuery = new _Table(clone.dml.database, clone.dml.table, clone.engine);
217
221
  callback(groupQuery);
218
- this.dml.where.push({
219
- type: this.nextType,
222
+ clone.dml.where.push({
223
+ type: clone.nextType,
220
224
  isGroup: true,
221
225
  conditions: groupQuery.dml.where
222
226
  });
223
- this.nextType = "AND";
224
- return this;
227
+ clone.nextType = "AND";
228
+ return clone;
225
229
  }
226
230
  or() {
227
- this.nextType = "OR";
228
- return this;
231
+ const clone = this.clone();
232
+ clone.nextType = "OR";
233
+ return clone;
229
234
  }
230
235
  and() {
231
- this.nextType = "AND";
232
- return this;
236
+ const clone = this.clone();
237
+ clone.nextType = "AND";
238
+ return clone;
233
239
  }
234
240
  /**
235
241
  * Adds a WHERE BETWEEN condition to the query.
@@ -243,18 +249,19 @@ var Table = class _Table {
243
249
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
244
250
  */
245
251
  whereBetween(column, values) {
252
+ const clone = this.clone();
246
253
  const [value1, value2] = values;
247
254
  if (value1 !== void 0 && value2 !== void 0) {
248
- this.dml.where.push({
255
+ clone.dml.where.push({
249
256
  column,
250
257
  operator: "BETWEEN",
251
258
  value: [value1, value2],
252
- type: this.nextType,
259
+ type: clone.nextType,
253
260
  isGroup: false
254
261
  });
255
- this.nextType = "AND";
262
+ clone.nextType = "AND";
256
263
  }
257
- return this;
264
+ return clone;
258
265
  }
259
266
  /**
260
267
  * Adds a WHERE IN condition to the query.
@@ -268,17 +275,18 @@ var Table = class _Table {
268
275
  * console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
269
276
  */
270
277
  whereIn(column, values) {
278
+ const clone = this.clone();
271
279
  if (Array.isArray(values) && values.length > 0) {
272
- this.dml.where.push({
280
+ clone.dml.where.push({
273
281
  column,
274
282
  operator: "IN",
275
283
  value: values,
276
- type: this.nextType,
284
+ type: clone.nextType,
277
285
  isGroup: false
278
286
  });
279
- this.nextType = "AND";
287
+ clone.nextType = "AND";
280
288
  }
281
- return this;
289
+ return clone;
282
290
  }
283
291
  /**
284
292
  * Adds a WHERE IS NULL condition to the query.
@@ -291,14 +299,15 @@ var Table = class _Table {
291
299
  * console.log(users); // [{ id: 3, name: 'Alice', email: null }]
292
300
  */
293
301
  whereNull(column) {
294
- this.dml.where.push({
302
+ const clone = this.clone();
303
+ clone.dml.where.push({
295
304
  column,
296
305
  operator: "IS NULL",
297
- type: this.nextType,
306
+ type: clone.nextType,
298
307
  isGroup: false
299
308
  });
300
- this.nextType = "AND";
301
- return this;
309
+ clone.nextType = "AND";
310
+ return clone;
302
311
  }
303
312
  /**
304
313
  * Adds a WHERE IS NOT NULL condition to the query.
@@ -311,14 +320,15 @@ var Table = class _Table {
311
320
  * console.log(users); // [{ id: 1, name: 'John', email: 'john@example.com' }]
312
321
  */
313
322
  whereNotNull(column) {
314
- this.dml.where.push({
323
+ const clone = this.clone();
324
+ clone.dml.where.push({
315
325
  column,
316
326
  operator: "IS NOT NULL",
317
- type: this.nextType,
327
+ type: clone.nextType,
318
328
  isGroup: false
319
329
  });
320
- this.nextType = "AND";
321
- return this;
330
+ clone.nextType = "AND";
331
+ return clone;
322
332
  }
323
333
  /**
324
334
  * Adds a JOIN clause to the query.
@@ -334,7 +344,8 @@ var Table = class _Table {
334
344
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }]
335
345
  */
336
346
  join(table, column1, operator, column2) {
337
- this.dml.joins.push({
347
+ const clone = this.clone();
348
+ clone.dml.joins.push({
338
349
  type: "INNER",
339
350
  table,
340
351
  on: {
@@ -343,7 +354,7 @@ var Table = class _Table {
343
354
  column2
344
355
  }
345
356
  });
346
- return this;
357
+ return clone;
347
358
  }
348
359
  /**
349
360
  * Adds a LEFT JOIN clause to the query.
@@ -359,7 +370,8 @@ var Table = class _Table {
359
370
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: 2, name: 'Jane', order_id: null }]
360
371
  */
361
372
  leftJoin(table, column1, operator, column2) {
362
- this.dml.joins.push({
373
+ const clone = this.clone();
374
+ clone.dml.joins.push({
363
375
  type: "LEFT",
364
376
  table,
365
377
  on: {
@@ -368,7 +380,7 @@ var Table = class _Table {
368
380
  column2
369
381
  }
370
382
  });
371
- return this;
383
+ return clone;
372
384
  }
373
385
  /**
374
386
  * Adds a RIGHT JOIN clause to the query.
@@ -384,7 +396,8 @@ var Table = class _Table {
384
396
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: null, name: null, order_id: 102 }]
385
397
  */
386
398
  rightJoin(table, column1, operator, column2) {
387
- this.dml.joins.push({
399
+ const clone = this.clone();
400
+ clone.dml.joins.push({
388
401
  type: "RIGHT",
389
402
  table,
390
403
  on: {
@@ -393,7 +406,7 @@ var Table = class _Table {
393
406
  column2
394
407
  }
395
408
  });
396
- return this;
409
+ return clone;
397
410
  }
398
411
  /**
399
412
  * Adds an ORDER BY clause to the query.
@@ -407,16 +420,17 @@ var Table = class _Table {
407
420
  * console.log(users); // [{ id: 2, name: 'Jane' }, { id: 1, name: 'John' }]
408
421
  */
409
422
  orderBy(column, direction = "ASC") {
423
+ const clone = this.clone();
410
424
  const validDirections = ["ASC", "DESC"];
411
425
  if (validDirections.includes(direction.toUpperCase())) {
412
- this.dml.orderBy.push({
426
+ clone.dml.orderBy.push({
413
427
  column,
414
428
  direction: direction.toUpperCase()
415
429
  });
416
430
  } else {
417
431
  throw new Error(`Invalid direction: ${direction}. Use 'ASC' or 'DESC'.`);
418
432
  }
419
- return this;
433
+ return clone;
420
434
  }
421
435
  /**
422
436
  * Adds a GROUP BY clause to the query.
@@ -429,8 +443,9 @@ var Table = class _Table {
429
443
  * console.log(users); // [{ age: 30, count: 1 }, { age: 25, count: 1 }]
430
444
  */
431
445
  groupBy(column) {
432
- this.dml.groupBy.push(column);
433
- return this;
446
+ const clone = this.clone();
447
+ clone.dml.groupBy.push(column);
448
+ return clone;
434
449
  }
435
450
  /**
436
451
  * Adds a DISTINCT clause to the query.
@@ -442,8 +457,9 @@ var Table = class _Table {
442
457
  * console.log(users); // [{ name: 'John' }, { name: 'Jane' }]
443
458
  */
444
459
  distinct() {
445
- this.dml.distinct = true;
446
- return this;
460
+ const clone = this.clone();
461
+ clone.dml.distinct = true;
462
+ return clone;
447
463
  }
448
464
  /**
449
465
  * Adds a COUNT clause to the query.
@@ -456,18 +472,20 @@ var Table = class _Table {
456
472
  * console.log(count); // { count: 2 }
457
473
  */
458
474
  async count(column = "*") {
459
- const clonedDML = this.cloneDML();
460
- clonedDML.type = "select";
461
- clonedDML.aggregation = {
462
- type: "COUNT",
463
- column,
464
- alias: "count"
475
+ const executionDML = {
476
+ ...this.dml,
477
+ type: "select",
478
+ aggregation: {
479
+ type: "COUNT",
480
+ column,
481
+ alias: "count"
482
+ },
483
+ columns: [`COUNT(${column}) AS count`],
484
+ data: null,
485
+ limit: 1
465
486
  };
466
- clonedDML.columns = [`COUNT(${column}) AS count`];
467
- clonedDML.data = null;
468
- clonedDML.limit = 1;
469
487
  try {
470
- const result = await this.getResponse(clonedDML);
488
+ const result = await this.getResponse(executionDML);
471
489
  const res = result[0] || null;
472
490
  if (res) {
473
491
  return res.count;
@@ -488,18 +506,20 @@ var Table = class _Table {
488
506
  * console.log(totalAge); // { sum: 55 }
489
507
  */
490
508
  async sum(column) {
491
- const clonedDML = this.cloneDML();
492
- clonedDML.type = "select";
493
- clonedDML.aggregation = {
494
- type: "SUM",
495
- column,
496
- alias: "sum"
509
+ const executionDML = {
510
+ ...this.dml,
511
+ type: "select",
512
+ aggregation: {
513
+ type: "SUM",
514
+ column,
515
+ alias: "sum"
516
+ },
517
+ columns: [`SUM(${column}) AS sum`],
518
+ data: null,
519
+ limit: 1
497
520
  };
498
- clonedDML.columns = [`SUM(${column}) AS sum`];
499
- clonedDML.data = null;
500
- clonedDML.limit = 1;
501
521
  try {
502
- const result = await this.getResponse(clonedDML);
522
+ const result = await this.getResponse(executionDML);
503
523
  const res = result[0] || null;
504
524
  if (res) {
505
525
  return res.sum;
@@ -520,18 +540,20 @@ var Table = class _Table {
520
540
  * console.log(avgAge); // { avg: 27.5 }
521
541
  */
522
542
  async avg(column) {
523
- const clonedDML = this.cloneDML();
524
- clonedDML.type = "select";
525
- clonedDML.aggregation = {
526
- type: "AVG",
527
- column,
528
- alias: "avg"
543
+ const executionDML = {
544
+ ...this.dml,
545
+ type: "select",
546
+ aggregation: {
547
+ type: "AVG",
548
+ column,
549
+ alias: "avg"
550
+ },
551
+ columns: [`AVG(${column}) AS avg`],
552
+ data: null,
553
+ limit: 1
529
554
  };
530
- clonedDML.columns = [`AVG(${column}) AS avg`];
531
- clonedDML.data = null;
532
- clonedDML.limit = 1;
533
555
  try {
534
- const result = await this.getResponse(clonedDML);
556
+ const result = await this.getResponse(executionDML);
535
557
  const res = result[0] || null;
536
558
  if (res) {
537
559
  return res.avg;
@@ -552,18 +574,20 @@ var Table = class _Table {
552
574
  * console.log(maxAge); // { max: 30 }
553
575
  */
554
576
  async max(column) {
555
- const clonedDML = this.cloneDML();
556
- clonedDML.type = "select";
557
- clonedDML.aggregation = {
558
- type: "MAX",
559
- column,
560
- alias: "max"
577
+ const executionDML = {
578
+ ...this.dml,
579
+ type: "select",
580
+ aggregation: {
581
+ type: "MAX",
582
+ column,
583
+ alias: "max"
584
+ },
585
+ columns: [`MAX(${column}) AS max`],
586
+ data: null,
587
+ limit: 1
561
588
  };
562
- clonedDML.columns = [`MAX(${column}) AS max`];
563
- clonedDML.data = null;
564
- clonedDML.limit = 1;
565
589
  try {
566
- const result = await this.getResponse(clonedDML);
590
+ const result = await this.getResponse(executionDML);
567
591
  const res = result[0] || null;
568
592
  if (res) {
569
593
  return res.max;
@@ -584,18 +608,20 @@ var Table = class _Table {
584
608
  * console.log(minAge); // { min: 25 }
585
609
  */
586
610
  async min(column) {
587
- const clonedDML = this.cloneDML();
588
- clonedDML.type = "select";
589
- clonedDML.aggregation = {
590
- type: "MIN",
591
- column,
592
- alias: "min"
611
+ const executionDML = {
612
+ ...this.dml,
613
+ type: "select",
614
+ aggregation: {
615
+ type: "MIN",
616
+ column,
617
+ alias: "min"
618
+ },
619
+ columns: [`MIN(${column}) AS min`],
620
+ data: null,
621
+ limit: 1
593
622
  };
594
- clonedDML.columns = [`MIN(${column}) AS min`];
595
- clonedDML.data = null;
596
- clonedDML.limit = 1;
597
623
  try {
598
- const result = await this.getResponse(clonedDML);
624
+ const result = await this.getResponse(executionDML);
599
625
  const res = result[0] || null;
600
626
  if (res) {
601
627
  return res.min;
@@ -616,8 +642,9 @@ var Table = class _Table {
616
642
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }]
617
643
  */
618
644
  limit(number) {
619
- this.dml.limit = number;
620
- return this;
645
+ const clone = this.clone();
646
+ clone.dml.limit = number;
647
+ return clone;
621
648
  }
622
649
  /**
623
650
  * Adds pagination to the query using LIMIT and OFFSET.
@@ -630,10 +657,11 @@ var Table = class _Table {
630
657
  * console.log(users); // [{ id: 2, name: 'Jane', age: 25 }]
631
658
  */
632
659
  page(number) {
633
- if (this.dml.limit) {
634
- this.dml.offset = (number - 1) * this.dml.limit;
660
+ const clone = this.clone();
661
+ if (clone.dml.limit) {
662
+ clone.dml.offset = (number - 1) * clone.dml.limit;
635
663
  }
636
- return this;
664
+ return clone;
637
665
  }
638
666
  /**
639
667
  * Executes the query and returns all matching rows.
@@ -646,10 +674,12 @@ var Table = class _Table {
646
674
  */
647
675
  async get() {
648
676
  try {
649
- const clonedDML = this.cloneDML();
650
- clonedDML.type = "select";
651
- clonedDML.data = null;
652
- const result = await this.getResponse(clonedDML);
677
+ const executionDML = {
678
+ ...this.dml,
679
+ type: "select",
680
+ data: null
681
+ };
682
+ const result = await this.getResponse(executionDML);
653
683
  return result;
654
684
  } catch (error) {
655
685
  throw error;
@@ -665,12 +695,14 @@ var Table = class _Table {
665
695
  * console.log(user); // { id: 1, name: 'John' }
666
696
  */
667
697
  async first() {
668
- const clonedDML = this.cloneDML();
669
- clonedDML.type = "select";
670
- clonedDML.data = null;
671
- clonedDML.limit = 1;
698
+ const executionDML = {
699
+ ...this.dml,
700
+ type: "select",
701
+ data: null,
702
+ limit: 1
703
+ };
672
704
  try {
673
- const result = await this.getResponse(clonedDML);
705
+ const result = await this.getResponse(executionDML);
674
706
  return result[0] || null;
675
707
  } catch (error) {
676
708
  throw error;
@@ -688,19 +720,21 @@ var Table = class _Table {
688
720
  * console.log(user); // { id: 1, name: 'John' }
689
721
  */
690
722
  async find(value, column = "id") {
691
- const clonedDML = this.cloneDML();
692
- clonedDML.type = "select";
693
- clonedDML.data = null;
694
- clonedDML.where.push({
695
- column,
696
- operator: "=",
697
- value,
698
- type: "AND",
699
- isGroup: false
700
- });
701
- clonedDML.limit = 1;
723
+ const executionDML = {
724
+ ...this.dml,
725
+ type: "select",
726
+ data: null,
727
+ where: [...this.dml.where, {
728
+ column,
729
+ operator: "=",
730
+ value,
731
+ type: "AND",
732
+ isGroup: false
733
+ }],
734
+ limit: 1
735
+ };
702
736
  try {
703
- const result = await this.getResponse(clonedDML);
737
+ const result = await this.getResponse(executionDML);
704
738
  return result[0] || null;
705
739
  } catch (error) {
706
740
  throw error;
@@ -726,10 +760,12 @@ var Table = class _Table {
726
760
  if (!data.every((item) => typeof item === "object" && item !== null)) {
727
761
  throw new Error("The array must contain only valid objects.");
728
762
  }
729
- const clonedDML = this.cloneDML();
730
- clonedDML.type = "insert";
731
- clonedDML.data = data;
732
- await this.getResponse(clonedDML, "Add");
763
+ const executionDML = {
764
+ ...this.dml,
765
+ type: "insert",
766
+ data
767
+ };
768
+ await this.getResponse(executionDML, "Add");
733
769
  return data;
734
770
  }
735
771
  /**
@@ -751,10 +787,12 @@ var Table = class _Table {
751
787
  if (this.dml.where.length === 0) {
752
788
  throw new Error("You must specify at least one WHERE condition to perform an update.");
753
789
  }
754
- const clonedDML = this.cloneDML();
755
- clonedDML.type = "update";
756
- clonedDML.data = data;
757
- await this.getResponse(clonedDML, "Update");
790
+ const executionDML = {
791
+ ...this.dml,
792
+ type: "update",
793
+ data
794
+ };
795
+ await this.getResponse(executionDML, "Update");
758
796
  return data;
759
797
  }
760
798
  /**
@@ -770,9 +808,11 @@ var Table = class _Table {
770
808
  if (this.dml.where.length === 0) {
771
809
  throw new Error("You must specify at least one WHERE condition to perform a delete.");
772
810
  }
773
- const clonedDML = this.cloneDML();
774
- clonedDML.type = "delete";
775
- const deleteData = await this.getResponse(clonedDML, "Delete");
811
+ const executionDML = {
812
+ ...this.dml,
813
+ type: "delete"
814
+ };
815
+ const deleteData = await this.getResponse(executionDML, "Delete");
776
816
  return deleteData;
777
817
  }
778
818
  async getResponse(dml = null, type = null) {
@@ -866,8 +906,14 @@ var Table = class _Table {
866
906
  }
867
907
  return arrayResult;
868
908
  }
869
- cloneDML() {
870
- return {
909
+ clone() {
910
+ const cloned = Object.create(Object.getPrototypeOf(this));
911
+ cloned.engine = this.engine;
912
+ cloned.nextType = this.nextType;
913
+ cloned.computedFields = this.computedFields;
914
+ cloned.trigger = this.trigger;
915
+ cloned.triggers = this.triggers;
916
+ cloned.dml = {
871
917
  ...this.dml,
872
918
  columns: [...this.dml.columns],
873
919
  joins: [...this.dml.joins],
@@ -875,22 +921,7 @@ var Table = class _Table {
875
921
  orderBy: [...this.dml.orderBy],
876
922
  groupBy: [...this.dml.groupBy]
877
923
  };
878
- }
879
- reset() {
880
- this.dml = {
881
- ...this.dml,
882
- type: "select",
883
- columns: ["*"],
884
- distinct: false,
885
- joins: [],
886
- where: [],
887
- orderBy: [],
888
- groupBy: [],
889
- limit: null,
890
- offset: null,
891
- data: null,
892
- aggregation: null
893
- };
924
+ return cloned;
894
925
  }
895
926
  };
896
927
  function returnFormattedError(status, message) {