@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/bun.lockb CHANGED
Binary file
package/dist/index.cjs CHANGED
@@ -213,30 +213,33 @@ var Table = class _Table {
213
213
  * console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
214
214
  */
215
215
  select(fields = []) {
216
- this.dml.type = "select";
217
- this.dml.columns = fields.length > 0 ? fields : ["*"];
218
- return this;
216
+ const clone = this.clone();
217
+ clone.dml.type = "select";
218
+ clone.dml.columns = fields.length > 0 ? fields : ["*"];
219
+ return clone;
219
220
  }
220
221
  where(column, operator, value) {
221
- this.dml.where.push({
222
+ const clone = this.clone();
223
+ clone.dml.where.push({
222
224
  column,
223
225
  operator,
224
226
  value,
225
- type: this.nextType,
227
+ type: clone.nextType,
226
228
  isGroup: false
227
229
  });
228
- this.nextType = "AND";
229
- return this;
230
+ clone.nextType = "AND";
231
+ return clone;
230
232
  }
231
233
  orWhere(column, operator, value) {
232
- this.dml.where.push({
234
+ const clone = this.clone();
235
+ clone.dml.where.push({
233
236
  column,
234
237
  operator,
235
238
  value,
236
239
  type: "OR",
237
240
  isGroup: false
238
241
  });
239
- return this;
242
+ return clone;
240
243
  }
241
244
  /**
242
245
  * Adds a grouped WHERE condition to the query.
@@ -251,23 +254,26 @@ var Table = class _Table {
251
254
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
252
255
  */
253
256
  whereGroup(callback) {
254
- const groupQuery = new _Table(this.dml.database, this.dml.table, this.engine);
257
+ const clone = this.clone();
258
+ const groupQuery = new _Table(clone.dml.database, clone.dml.table, clone.engine);
255
259
  callback(groupQuery);
256
- this.dml.where.push({
257
- type: this.nextType,
260
+ clone.dml.where.push({
261
+ type: clone.nextType,
258
262
  isGroup: true,
259
263
  conditions: groupQuery.dml.where
260
264
  });
261
- this.nextType = "AND";
262
- return this;
265
+ clone.nextType = "AND";
266
+ return clone;
263
267
  }
264
268
  or() {
265
- this.nextType = "OR";
266
- return this;
269
+ const clone = this.clone();
270
+ clone.nextType = "OR";
271
+ return clone;
267
272
  }
268
273
  and() {
269
- this.nextType = "AND";
270
- return this;
274
+ const clone = this.clone();
275
+ clone.nextType = "AND";
276
+ return clone;
271
277
  }
272
278
  /**
273
279
  * Adds a WHERE BETWEEN condition to the query.
@@ -281,18 +287,19 @@ var Table = class _Table {
281
287
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
282
288
  */
283
289
  whereBetween(column, values) {
290
+ const clone = this.clone();
284
291
  const [value1, value2] = values;
285
292
  if (value1 !== void 0 && value2 !== void 0) {
286
- this.dml.where.push({
293
+ clone.dml.where.push({
287
294
  column,
288
295
  operator: "BETWEEN",
289
296
  value: [value1, value2],
290
- type: this.nextType,
297
+ type: clone.nextType,
291
298
  isGroup: false
292
299
  });
293
- this.nextType = "AND";
300
+ clone.nextType = "AND";
294
301
  }
295
- return this;
302
+ return clone;
296
303
  }
297
304
  /**
298
305
  * Adds a WHERE IN condition to the query.
@@ -306,17 +313,18 @@ var Table = class _Table {
306
313
  * console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
307
314
  */
308
315
  whereIn(column, values) {
316
+ const clone = this.clone();
309
317
  if (Array.isArray(values) && values.length > 0) {
310
- this.dml.where.push({
318
+ clone.dml.where.push({
311
319
  column,
312
320
  operator: "IN",
313
321
  value: values,
314
- type: this.nextType,
322
+ type: clone.nextType,
315
323
  isGroup: false
316
324
  });
317
- this.nextType = "AND";
325
+ clone.nextType = "AND";
318
326
  }
319
- return this;
327
+ return clone;
320
328
  }
321
329
  /**
322
330
  * Adds a WHERE IS NULL condition to the query.
@@ -329,14 +337,15 @@ var Table = class _Table {
329
337
  * console.log(users); // [{ id: 3, name: 'Alice', email: null }]
330
338
  */
331
339
  whereNull(column) {
332
- this.dml.where.push({
340
+ const clone = this.clone();
341
+ clone.dml.where.push({
333
342
  column,
334
343
  operator: "IS NULL",
335
- type: this.nextType,
344
+ type: clone.nextType,
336
345
  isGroup: false
337
346
  });
338
- this.nextType = "AND";
339
- return this;
347
+ clone.nextType = "AND";
348
+ return clone;
340
349
  }
341
350
  /**
342
351
  * Adds a WHERE IS NOT NULL condition to the query.
@@ -349,14 +358,15 @@ var Table = class _Table {
349
358
  * console.log(users); // [{ id: 1, name: 'John', email: 'john@example.com' }]
350
359
  */
351
360
  whereNotNull(column) {
352
- this.dml.where.push({
361
+ const clone = this.clone();
362
+ clone.dml.where.push({
353
363
  column,
354
364
  operator: "IS NOT NULL",
355
- type: this.nextType,
365
+ type: clone.nextType,
356
366
  isGroup: false
357
367
  });
358
- this.nextType = "AND";
359
- return this;
368
+ clone.nextType = "AND";
369
+ return clone;
360
370
  }
361
371
  /**
362
372
  * Adds a JOIN clause to the query.
@@ -372,7 +382,8 @@ var Table = class _Table {
372
382
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }]
373
383
  */
374
384
  join(table, column1, operator, column2) {
375
- this.dml.joins.push({
385
+ const clone = this.clone();
386
+ clone.dml.joins.push({
376
387
  type: "INNER",
377
388
  table,
378
389
  on: {
@@ -381,7 +392,7 @@ var Table = class _Table {
381
392
  column2
382
393
  }
383
394
  });
384
- return this;
395
+ return clone;
385
396
  }
386
397
  /**
387
398
  * Adds a LEFT JOIN clause to the query.
@@ -397,7 +408,8 @@ var Table = class _Table {
397
408
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: 2, name: 'Jane', order_id: null }]
398
409
  */
399
410
  leftJoin(table, column1, operator, column2) {
400
- this.dml.joins.push({
411
+ const clone = this.clone();
412
+ clone.dml.joins.push({
401
413
  type: "LEFT",
402
414
  table,
403
415
  on: {
@@ -406,7 +418,7 @@ var Table = class _Table {
406
418
  column2
407
419
  }
408
420
  });
409
- return this;
421
+ return clone;
410
422
  }
411
423
  /**
412
424
  * Adds a RIGHT JOIN clause to the query.
@@ -422,7 +434,8 @@ var Table = class _Table {
422
434
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: null, name: null, order_id: 102 }]
423
435
  */
424
436
  rightJoin(table, column1, operator, column2) {
425
- this.dml.joins.push({
437
+ const clone = this.clone();
438
+ clone.dml.joins.push({
426
439
  type: "RIGHT",
427
440
  table,
428
441
  on: {
@@ -431,7 +444,7 @@ var Table = class _Table {
431
444
  column2
432
445
  }
433
446
  });
434
- return this;
447
+ return clone;
435
448
  }
436
449
  /**
437
450
  * Adds an ORDER BY clause to the query.
@@ -445,16 +458,17 @@ var Table = class _Table {
445
458
  * console.log(users); // [{ id: 2, name: 'Jane' }, { id: 1, name: 'John' }]
446
459
  */
447
460
  orderBy(column, direction = "ASC") {
461
+ const clone = this.clone();
448
462
  const validDirections = ["ASC", "DESC"];
449
463
  if (validDirections.includes(direction.toUpperCase())) {
450
- this.dml.orderBy.push({
464
+ clone.dml.orderBy.push({
451
465
  column,
452
466
  direction: direction.toUpperCase()
453
467
  });
454
468
  } else {
455
469
  throw new Error(`Invalid direction: ${direction}. Use 'ASC' or 'DESC'.`);
456
470
  }
457
- return this;
471
+ return clone;
458
472
  }
459
473
  /**
460
474
  * Adds a GROUP BY clause to the query.
@@ -467,8 +481,9 @@ var Table = class _Table {
467
481
  * console.log(users); // [{ age: 30, count: 1 }, { age: 25, count: 1 }]
468
482
  */
469
483
  groupBy(column) {
470
- this.dml.groupBy.push(column);
471
- return this;
484
+ const clone = this.clone();
485
+ clone.dml.groupBy.push(column);
486
+ return clone;
472
487
  }
473
488
  /**
474
489
  * Adds a DISTINCT clause to the query.
@@ -480,8 +495,9 @@ var Table = class _Table {
480
495
  * console.log(users); // [{ name: 'John' }, { name: 'Jane' }]
481
496
  */
482
497
  distinct() {
483
- this.dml.distinct = true;
484
- return this;
498
+ const clone = this.clone();
499
+ clone.dml.distinct = true;
500
+ return clone;
485
501
  }
486
502
  /**
487
503
  * Adds a COUNT clause to the query.
@@ -494,18 +510,20 @@ var Table = class _Table {
494
510
  * console.log(count); // { count: 2 }
495
511
  */
496
512
  async count(column = "*") {
497
- const clonedDML = this.cloneDML();
498
- clonedDML.type = "select";
499
- clonedDML.aggregation = {
500
- type: "COUNT",
501
- column,
502
- alias: "count"
513
+ const executionDML = {
514
+ ...this.dml,
515
+ type: "select",
516
+ aggregation: {
517
+ type: "COUNT",
518
+ column,
519
+ alias: "count"
520
+ },
521
+ columns: [`COUNT(${column}) AS count`],
522
+ data: null,
523
+ limit: 1
503
524
  };
504
- clonedDML.columns = [`COUNT(${column}) AS count`];
505
- clonedDML.data = null;
506
- clonedDML.limit = 1;
507
525
  try {
508
- const result = await this.getResponse(clonedDML);
526
+ const result = await this.getResponse(executionDML);
509
527
  const res = result[0] || null;
510
528
  if (res) {
511
529
  return res.count;
@@ -526,18 +544,20 @@ var Table = class _Table {
526
544
  * console.log(totalAge); // { sum: 55 }
527
545
  */
528
546
  async sum(column) {
529
- const clonedDML = this.cloneDML();
530
- clonedDML.type = "select";
531
- clonedDML.aggregation = {
532
- type: "SUM",
533
- column,
534
- alias: "sum"
547
+ const executionDML = {
548
+ ...this.dml,
549
+ type: "select",
550
+ aggregation: {
551
+ type: "SUM",
552
+ column,
553
+ alias: "sum"
554
+ },
555
+ columns: [`SUM(${column}) AS sum`],
556
+ data: null,
557
+ limit: 1
535
558
  };
536
- clonedDML.columns = [`SUM(${column}) AS sum`];
537
- clonedDML.data = null;
538
- clonedDML.limit = 1;
539
559
  try {
540
- const result = await this.getResponse(clonedDML);
560
+ const result = await this.getResponse(executionDML);
541
561
  const res = result[0] || null;
542
562
  if (res) {
543
563
  return res.sum;
@@ -558,18 +578,20 @@ var Table = class _Table {
558
578
  * console.log(avgAge); // { avg: 27.5 }
559
579
  */
560
580
  async avg(column) {
561
- const clonedDML = this.cloneDML();
562
- clonedDML.type = "select";
563
- clonedDML.aggregation = {
564
- type: "AVG",
565
- column,
566
- alias: "avg"
581
+ const executionDML = {
582
+ ...this.dml,
583
+ type: "select",
584
+ aggregation: {
585
+ type: "AVG",
586
+ column,
587
+ alias: "avg"
588
+ },
589
+ columns: [`AVG(${column}) AS avg`],
590
+ data: null,
591
+ limit: 1
567
592
  };
568
- clonedDML.columns = [`AVG(${column}) AS avg`];
569
- clonedDML.data = null;
570
- clonedDML.limit = 1;
571
593
  try {
572
- const result = await this.getResponse(clonedDML);
594
+ const result = await this.getResponse(executionDML);
573
595
  const res = result[0] || null;
574
596
  if (res) {
575
597
  return res.avg;
@@ -590,18 +612,20 @@ var Table = class _Table {
590
612
  * console.log(maxAge); // { max: 30 }
591
613
  */
592
614
  async max(column) {
593
- const clonedDML = this.cloneDML();
594
- clonedDML.type = "select";
595
- clonedDML.aggregation = {
596
- type: "MAX",
597
- column,
598
- alias: "max"
615
+ const executionDML = {
616
+ ...this.dml,
617
+ type: "select",
618
+ aggregation: {
619
+ type: "MAX",
620
+ column,
621
+ alias: "max"
622
+ },
623
+ columns: [`MAX(${column}) AS max`],
624
+ data: null,
625
+ limit: 1
599
626
  };
600
- clonedDML.columns = [`MAX(${column}) AS max`];
601
- clonedDML.data = null;
602
- clonedDML.limit = 1;
603
627
  try {
604
- const result = await this.getResponse(clonedDML);
628
+ const result = await this.getResponse(executionDML);
605
629
  const res = result[0] || null;
606
630
  if (res) {
607
631
  return res.max;
@@ -622,18 +646,20 @@ var Table = class _Table {
622
646
  * console.log(minAge); // { min: 25 }
623
647
  */
624
648
  async min(column) {
625
- const clonedDML = this.cloneDML();
626
- clonedDML.type = "select";
627
- clonedDML.aggregation = {
628
- type: "MIN",
629
- column,
630
- alias: "min"
649
+ const executionDML = {
650
+ ...this.dml,
651
+ type: "select",
652
+ aggregation: {
653
+ type: "MIN",
654
+ column,
655
+ alias: "min"
656
+ },
657
+ columns: [`MIN(${column}) AS min`],
658
+ data: null,
659
+ limit: 1
631
660
  };
632
- clonedDML.columns = [`MIN(${column}) AS min`];
633
- clonedDML.data = null;
634
- clonedDML.limit = 1;
635
661
  try {
636
- const result = await this.getResponse(clonedDML);
662
+ const result = await this.getResponse(executionDML);
637
663
  const res = result[0] || null;
638
664
  if (res) {
639
665
  return res.min;
@@ -654,8 +680,9 @@ var Table = class _Table {
654
680
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }]
655
681
  */
656
682
  limit(number) {
657
- this.dml.limit = number;
658
- return this;
683
+ const clone = this.clone();
684
+ clone.dml.limit = number;
685
+ return clone;
659
686
  }
660
687
  /**
661
688
  * Adds pagination to the query using LIMIT and OFFSET.
@@ -668,10 +695,11 @@ var Table = class _Table {
668
695
  * console.log(users); // [{ id: 2, name: 'Jane', age: 25 }]
669
696
  */
670
697
  page(number) {
671
- if (this.dml.limit) {
672
- this.dml.offset = (number - 1) * this.dml.limit;
698
+ const clone = this.clone();
699
+ if (clone.dml.limit) {
700
+ clone.dml.offset = (number - 1) * clone.dml.limit;
673
701
  }
674
- return this;
702
+ return clone;
675
703
  }
676
704
  /**
677
705
  * Executes the query and returns all matching rows.
@@ -684,10 +712,12 @@ var Table = class _Table {
684
712
  */
685
713
  async get() {
686
714
  try {
687
- const clonedDML = this.cloneDML();
688
- clonedDML.type = "select";
689
- clonedDML.data = null;
690
- const result = await this.getResponse(clonedDML);
715
+ const executionDML = {
716
+ ...this.dml,
717
+ type: "select",
718
+ data: null
719
+ };
720
+ const result = await this.getResponse(executionDML);
691
721
  return result;
692
722
  } catch (error) {
693
723
  throw error;
@@ -703,12 +733,14 @@ var Table = class _Table {
703
733
  * console.log(user); // { id: 1, name: 'John' }
704
734
  */
705
735
  async first() {
706
- const clonedDML = this.cloneDML();
707
- clonedDML.type = "select";
708
- clonedDML.data = null;
709
- clonedDML.limit = 1;
736
+ const executionDML = {
737
+ ...this.dml,
738
+ type: "select",
739
+ data: null,
740
+ limit: 1
741
+ };
710
742
  try {
711
- const result = await this.getResponse(clonedDML);
743
+ const result = await this.getResponse(executionDML);
712
744
  return result[0] || null;
713
745
  } catch (error) {
714
746
  throw error;
@@ -726,19 +758,21 @@ var Table = class _Table {
726
758
  * console.log(user); // { id: 1, name: 'John' }
727
759
  */
728
760
  async find(value, column = "id") {
729
- const clonedDML = this.cloneDML();
730
- clonedDML.type = "select";
731
- clonedDML.data = null;
732
- clonedDML.where.push({
733
- column,
734
- operator: "=",
735
- value,
736
- type: "AND",
737
- isGroup: false
738
- });
739
- clonedDML.limit = 1;
761
+ const executionDML = {
762
+ ...this.dml,
763
+ type: "select",
764
+ data: null,
765
+ where: [...this.dml.where, {
766
+ column,
767
+ operator: "=",
768
+ value,
769
+ type: "AND",
770
+ isGroup: false
771
+ }],
772
+ limit: 1
773
+ };
740
774
  try {
741
- const result = await this.getResponse(clonedDML);
775
+ const result = await this.getResponse(executionDML);
742
776
  return result[0] || null;
743
777
  } catch (error) {
744
778
  throw error;
@@ -764,10 +798,12 @@ var Table = class _Table {
764
798
  if (!data.every((item) => typeof item === "object" && item !== null)) {
765
799
  throw new Error("The array must contain only valid objects.");
766
800
  }
767
- const clonedDML = this.cloneDML();
768
- clonedDML.type = "insert";
769
- clonedDML.data = data;
770
- await this.getResponse(clonedDML, "Add");
801
+ const executionDML = {
802
+ ...this.dml,
803
+ type: "insert",
804
+ data
805
+ };
806
+ await this.getResponse(executionDML, "Add");
771
807
  return data;
772
808
  }
773
809
  /**
@@ -789,10 +825,12 @@ var Table = class _Table {
789
825
  if (this.dml.where.length === 0) {
790
826
  throw new Error("You must specify at least one WHERE condition to perform an update.");
791
827
  }
792
- const clonedDML = this.cloneDML();
793
- clonedDML.type = "update";
794
- clonedDML.data = data;
795
- await this.getResponse(clonedDML, "Update");
828
+ const executionDML = {
829
+ ...this.dml,
830
+ type: "update",
831
+ data
832
+ };
833
+ await this.getResponse(executionDML, "Update");
796
834
  return data;
797
835
  }
798
836
  /**
@@ -808,9 +846,11 @@ var Table = class _Table {
808
846
  if (this.dml.where.length === 0) {
809
847
  throw new Error("You must specify at least one WHERE condition to perform a delete.");
810
848
  }
811
- const clonedDML = this.cloneDML();
812
- clonedDML.type = "delete";
813
- const deleteData = await this.getResponse(clonedDML, "Delete");
849
+ const executionDML = {
850
+ ...this.dml,
851
+ type: "delete"
852
+ };
853
+ const deleteData = await this.getResponse(executionDML, "Delete");
814
854
  return deleteData;
815
855
  }
816
856
  async getResponse(dml = null, type = null) {
@@ -904,8 +944,14 @@ var Table = class _Table {
904
944
  }
905
945
  return arrayResult;
906
946
  }
907
- cloneDML() {
908
- return {
947
+ clone() {
948
+ const cloned = Object.create(Object.getPrototypeOf(this));
949
+ cloned.engine = this.engine;
950
+ cloned.nextType = this.nextType;
951
+ cloned.computedFields = this.computedFields;
952
+ cloned.trigger = this.trigger;
953
+ cloned.triggers = this.triggers;
954
+ cloned.dml = {
909
955
  ...this.dml,
910
956
  columns: [...this.dml.columns],
911
957
  joins: [...this.dml.joins],
@@ -913,22 +959,7 @@ var Table = class _Table {
913
959
  orderBy: [...this.dml.orderBy],
914
960
  groupBy: [...this.dml.groupBy]
915
961
  };
916
- }
917
- reset() {
918
- this.dml = {
919
- ...this.dml,
920
- type: "select",
921
- columns: ["*"],
922
- distinct: false,
923
- joins: [],
924
- where: [],
925
- orderBy: [],
926
- groupBy: [],
927
- limit: null,
928
- offset: null,
929
- data: null,
930
- aggregation: null
931
- };
962
+ return cloned;
932
963
  }
933
964
  };
934
965
  function returnFormattedError(status, message) {