@dbcube/query-builder 3.0.35 → 3.0.36

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.d.mts CHANGED
@@ -255,7 +255,7 @@ declare class Table {
255
255
  * const count = await db.table('users').count().first();
256
256
  * console.log(count); // { count: 2 }
257
257
  */
258
- count(column?: string): Promise<number>;
258
+ count(column?: string): Promise<Number>;
259
259
  /**
260
260
  * Adds a SUM clause to the query.
261
261
  *
@@ -266,7 +266,7 @@ declare class Table {
266
266
  * const totalAge = await db.table('users').sum('age').first();
267
267
  * console.log(totalAge); // { sum: 55 }
268
268
  */
269
- sum(column: string): Promise<number>;
269
+ sum(column: string): Promise<Number>;
270
270
  /**
271
271
  * Adds an AVG clause to the query.
272
272
  *
@@ -277,7 +277,7 @@ declare class Table {
277
277
  * const avgAge = await db.table('users').avg('age').first();
278
278
  * console.log(avgAge); // { avg: 27.5 }
279
279
  */
280
- avg(column: string): Promise<number>;
280
+ avg(column: string): Promise<Number>;
281
281
  /**
282
282
  * Adds a MAX clause to the query.
283
283
  *
@@ -288,7 +288,7 @@ declare class Table {
288
288
  * const maxAge = await db.table('users').max('age').first();
289
289
  * console.log(maxAge); // { max: 30 }
290
290
  */
291
- max(column: string): Promise<number>;
291
+ max(column: string): Promise<Number>;
292
292
  /**
293
293
  * Adds a MIN clause to the query.
294
294
  *
@@ -299,7 +299,7 @@ declare class Table {
299
299
  * const minAge = await db.table('users').min('age').first();
300
300
  * console.log(minAge); // { min: 25 }
301
301
  */
302
- min(column: string): Promise<number>;
302
+ min(column: string): Promise<Number>;
303
303
  /**
304
304
  * Adds a LIMIT clause to the query.
305
305
  *
package/dist/index.d.ts CHANGED
@@ -255,7 +255,7 @@ declare class Table {
255
255
  * const count = await db.table('users').count().first();
256
256
  * console.log(count); // { count: 2 }
257
257
  */
258
- count(column?: string): Promise<number>;
258
+ count(column?: string): Promise<Number>;
259
259
  /**
260
260
  * Adds a SUM clause to the query.
261
261
  *
@@ -266,7 +266,7 @@ declare class Table {
266
266
  * const totalAge = await db.table('users').sum('age').first();
267
267
  * console.log(totalAge); // { sum: 55 }
268
268
  */
269
- sum(column: string): Promise<number>;
269
+ sum(column: string): Promise<Number>;
270
270
  /**
271
271
  * Adds an AVG clause to the query.
272
272
  *
@@ -277,7 +277,7 @@ declare class Table {
277
277
  * const avgAge = await db.table('users').avg('age').first();
278
278
  * console.log(avgAge); // { avg: 27.5 }
279
279
  */
280
- avg(column: string): Promise<number>;
280
+ avg(column: string): Promise<Number>;
281
281
  /**
282
282
  * Adds a MAX clause to the query.
283
283
  *
@@ -288,7 +288,7 @@ declare class Table {
288
288
  * const maxAge = await db.table('users').max('age').first();
289
289
  * console.log(maxAge); // { max: 30 }
290
290
  */
291
- max(column: string): Promise<number>;
291
+ max(column: string): Promise<Number>;
292
292
  /**
293
293
  * Adds a MIN clause to the query.
294
294
  *
@@ -299,7 +299,7 @@ declare class Table {
299
299
  * const minAge = await db.table('users').min('age').first();
300
300
  * console.log(minAge); // { min: 25 }
301
301
  */
302
- min(column: string): Promise<number>;
302
+ min(column: string): Promise<Number>;
303
303
  /**
304
304
  * Adds a LIMIT clause to the query.
305
305
  *
package/dist/index.js CHANGED
@@ -472,20 +472,18 @@ var Table = class _Table {
472
472
  * console.log(count); // { count: 2 }
473
473
  */
474
474
  async count(column = "*") {
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
475
+ const clone = this.clone();
476
+ clone.dml.type = "select";
477
+ clone.dml.aggregation = {
478
+ type: "COUNT",
479
+ column,
480
+ alias: "count"
486
481
  };
482
+ clone.dml.columns = [`COUNT(${column}) AS count`];
483
+ clone.dml.data = null;
484
+ clone.dml.limit = 1;
487
485
  try {
488
- const result = await this.getResponse(executionDML);
486
+ const result = await clone.getResponse();
489
487
  const res = result[0] || null;
490
488
  if (res) {
491
489
  return res.count;
@@ -506,20 +504,18 @@ var Table = class _Table {
506
504
  * console.log(totalAge); // { sum: 55 }
507
505
  */
508
506
  async sum(column) {
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
507
+ const clone = this.clone();
508
+ clone.dml.type = "select";
509
+ clone.dml.aggregation = {
510
+ type: "SUM",
511
+ column,
512
+ alias: "sum"
520
513
  };
514
+ clone.dml.columns = [`SUM(${column}) AS sum`];
515
+ clone.dml.data = null;
516
+ clone.dml.limit = 1;
521
517
  try {
522
- const result = await this.getResponse(executionDML);
518
+ const result = await clone.getResponse();
523
519
  const res = result[0] || null;
524
520
  if (res) {
525
521
  return res.sum;
@@ -540,20 +536,18 @@ var Table = class _Table {
540
536
  * console.log(avgAge); // { avg: 27.5 }
541
537
  */
542
538
  async avg(column) {
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
539
+ const clone = this.clone();
540
+ clone.dml.type = "select";
541
+ clone.dml.aggregation = {
542
+ type: "AVG",
543
+ column,
544
+ alias: "avg"
554
545
  };
546
+ clone.dml.columns = [`AVG(${column}) AS avg`];
547
+ clone.dml.data = null;
548
+ clone.dml.limit = 1;
555
549
  try {
556
- const result = await this.getResponse(executionDML);
550
+ const result = await clone.getResponse();
557
551
  const res = result[0] || null;
558
552
  if (res) {
559
553
  return res.avg;
@@ -574,20 +568,18 @@ var Table = class _Table {
574
568
  * console.log(maxAge); // { max: 30 }
575
569
  */
576
570
  async max(column) {
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
571
+ const clone = this.clone();
572
+ clone.dml.type = "select";
573
+ clone.dml.aggregation = {
574
+ type: "MAX",
575
+ column,
576
+ alias: "max"
588
577
  };
578
+ clone.dml.columns = [`MAX(${column}) AS max`];
579
+ clone.dml.data = null;
580
+ clone.dml.limit = 1;
589
581
  try {
590
- const result = await this.getResponse(executionDML);
582
+ const result = await clone.getResponse();
591
583
  const res = result[0] || null;
592
584
  if (res) {
593
585
  return res.max;
@@ -608,20 +600,18 @@ var Table = class _Table {
608
600
  * console.log(minAge); // { min: 25 }
609
601
  */
610
602
  async min(column) {
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
603
+ const clone = this.clone();
604
+ clone.dml.type = "select";
605
+ clone.dml.aggregation = {
606
+ type: "MIN",
607
+ column,
608
+ alias: "min"
622
609
  };
610
+ clone.dml.columns = [`MIN(${column}) AS min`];
611
+ clone.dml.data = null;
612
+ clone.dml.limit = 1;
623
613
  try {
624
- const result = await this.getResponse(executionDML);
614
+ const result = await clone.getResponse();
625
615
  const res = result[0] || null;
626
616
  if (res) {
627
617
  return res.min;
@@ -674,12 +664,10 @@ var Table = class _Table {
674
664
  */
675
665
  async get() {
676
666
  try {
677
- const executionDML = {
678
- ...this.dml,
679
- type: "select",
680
- data: null
681
- };
682
- const result = await this.getResponse(executionDML);
667
+ const clone = this.clone();
668
+ clone.dml.type = "select";
669
+ clone.dml.data = null;
670
+ const result = await clone.getResponse();
683
671
  return result;
684
672
  } catch (error) {
685
673
  throw error;
@@ -695,14 +683,12 @@ var Table = class _Table {
695
683
  * console.log(user); // { id: 1, name: 'John' }
696
684
  */
697
685
  async first() {
698
- const executionDML = {
699
- ...this.dml,
700
- type: "select",
701
- data: null,
702
- limit: 1
703
- };
686
+ const clone = this.clone();
687
+ clone.dml.type = "select";
688
+ clone.dml.data = null;
689
+ clone.dml.limit = 1;
704
690
  try {
705
- const result = await this.getResponse(executionDML);
691
+ const result = await clone.getResponse();
706
692
  return result[0] || null;
707
693
  } catch (error) {
708
694
  throw error;
@@ -720,21 +706,13 @@ var Table = class _Table {
720
706
  * console.log(user); // { id: 1, name: 'John' }
721
707
  */
722
708
  async find(value, column = "id") {
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
- };
709
+ const clone = this.clone();
710
+ clone.dml.type = "select";
711
+ clone.dml.data = null;
712
+ clone.where(column, "=", value);
713
+ clone.dml.limit = 1;
736
714
  try {
737
- const result = await this.getResponse(executionDML);
715
+ const result = await clone.getResponse();
738
716
  return result[0] || null;
739
717
  } catch (error) {
740
718
  throw error;
@@ -754,53 +732,16 @@ var Table = class _Table {
754
732
  * console.log(newUsers); // [{ id: 3, name: 'Alice', age: 28 }, { id: 4, name: 'Bob', age: 32 }]
755
733
  */
756
734
  async insert(data) {
735
+ const clone = this.clone();
757
736
  if (!Array.isArray(data)) {
758
737
  throw new Error("The insert method requires an array of objects with key-value pairs.");
759
738
  }
760
739
  if (!data.every((item) => typeof item === "object" && item !== null)) {
761
740
  throw new Error("The array must contain only valid objects.");
762
741
  }
763
- const executionDML = {
764
- ...this.dml,
765
- type: "insert",
766
- data
767
- };
768
- const result = await this.getResponse(executionDML, "Add");
769
- console.log("\u{1F50D} INSERT result from engine:", JSON.stringify(result, null, 2));
770
- let insertedUuids = [];
771
- if (result && result[0] && result[0].inserted_uuids && Array.isArray(result[0].inserted_uuids)) {
772
- insertedUuids = result[0].inserted_uuids;
773
- }
774
- console.log("\u{1F50D} Extracted UUIDs:", insertedUuids);
775
- if (insertedUuids.length > 0) {
776
- const selectDML = {
777
- ...this.dml,
778
- type: "select",
779
- columns: ["*"],
780
- where: [{
781
- column: "uuid",
782
- operator: "IN",
783
- value: insertedUuids,
784
- type: "AND",
785
- isGroup: false
786
- }],
787
- data: null,
788
- aggregation: null,
789
- limit: null,
790
- offset: null
791
- };
792
- try {
793
- const insertedRecords = await this.getResponse(selectDML);
794
- console.log("\u{1F50D} Selected records:", JSON.stringify(insertedRecords, null, 2));
795
- return insertedRecords;
796
- } catch (error) {
797
- console.warn("Failed to fetch inserted records, returning data with UUIDs");
798
- return data.map((item, index) => ({
799
- ...item,
800
- uuid: insertedUuids[index]
801
- }));
802
- }
803
- }
742
+ clone.dml.type = "insert";
743
+ clone.dml.data = data;
744
+ await clone.getResponse(clone.dml, "Add");
804
745
  return data;
805
746
  }
806
747
  /**
@@ -816,18 +757,16 @@ var Table = class _Table {
816
757
  * console.log(result); // { affectedRows: 1 }
817
758
  */
818
759
  async update(data) {
760
+ const clone = this.clone();
819
761
  if (typeof data !== "object" || Array.isArray(data)) {
820
762
  throw new Error("The update method requires an object with key-value pairs.");
821
763
  }
822
- if (this.dml.where.length === 0) {
764
+ if (clone.dml.where.length === 0) {
823
765
  throw new Error("You must specify at least one WHERE condition to perform an update.");
824
766
  }
825
- const executionDML = {
826
- ...this.dml,
827
- type: "update",
828
- data
829
- };
830
- await this.getResponse(executionDML, "Update");
767
+ clone.dml.type = "update";
768
+ clone.dml.data = data;
769
+ await clone.getResponse(clone.dml, "Update");
831
770
  return data;
832
771
  }
833
772
  /**
@@ -840,14 +779,12 @@ var Table = class _Table {
840
779
  * console.log(result); // { affectedRows: 1 }
841
780
  */
842
781
  async delete() {
843
- if (this.dml.where.length === 0) {
782
+ const clone = this.clone();
783
+ if (clone.dml.where.length === 0) {
844
784
  throw new Error("You must specify at least one WHERE condition to perform a delete.");
845
785
  }
846
- const executionDML = {
847
- ...this.dml,
848
- type: "delete"
849
- };
850
- const deleteData = await this.getResponse(executionDML, "Delete");
786
+ clone.dml.type = "delete";
787
+ const deleteData = await clone.getResponse(clone.dml, "Delete");
851
788
  return deleteData;
852
789
  }
853
790
  async getResponse(dml = null, type = null) {