@dbcube/query-builder 3.0.27 → 3.0.28

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,17 @@ 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 = {
513
+ this.dml.type = "select";
514
+ this.dml.aggregation = {
500
515
  type: "COUNT",
501
516
  column,
502
517
  alias: "count"
503
518
  };
504
- clonedDML.columns = [`COUNT(${column}) AS count`];
505
- clonedDML.data = null;
506
- clonedDML.limit = 1;
519
+ this.dml.columns = [`COUNT(${column}) AS count`];
520
+ this.dml.data = null;
521
+ this.dml.limit = 1;
507
522
  try {
508
- const result = await this.getResponse(clonedDML);
523
+ const result = await this.getResponse();
509
524
  const res = result[0] || null;
510
525
  if (res) {
511
526
  return res.count;
@@ -526,18 +541,17 @@ var Table = class _Table {
526
541
  * console.log(totalAge); // { sum: 55 }
527
542
  */
528
543
  async sum(column) {
529
- const clonedDML = this.cloneDML();
530
- clonedDML.type = "select";
531
- clonedDML.aggregation = {
544
+ this.dml.type = "select";
545
+ this.dml.aggregation = {
532
546
  type: "SUM",
533
547
  column,
534
548
  alias: "sum"
535
549
  };
536
- clonedDML.columns = [`SUM(${column}) AS sum`];
537
- clonedDML.data = null;
538
- clonedDML.limit = 1;
550
+ this.dml.columns = [`SUM(${column}) AS sum`];
551
+ this.dml.data = null;
552
+ this.dml.limit = 1;
539
553
  try {
540
- const result = await this.getResponse(clonedDML);
554
+ const result = await this.getResponse();
541
555
  const res = result[0] || null;
542
556
  if (res) {
543
557
  return res.sum;
@@ -558,18 +572,17 @@ var Table = class _Table {
558
572
  * console.log(avgAge); // { avg: 27.5 }
559
573
  */
560
574
  async avg(column) {
561
- const clonedDML = this.cloneDML();
562
- clonedDML.type = "select";
563
- clonedDML.aggregation = {
575
+ this.dml.type = "select";
576
+ this.dml.aggregation = {
564
577
  type: "AVG",
565
578
  column,
566
579
  alias: "avg"
567
580
  };
568
- clonedDML.columns = [`AVG(${column}) AS avg`];
569
- clonedDML.data = null;
570
- clonedDML.limit = 1;
581
+ this.dml.columns = [`AVG(${column}) AS avg`];
582
+ this.dml.data = null;
583
+ this.dml.limit = 1;
571
584
  try {
572
- const result = await this.getResponse(clonedDML);
585
+ const result = await this.getResponse();
573
586
  const res = result[0] || null;
574
587
  if (res) {
575
588
  return res.avg;
@@ -590,18 +603,17 @@ var Table = class _Table {
590
603
  * console.log(maxAge); // { max: 30 }
591
604
  */
592
605
  async max(column) {
593
- const clonedDML = this.cloneDML();
594
- clonedDML.type = "select";
595
- clonedDML.aggregation = {
606
+ this.dml.type = "select";
607
+ this.dml.aggregation = {
596
608
  type: "MAX",
597
609
  column,
598
610
  alias: "max"
599
611
  };
600
- clonedDML.columns = [`MAX(${column}) AS max`];
601
- clonedDML.data = null;
602
- clonedDML.limit = 1;
612
+ this.dml.columns = [`MAX(${column}) AS max`];
613
+ this.dml.data = null;
614
+ this.dml.limit = 1;
603
615
  try {
604
- const result = await this.getResponse(clonedDML);
616
+ const result = await this.getResponse();
605
617
  const res = result[0] || null;
606
618
  if (res) {
607
619
  return res.max;
@@ -622,18 +634,17 @@ var Table = class _Table {
622
634
  * console.log(minAge); // { min: 25 }
623
635
  */
624
636
  async min(column) {
625
- const clonedDML = this.cloneDML();
626
- clonedDML.type = "select";
627
- clonedDML.aggregation = {
637
+ this.dml.type = "select";
638
+ this.dml.aggregation = {
628
639
  type: "MIN",
629
640
  column,
630
641
  alias: "min"
631
642
  };
632
- clonedDML.columns = [`MIN(${column}) AS min`];
633
- clonedDML.data = null;
634
- clonedDML.limit = 1;
643
+ this.dml.columns = [`MIN(${column}) AS min`];
644
+ this.dml.data = null;
645
+ this.dml.limit = 1;
635
646
  try {
636
- const result = await this.getResponse(clonedDML);
647
+ const result = await this.getResponse();
637
648
  const res = result[0] || null;
638
649
  if (res) {
639
650
  return res.min;
@@ -654,8 +665,9 @@ var Table = class _Table {
654
665
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }]
655
666
  */
656
667
  limit(number) {
657
- this.dml.limit = number;
658
- return this;
668
+ const clone = this.clone();
669
+ clone.dml.limit = number;
670
+ return clone;
659
671
  }
660
672
  /**
661
673
  * Adds pagination to the query using LIMIT and OFFSET.
@@ -668,10 +680,11 @@ var Table = class _Table {
668
680
  * console.log(users); // [{ id: 2, name: 'Jane', age: 25 }]
669
681
  */
670
682
  page(number) {
671
- if (this.dml.limit) {
672
- this.dml.offset = (number - 1) * this.dml.limit;
683
+ const clone = this.clone();
684
+ if (clone.dml.limit) {
685
+ clone.dml.offset = (number - 1) * clone.dml.limit;
673
686
  }
674
- return this;
687
+ return clone;
675
688
  }
676
689
  /**
677
690
  * Executes the query and returns all matching rows.
@@ -684,10 +697,9 @@ var Table = class _Table {
684
697
  */
685
698
  async get() {
686
699
  try {
687
- const clonedDML = this.cloneDML();
688
- clonedDML.type = "select";
689
- clonedDML.data = null;
690
- const result = await this.getResponse(clonedDML);
700
+ this.dml.type = "select";
701
+ this.dml.data = null;
702
+ const result = await this.getResponse();
691
703
  return result;
692
704
  } catch (error) {
693
705
  throw error;
@@ -703,12 +715,11 @@ var Table = class _Table {
703
715
  * console.log(user); // { id: 1, name: 'John' }
704
716
  */
705
717
  async first() {
706
- const clonedDML = this.cloneDML();
707
- clonedDML.type = "select";
708
- clonedDML.data = null;
709
- clonedDML.limit = 1;
718
+ this.dml.type = "select";
719
+ this.dml.data = null;
720
+ this.dml.limit = 1;
710
721
  try {
711
- const result = await this.getResponse(clonedDML);
722
+ const result = await this.getResponse();
712
723
  return result[0] || null;
713
724
  } catch (error) {
714
725
  throw error;
@@ -726,19 +737,12 @@ var Table = class _Table {
726
737
  * console.log(user); // { id: 1, name: 'John' }
727
738
  */
728
739
  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;
740
+ this.dml.type = "select";
741
+ this.dml.data = null;
742
+ this.where(column, "=", value);
743
+ this.dml.limit = 1;
740
744
  try {
741
- const result = await this.getResponse(clonedDML);
745
+ const result = await this.getResponse();
742
746
  return result[0] || null;
743
747
  } catch (error) {
744
748
  throw error;
@@ -764,10 +768,9 @@ var Table = class _Table {
764
768
  if (!data.every((item) => typeof item === "object" && item !== null)) {
765
769
  throw new Error("The array must contain only valid objects.");
766
770
  }
767
- const clonedDML = this.cloneDML();
768
- clonedDML.type = "insert";
769
- clonedDML.data = data;
770
- await this.getResponse(clonedDML, "Add");
771
+ this.dml.type = "insert";
772
+ this.dml.data = data;
773
+ await this.getResponse(this.dml, "Add");
771
774
  return data;
772
775
  }
773
776
  /**
@@ -789,10 +792,9 @@ var Table = class _Table {
789
792
  if (this.dml.where.length === 0) {
790
793
  throw new Error("You must specify at least one WHERE condition to perform an update.");
791
794
  }
792
- const clonedDML = this.cloneDML();
793
- clonedDML.type = "update";
794
- clonedDML.data = data;
795
- await this.getResponse(clonedDML, "Update");
795
+ this.dml.type = "update";
796
+ this.dml.data = data;
797
+ await this.getResponse(this.dml, "Update");
796
798
  return data;
797
799
  }
798
800
  /**
@@ -808,9 +810,8 @@ var Table = class _Table {
808
810
  if (this.dml.where.length === 0) {
809
811
  throw new Error("You must specify at least one WHERE condition to perform a delete.");
810
812
  }
811
- const clonedDML = this.cloneDML();
812
- clonedDML.type = "delete";
813
- const deleteData = await this.getResponse(clonedDML, "Delete");
813
+ this.dml.type = "delete";
814
+ const deleteData = await this.getResponse(this.dml, "Delete");
814
815
  return deleteData;
815
816
  }
816
817
  async getResponse(dml = null, type = null) {
@@ -904,8 +905,14 @@ var Table = class _Table {
904
905
  }
905
906
  return arrayResult;
906
907
  }
907
- cloneDML() {
908
- return {
908
+ clone() {
909
+ const cloned = Object.create(Object.getPrototypeOf(this));
910
+ cloned.engine = this.engine;
911
+ cloned.nextType = this.nextType;
912
+ cloned.computedFields = this.computedFields;
913
+ cloned.trigger = this.trigger;
914
+ cloned.triggers = this.triggers;
915
+ cloned.dml = {
909
916
  ...this.dml,
910
917
  columns: [...this.dml.columns],
911
918
  joins: [...this.dml.joins],
@@ -913,22 +920,7 @@ var Table = class _Table {
913
920
  orderBy: [...this.dml.orderBy],
914
921
  groupBy: [...this.dml.groupBy]
915
922
  };
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
- };
923
+ return cloned;
932
924
  }
933
925
  };
934
926
  function returnFormattedError(status, message) {