@dbcube/query-builder 3.0.35 → 3.0.37
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 +0 -0
- package/dist/index.cjs +82 -141
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +82 -141
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
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<
|
|
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<
|
|
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<
|
|
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<
|
|
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<
|
|
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<
|
|
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<
|
|
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<
|
|
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<
|
|
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<
|
|
302
|
+
min(column: string): Promise<Number>;
|
|
303
303
|
/**
|
|
304
304
|
* Adds a LIMIT clause to the query.
|
|
305
305
|
*
|
package/dist/index.js
CHANGED
|
@@ -178,6 +178,7 @@ var Table = class _Table {
|
|
|
178
178
|
const clone = this.clone();
|
|
179
179
|
clone.dml.type = "select";
|
|
180
180
|
clone.dml.columns = fields.length > 0 ? fields : ["*"];
|
|
181
|
+
clone.dml.aggregation = null;
|
|
181
182
|
return clone;
|
|
182
183
|
}
|
|
183
184
|
where(column, operator, value) {
|
|
@@ -472,20 +473,18 @@ var Table = class _Table {
|
|
|
472
473
|
* console.log(count); // { count: 2 }
|
|
473
474
|
*/
|
|
474
475
|
async count(column = "*") {
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
alias: "count"
|
|
482
|
-
},
|
|
483
|
-
columns: [`COUNT(${column}) AS count`],
|
|
484
|
-
data: null,
|
|
485
|
-
limit: 1
|
|
476
|
+
const clone = this.clone();
|
|
477
|
+
clone.dml.type = "select";
|
|
478
|
+
clone.dml.aggregation = {
|
|
479
|
+
type: "COUNT",
|
|
480
|
+
column,
|
|
481
|
+
alias: "count"
|
|
486
482
|
};
|
|
483
|
+
clone.dml.columns = [`COUNT(${column}) AS count`];
|
|
484
|
+
clone.dml.data = null;
|
|
485
|
+
clone.dml.limit = 1;
|
|
487
486
|
try {
|
|
488
|
-
const result = await
|
|
487
|
+
const result = await clone.getResponse();
|
|
489
488
|
const res = result[0] || null;
|
|
490
489
|
if (res) {
|
|
491
490
|
return res.count;
|
|
@@ -506,20 +505,18 @@ var Table = class _Table {
|
|
|
506
505
|
* console.log(totalAge); // { sum: 55 }
|
|
507
506
|
*/
|
|
508
507
|
async sum(column) {
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
alias: "sum"
|
|
516
|
-
},
|
|
517
|
-
columns: [`SUM(${column}) AS sum`],
|
|
518
|
-
data: null,
|
|
519
|
-
limit: 1
|
|
508
|
+
const clone = this.clone();
|
|
509
|
+
clone.dml.type = "select";
|
|
510
|
+
clone.dml.aggregation = {
|
|
511
|
+
type: "SUM",
|
|
512
|
+
column,
|
|
513
|
+
alias: "sum"
|
|
520
514
|
};
|
|
515
|
+
clone.dml.columns = [`SUM(${column}) AS sum`];
|
|
516
|
+
clone.dml.data = null;
|
|
517
|
+
clone.dml.limit = 1;
|
|
521
518
|
try {
|
|
522
|
-
const result = await
|
|
519
|
+
const result = await clone.getResponse();
|
|
523
520
|
const res = result[0] || null;
|
|
524
521
|
if (res) {
|
|
525
522
|
return res.sum;
|
|
@@ -540,20 +537,18 @@ var Table = class _Table {
|
|
|
540
537
|
* console.log(avgAge); // { avg: 27.5 }
|
|
541
538
|
*/
|
|
542
539
|
async avg(column) {
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
alias: "avg"
|
|
550
|
-
},
|
|
551
|
-
columns: [`AVG(${column}) AS avg`],
|
|
552
|
-
data: null,
|
|
553
|
-
limit: 1
|
|
540
|
+
const clone = this.clone();
|
|
541
|
+
clone.dml.type = "select";
|
|
542
|
+
clone.dml.aggregation = {
|
|
543
|
+
type: "AVG",
|
|
544
|
+
column,
|
|
545
|
+
alias: "avg"
|
|
554
546
|
};
|
|
547
|
+
clone.dml.columns = [`AVG(${column}) AS avg`];
|
|
548
|
+
clone.dml.data = null;
|
|
549
|
+
clone.dml.limit = 1;
|
|
555
550
|
try {
|
|
556
|
-
const result = await
|
|
551
|
+
const result = await clone.getResponse();
|
|
557
552
|
const res = result[0] || null;
|
|
558
553
|
if (res) {
|
|
559
554
|
return res.avg;
|
|
@@ -574,20 +569,18 @@ var Table = class _Table {
|
|
|
574
569
|
* console.log(maxAge); // { max: 30 }
|
|
575
570
|
*/
|
|
576
571
|
async max(column) {
|
|
577
|
-
const
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
alias: "max"
|
|
584
|
-
},
|
|
585
|
-
columns: [`MAX(${column}) AS max`],
|
|
586
|
-
data: null,
|
|
587
|
-
limit: 1
|
|
572
|
+
const clone = this.clone();
|
|
573
|
+
clone.dml.type = "select";
|
|
574
|
+
clone.dml.aggregation = {
|
|
575
|
+
type: "MAX",
|
|
576
|
+
column,
|
|
577
|
+
alias: "max"
|
|
588
578
|
};
|
|
579
|
+
clone.dml.columns = [`MAX(${column}) AS max`];
|
|
580
|
+
clone.dml.data = null;
|
|
581
|
+
clone.dml.limit = 1;
|
|
589
582
|
try {
|
|
590
|
-
const result = await
|
|
583
|
+
const result = await clone.getResponse();
|
|
591
584
|
const res = result[0] || null;
|
|
592
585
|
if (res) {
|
|
593
586
|
return res.max;
|
|
@@ -608,20 +601,18 @@ var Table = class _Table {
|
|
|
608
601
|
* console.log(minAge); // { min: 25 }
|
|
609
602
|
*/
|
|
610
603
|
async min(column) {
|
|
611
|
-
const
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
alias: "min"
|
|
618
|
-
},
|
|
619
|
-
columns: [`MIN(${column}) AS min`],
|
|
620
|
-
data: null,
|
|
621
|
-
limit: 1
|
|
604
|
+
const clone = this.clone();
|
|
605
|
+
clone.dml.type = "select";
|
|
606
|
+
clone.dml.aggregation = {
|
|
607
|
+
type: "MIN",
|
|
608
|
+
column,
|
|
609
|
+
alias: "min"
|
|
622
610
|
};
|
|
611
|
+
clone.dml.columns = [`MIN(${column}) AS min`];
|
|
612
|
+
clone.dml.data = null;
|
|
613
|
+
clone.dml.limit = 1;
|
|
623
614
|
try {
|
|
624
|
-
const result = await
|
|
615
|
+
const result = await clone.getResponse();
|
|
625
616
|
const res = result[0] || null;
|
|
626
617
|
if (res) {
|
|
627
618
|
return res.min;
|
|
@@ -674,12 +665,11 @@ var Table = class _Table {
|
|
|
674
665
|
*/
|
|
675
666
|
async get() {
|
|
676
667
|
try {
|
|
677
|
-
const
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
const result = await this.getResponse(executionDML);
|
|
668
|
+
const clone = this.clone();
|
|
669
|
+
clone.dml.type = "select";
|
|
670
|
+
clone.dml.data = null;
|
|
671
|
+
clone.dml.aggregation = null;
|
|
672
|
+
const result = await clone.getResponse();
|
|
683
673
|
return result;
|
|
684
674
|
} catch (error) {
|
|
685
675
|
throw error;
|
|
@@ -695,14 +685,13 @@ var Table = class _Table {
|
|
|
695
685
|
* console.log(user); // { id: 1, name: 'John' }
|
|
696
686
|
*/
|
|
697
687
|
async first() {
|
|
698
|
-
const
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
};
|
|
688
|
+
const clone = this.clone();
|
|
689
|
+
clone.dml.type = "select";
|
|
690
|
+
clone.dml.data = null;
|
|
691
|
+
clone.dml.aggregation = null;
|
|
692
|
+
clone.dml.limit = 1;
|
|
704
693
|
try {
|
|
705
|
-
const result = await
|
|
694
|
+
const result = await clone.getResponse();
|
|
706
695
|
return result[0] || null;
|
|
707
696
|
} catch (error) {
|
|
708
697
|
throw error;
|
|
@@ -720,21 +709,14 @@ var Table = class _Table {
|
|
|
720
709
|
* console.log(user); // { id: 1, name: 'John' }
|
|
721
710
|
*/
|
|
722
711
|
async find(value, column = "id") {
|
|
723
|
-
const
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
operator: "=",
|
|
730
|
-
value,
|
|
731
|
-
type: "AND",
|
|
732
|
-
isGroup: false
|
|
733
|
-
}],
|
|
734
|
-
limit: 1
|
|
735
|
-
};
|
|
712
|
+
const clone = this.clone();
|
|
713
|
+
clone.dml.type = "select";
|
|
714
|
+
clone.dml.data = null;
|
|
715
|
+
clone.dml.aggregation = null;
|
|
716
|
+
clone.where(column, "=", value);
|
|
717
|
+
clone.dml.limit = 1;
|
|
736
718
|
try {
|
|
737
|
-
const result = await
|
|
719
|
+
const result = await clone.getResponse();
|
|
738
720
|
return result[0] || null;
|
|
739
721
|
} catch (error) {
|
|
740
722
|
throw error;
|
|
@@ -754,53 +736,16 @@ var Table = class _Table {
|
|
|
754
736
|
* console.log(newUsers); // [{ id: 3, name: 'Alice', age: 28 }, { id: 4, name: 'Bob', age: 32 }]
|
|
755
737
|
*/
|
|
756
738
|
async insert(data) {
|
|
739
|
+
const clone = this.clone();
|
|
757
740
|
if (!Array.isArray(data)) {
|
|
758
741
|
throw new Error("The insert method requires an array of objects with key-value pairs.");
|
|
759
742
|
}
|
|
760
743
|
if (!data.every((item) => typeof item === "object" && item !== null)) {
|
|
761
744
|
throw new Error("The array must contain only valid objects.");
|
|
762
745
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
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
|
-
}
|
|
746
|
+
clone.dml.type = "insert";
|
|
747
|
+
clone.dml.data = data;
|
|
748
|
+
await clone.getResponse(clone.dml, "Add");
|
|
804
749
|
return data;
|
|
805
750
|
}
|
|
806
751
|
/**
|
|
@@ -816,18 +761,16 @@ var Table = class _Table {
|
|
|
816
761
|
* console.log(result); // { affectedRows: 1 }
|
|
817
762
|
*/
|
|
818
763
|
async update(data) {
|
|
764
|
+
const clone = this.clone();
|
|
819
765
|
if (typeof data !== "object" || Array.isArray(data)) {
|
|
820
766
|
throw new Error("The update method requires an object with key-value pairs.");
|
|
821
767
|
}
|
|
822
|
-
if (
|
|
768
|
+
if (clone.dml.where.length === 0) {
|
|
823
769
|
throw new Error("You must specify at least one WHERE condition to perform an update.");
|
|
824
770
|
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
data
|
|
829
|
-
};
|
|
830
|
-
await this.getResponse(executionDML, "Update");
|
|
771
|
+
clone.dml.type = "update";
|
|
772
|
+
clone.dml.data = data;
|
|
773
|
+
await clone.getResponse(clone.dml, "Update");
|
|
831
774
|
return data;
|
|
832
775
|
}
|
|
833
776
|
/**
|
|
@@ -840,14 +783,12 @@ var Table = class _Table {
|
|
|
840
783
|
* console.log(result); // { affectedRows: 1 }
|
|
841
784
|
*/
|
|
842
785
|
async delete() {
|
|
843
|
-
|
|
786
|
+
const clone = this.clone();
|
|
787
|
+
if (clone.dml.where.length === 0) {
|
|
844
788
|
throw new Error("You must specify at least one WHERE condition to perform a delete.");
|
|
845
789
|
}
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
type: "delete"
|
|
849
|
-
};
|
|
850
|
-
const deleteData = await this.getResponse(executionDML, "Delete");
|
|
790
|
+
clone.dml.type = "delete";
|
|
791
|
+
const deleteData = await clone.getResponse(clone.dml, "Delete");
|
|
851
792
|
return deleteData;
|
|
852
793
|
}
|
|
853
794
|
async getResponse(dml = null, type = null) {
|