@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/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,17 @@ 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 = {
475
+ this.dml.type = "select";
476
+ this.dml.aggregation = {
462
477
  type: "COUNT",
463
478
  column,
464
479
  alias: "count"
465
480
  };
466
- clonedDML.columns = [`COUNT(${column}) AS count`];
467
- clonedDML.data = null;
468
- clonedDML.limit = 1;
481
+ this.dml.columns = [`COUNT(${column}) AS count`];
482
+ this.dml.data = null;
483
+ this.dml.limit = 1;
469
484
  try {
470
- const result = await this.getResponse(clonedDML);
485
+ const result = await this.getResponse();
471
486
  const res = result[0] || null;
472
487
  if (res) {
473
488
  return res.count;
@@ -488,18 +503,17 @@ var Table = class _Table {
488
503
  * console.log(totalAge); // { sum: 55 }
489
504
  */
490
505
  async sum(column) {
491
- const clonedDML = this.cloneDML();
492
- clonedDML.type = "select";
493
- clonedDML.aggregation = {
506
+ this.dml.type = "select";
507
+ this.dml.aggregation = {
494
508
  type: "SUM",
495
509
  column,
496
510
  alias: "sum"
497
511
  };
498
- clonedDML.columns = [`SUM(${column}) AS sum`];
499
- clonedDML.data = null;
500
- clonedDML.limit = 1;
512
+ this.dml.columns = [`SUM(${column}) AS sum`];
513
+ this.dml.data = null;
514
+ this.dml.limit = 1;
501
515
  try {
502
- const result = await this.getResponse(clonedDML);
516
+ const result = await this.getResponse();
503
517
  const res = result[0] || null;
504
518
  if (res) {
505
519
  return res.sum;
@@ -520,18 +534,17 @@ var Table = class _Table {
520
534
  * console.log(avgAge); // { avg: 27.5 }
521
535
  */
522
536
  async avg(column) {
523
- const clonedDML = this.cloneDML();
524
- clonedDML.type = "select";
525
- clonedDML.aggregation = {
537
+ this.dml.type = "select";
538
+ this.dml.aggregation = {
526
539
  type: "AVG",
527
540
  column,
528
541
  alias: "avg"
529
542
  };
530
- clonedDML.columns = [`AVG(${column}) AS avg`];
531
- clonedDML.data = null;
532
- clonedDML.limit = 1;
543
+ this.dml.columns = [`AVG(${column}) AS avg`];
544
+ this.dml.data = null;
545
+ this.dml.limit = 1;
533
546
  try {
534
- const result = await this.getResponse(clonedDML);
547
+ const result = await this.getResponse();
535
548
  const res = result[0] || null;
536
549
  if (res) {
537
550
  return res.avg;
@@ -552,18 +565,17 @@ var Table = class _Table {
552
565
  * console.log(maxAge); // { max: 30 }
553
566
  */
554
567
  async max(column) {
555
- const clonedDML = this.cloneDML();
556
- clonedDML.type = "select";
557
- clonedDML.aggregation = {
568
+ this.dml.type = "select";
569
+ this.dml.aggregation = {
558
570
  type: "MAX",
559
571
  column,
560
572
  alias: "max"
561
573
  };
562
- clonedDML.columns = [`MAX(${column}) AS max`];
563
- clonedDML.data = null;
564
- clonedDML.limit = 1;
574
+ this.dml.columns = [`MAX(${column}) AS max`];
575
+ this.dml.data = null;
576
+ this.dml.limit = 1;
565
577
  try {
566
- const result = await this.getResponse(clonedDML);
578
+ const result = await this.getResponse();
567
579
  const res = result[0] || null;
568
580
  if (res) {
569
581
  return res.max;
@@ -584,18 +596,17 @@ var Table = class _Table {
584
596
  * console.log(minAge); // { min: 25 }
585
597
  */
586
598
  async min(column) {
587
- const clonedDML = this.cloneDML();
588
- clonedDML.type = "select";
589
- clonedDML.aggregation = {
599
+ this.dml.type = "select";
600
+ this.dml.aggregation = {
590
601
  type: "MIN",
591
602
  column,
592
603
  alias: "min"
593
604
  };
594
- clonedDML.columns = [`MIN(${column}) AS min`];
595
- clonedDML.data = null;
596
- clonedDML.limit = 1;
605
+ this.dml.columns = [`MIN(${column}) AS min`];
606
+ this.dml.data = null;
607
+ this.dml.limit = 1;
597
608
  try {
598
- const result = await this.getResponse(clonedDML);
609
+ const result = await this.getResponse();
599
610
  const res = result[0] || null;
600
611
  if (res) {
601
612
  return res.min;
@@ -616,8 +627,9 @@ var Table = class _Table {
616
627
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }]
617
628
  */
618
629
  limit(number) {
619
- this.dml.limit = number;
620
- return this;
630
+ const clone = this.clone();
631
+ clone.dml.limit = number;
632
+ return clone;
621
633
  }
622
634
  /**
623
635
  * Adds pagination to the query using LIMIT and OFFSET.
@@ -630,10 +642,11 @@ var Table = class _Table {
630
642
  * console.log(users); // [{ id: 2, name: 'Jane', age: 25 }]
631
643
  */
632
644
  page(number) {
633
- if (this.dml.limit) {
634
- this.dml.offset = (number - 1) * this.dml.limit;
645
+ const clone = this.clone();
646
+ if (clone.dml.limit) {
647
+ clone.dml.offset = (number - 1) * clone.dml.limit;
635
648
  }
636
- return this;
649
+ return clone;
637
650
  }
638
651
  /**
639
652
  * Executes the query and returns all matching rows.
@@ -646,10 +659,9 @@ var Table = class _Table {
646
659
  */
647
660
  async get() {
648
661
  try {
649
- const clonedDML = this.cloneDML();
650
- clonedDML.type = "select";
651
- clonedDML.data = null;
652
- const result = await this.getResponse(clonedDML);
662
+ this.dml.type = "select";
663
+ this.dml.data = null;
664
+ const result = await this.getResponse();
653
665
  return result;
654
666
  } catch (error) {
655
667
  throw error;
@@ -665,12 +677,11 @@ var Table = class _Table {
665
677
  * console.log(user); // { id: 1, name: 'John' }
666
678
  */
667
679
  async first() {
668
- const clonedDML = this.cloneDML();
669
- clonedDML.type = "select";
670
- clonedDML.data = null;
671
- clonedDML.limit = 1;
680
+ this.dml.type = "select";
681
+ this.dml.data = null;
682
+ this.dml.limit = 1;
672
683
  try {
673
- const result = await this.getResponse(clonedDML);
684
+ const result = await this.getResponse();
674
685
  return result[0] || null;
675
686
  } catch (error) {
676
687
  throw error;
@@ -688,19 +699,12 @@ var Table = class _Table {
688
699
  * console.log(user); // { id: 1, name: 'John' }
689
700
  */
690
701
  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;
702
+ this.dml.type = "select";
703
+ this.dml.data = null;
704
+ this.where(column, "=", value);
705
+ this.dml.limit = 1;
702
706
  try {
703
- const result = await this.getResponse(clonedDML);
707
+ const result = await this.getResponse();
704
708
  return result[0] || null;
705
709
  } catch (error) {
706
710
  throw error;
@@ -726,10 +730,9 @@ var Table = class _Table {
726
730
  if (!data.every((item) => typeof item === "object" && item !== null)) {
727
731
  throw new Error("The array must contain only valid objects.");
728
732
  }
729
- const clonedDML = this.cloneDML();
730
- clonedDML.type = "insert";
731
- clonedDML.data = data;
732
- await this.getResponse(clonedDML, "Add");
733
+ this.dml.type = "insert";
734
+ this.dml.data = data;
735
+ await this.getResponse(this.dml, "Add");
733
736
  return data;
734
737
  }
735
738
  /**
@@ -751,10 +754,9 @@ var Table = class _Table {
751
754
  if (this.dml.where.length === 0) {
752
755
  throw new Error("You must specify at least one WHERE condition to perform an update.");
753
756
  }
754
- const clonedDML = this.cloneDML();
755
- clonedDML.type = "update";
756
- clonedDML.data = data;
757
- await this.getResponse(clonedDML, "Update");
757
+ this.dml.type = "update";
758
+ this.dml.data = data;
759
+ await this.getResponse(this.dml, "Update");
758
760
  return data;
759
761
  }
760
762
  /**
@@ -770,9 +772,8 @@ var Table = class _Table {
770
772
  if (this.dml.where.length === 0) {
771
773
  throw new Error("You must specify at least one WHERE condition to perform a delete.");
772
774
  }
773
- const clonedDML = this.cloneDML();
774
- clonedDML.type = "delete";
775
- const deleteData = await this.getResponse(clonedDML, "Delete");
775
+ this.dml.type = "delete";
776
+ const deleteData = await this.getResponse(this.dml, "Delete");
776
777
  return deleteData;
777
778
  }
778
779
  async getResponse(dml = null, type = null) {
@@ -866,8 +867,14 @@ var Table = class _Table {
866
867
  }
867
868
  return arrayResult;
868
869
  }
869
- cloneDML() {
870
- return {
870
+ clone() {
871
+ const cloned = Object.create(Object.getPrototypeOf(this));
872
+ cloned.engine = this.engine;
873
+ cloned.nextType = this.nextType;
874
+ cloned.computedFields = this.computedFields;
875
+ cloned.trigger = this.trigger;
876
+ cloned.triggers = this.triggers;
877
+ cloned.dml = {
871
878
  ...this.dml,
872
879
  columns: [...this.dml.columns],
873
880
  joins: [...this.dml.joins],
@@ -875,22 +882,7 @@ var Table = class _Table {
875
882
  orderBy: [...this.dml.orderBy],
876
883
  groupBy: [...this.dml.groupBy]
877
884
  };
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
- };
885
+ return cloned;
894
886
  }
895
887
  };
896
888
  function returnFormattedError(status, message) {