tspace-mysql 1.4.4 → 1.4.6

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.
@@ -74,26 +74,24 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
74
74
  }
75
75
  /**
76
76
  *
77
- * @param {string=} column [column=id]
78
77
  * @return {this} this
79
78
  */
80
- distinct(column = 'id') {
81
- this.$state.set('SELECT', [
82
- `${this.$constants('SELECT')}`,
83
- `${this.$constants('DISTINCT')}`,
84
- `${column}`
85
- ].join(' '));
79
+ distinct() {
80
+ this.$state.set('DISTINCT', true);
81
+ const select = this.$state.get('SELECT');
82
+ if (select.includes('*'))
83
+ this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('DISTINCT')} *`);
86
84
  return this;
87
85
  }
88
86
  /**
89
87
  * select data form table
90
- * @param {Array<string>} ...columns
88
+ * @param {string[]} ...columns
91
89
  * @return {this} this
92
90
  */
93
91
  select(...columns) {
94
92
  if (!columns.length)
95
93
  return this;
96
- const select = columns.map((column) => {
94
+ let select = columns.map((column) => {
97
95
  if (column === '*')
98
96
  return column;
99
97
  if (/\./.test(column))
@@ -102,19 +100,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
102
100
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
103
101
  return `\`${column}\``;
104
102
  }).join(', ');
103
+ select = this.$state.get('DISTINCT') && !select.includes(this.$constants('DISTINCT'))
104
+ ? `${this.$constants('DISTINCT')} ${select}`
105
+ : `${select}`;
105
106
  this.$state.set('SELECT', `${this.$constants('SELECT')} ${select}`);
106
107
  return this;
107
108
  }
108
109
  selectRaw(...columns) {
109
110
  if (!columns.length)
110
111
  return this;
111
- const select = columns.map((column) => {
112
+ let select = columns.map((column) => {
112
113
  if (column === '*')
113
114
  return column;
114
115
  if (column.includes(this.$constants('RAW')))
115
116
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
116
117
  return column;
117
118
  }).join(', ');
119
+ select = this.$state.get('DISTINCT') && !select.includes(this.$constants('DISTINCT'))
120
+ ? `${this.$constants('DISTINCT')} ${select}`
121
+ : `${select}`;
118
122
  this.$state.set('SELECT', `${this.$constants('SELECT')} ${select}`);
119
123
  return this;
120
124
  }
@@ -186,14 +190,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
186
190
  }
187
191
  /**
188
192
  * where json target to key in json values default operator '='
193
+ * @param {string} column
189
194
  * @param {object} property
190
- * @property {string} property.column
191
195
  * @property {string} property.targetKey
192
196
  * @property {string} property.value
193
197
  * @property {string?} property.operator
198
+ * @example
194
199
  * @return {this}
195
200
  */
196
- whereJson({ column, targetKey, value, operator }) {
201
+ whereJSON(column, { targetKey, value, operator }) {
197
202
  value = this.$utils.escape(value);
198
203
  value = this._valueTrueFalse(value);
199
204
  this.$state.set('WHERE', [
@@ -201,7 +206,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
201
206
  ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
202
207
  : `${this.$constants('WHERE')}`,
203
208
  `${this._bindTableAndColumnInQueryWhere(column)}->>'$.${targetKey}'`,
204
- `${operator == null ? '=' : operator}`,
209
+ `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
205
210
  `${this._checkValueHasRaw(value)}`
206
211
  ].join(' '));
207
212
  return this;
@@ -671,7 +676,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
671
676
  this._queryWhereIsExists()
672
677
  ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
673
678
  : `${this.$constants('WHERE')}`,
674
- `BINARY ${this._bindTableAndColumnInQueryWhere(column)}`,
679
+ `${this.$constants('BINARY')} ${this._bindTableAndColumnInQueryWhere(column)}`,
675
680
  `${operator}`,
676
681
  `${this._checkValueHasRaw(this.$utils.escape(value))}`
677
682
  ].join(' '));
@@ -702,7 +707,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
702
707
  this._queryWhereIsExists()
703
708
  ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
704
709
  : `${this.$constants('WHERE')}`,
705
- `BINARY ${this._bindTableAndColumnInQueryWhere(column)}`,
710
+ `${this.$constants('BINARY')} ${this._bindTableAndColumnInQueryWhere(column)}`,
706
711
  `${operator}`,
707
712
  `${this._checkValueHasRaw(this.$utils.escape(value))}`
708
713
  ].join(' '));
@@ -802,8 +807,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
802
807
  }
803
808
  /**
804
809
  *
805
- * @param {string} pk talbe.pk
806
- * @param {string} fk talbe.fk
810
+ * @param {string} localKey local key in current table
811
+ * @param {string} referenceKey reference key in next table
807
812
  * @example
808
813
  * await new DB('users')
809
814
  * .select('users.id as userId','posts.id as postId','email')
@@ -814,89 +819,89 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
814
819
  * .get()
815
820
  * @return {this}
816
821
  */
817
- join(pk, fk) {
822
+ join(localKey, referenceKey) {
818
823
  var _a;
819
- const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
824
+ const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
820
825
  if (this.$state.get('JOIN')) {
821
826
  this.$state.set('JOIN', [
822
827
  `${this.$state.get('JOIN')}`,
823
828
  `${this.$constants('INNER_JOIN')}`,
824
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
829
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
825
830
  ].join(' '));
826
831
  return this;
827
832
  }
828
833
  this.$state.set('JOIN', [
829
834
  `${this.$constants('INNER_JOIN')}`,
830
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
835
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
831
836
  ].join(' '));
832
837
  return this;
833
838
  }
834
839
  /**
835
840
  *
836
- * @param {string} pk talbe.pk
837
- * @param {string} fk talbe.fk
841
+ * @param {string} localKey local key in current table
842
+ * @param {string} referenceKey reference key in next table
838
843
  * @return {this}
839
844
  */
840
- rightJoin(pk, fk) {
845
+ rightJoin(localKey, referenceKey) {
841
846
  var _a;
842
- const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
847
+ const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
843
848
  if (this.$state.get('JOIN')) {
844
849
  this.$state.set('JOIN', [
845
850
  `${this.$state.get('JOIN')}`,
846
851
  `${this.$constants('RIGHT_JOIN')}`,
847
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
852
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
848
853
  ].join(' '));
849
854
  return this;
850
855
  }
851
856
  this.$state.set('JOIN', [
852
857
  `${this.$constants('RIGHT_JOIN')}`,
853
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
858
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
854
859
  ].join(' '));
855
860
  return this;
856
861
  }
857
862
  /**
858
863
  *
859
- * @param {string} pk talbe.pk
860
- * @param {string} fk talbe.fk
864
+ * @param {string} localKey local key in current table
865
+ * @param {string} referenceKey reference key in next table
861
866
  * @return {this}
862
867
  */
863
- leftJoin(pk, fk) {
868
+ leftJoin(localKey, referenceKey) {
864
869
  var _a;
865
- const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
870
+ const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
866
871
  if (this.$state.get('JOIN')) {
867
872
  this.$state.set('JOIN', [
868
873
  `${this.$state.get('JOIN')}`,
869
874
  `${this.$constants('LEFT_JOIN')}`,
870
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
875
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
871
876
  ].join(' '));
872
877
  return this;
873
878
  }
874
879
  this.$state.set('JOIN', [
875
880
  `${this.$constants('LEFT_JOIN')}`,
876
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
881
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
877
882
  ].join(' '));
878
883
  return this;
879
884
  }
880
885
  /**
881
886
  *
882
- * @param {string} pk talbe.pk
883
- * @param {string} fk talbe.fk
887
+ * @param {string} localKey local key in current table
888
+ * @param {string} referenceKey reference key in next table
884
889
  * @return {this}
885
890
  */
886
- crossJoin(pk, fk) {
891
+ crossJoin(localKey, referenceKey) {
887
892
  var _a;
888
- const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
893
+ const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
889
894
  if (this.$state.get('JOIN')) {
890
895
  this.$state.set('JOIN', [
891
896
  `${this.$state.get('JOIN')}`,
892
897
  `${this.$constants('CROSS_JOIN')}`,
893
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
898
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
894
899
  ].join(' '));
895
900
  return this;
896
901
  }
897
902
  this.$state.set('JOIN', [
898
903
  `${this.$constants('CROSS_JOIN')}`,
899
- `\`${table}\` ${this.$constants('ON')} ${pk} = ${fk}`
904
+ `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
900
905
  ].join(' '));
901
906
  return this;
902
907
  }
@@ -1143,9 +1148,45 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1143
1148
  *
1144
1149
  * update data in the database
1145
1150
  * @param {object} data
1151
+ * @param {array?} updateNotExists options for except update some records in your ${data} using name column(s)
1146
1152
  * @return {this} this
1147
1153
  */
1148
- update(data) {
1154
+ update(data, updateNotExists = []) {
1155
+ if (!Object.keys(data).length)
1156
+ throw new Error('This method must be required');
1157
+ if (updateNotExists.length) {
1158
+ for (const c of updateNotExists) {
1159
+ for (const column in data) {
1160
+ if (c !== column)
1161
+ continue;
1162
+ const value = data[column];
1163
+ data[column] = this._updateHandler(column, value);
1164
+ break;
1165
+ }
1166
+ }
1167
+ }
1168
+ const query = this._queryUpdate(data);
1169
+ this.$state.set('UPDATE', [
1170
+ `${this.$constants('UPDATE')}`,
1171
+ `${this.$state.get('TABLE_NAME')}`,
1172
+ `${query}`
1173
+ ].join(' '));
1174
+ this.$state.set('SAVE', 'UPDATE');
1175
+ return this;
1176
+ }
1177
+ /**
1178
+ *
1179
+ * update record if data is empty in the database
1180
+ * @param {object} data
1181
+ * @return {this} this
1182
+ */
1183
+ updateNotExists(data) {
1184
+ if (!Object.keys(data).length)
1185
+ throw new Error('This method must be required');
1186
+ for (const column in data) {
1187
+ const value = data[column];
1188
+ data[column] = this._updateHandler(column, value);
1189
+ }
1149
1190
  const query = this._queryUpdate(data);
1150
1191
  this.$state.set('UPDATE', [
1151
1192
  `${this.$constants('UPDATE')}`,
@@ -1162,6 +1203,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1162
1203
  * @return {this} this
1163
1204
  */
1164
1205
  insert(data) {
1206
+ if (!Object.keys(data).length)
1207
+ throw new Error('This method must be required');
1165
1208
  const query = this._queryInsert(data);
1166
1209
  this.$state.set('INSERT', [
1167
1210
  `${this.$constants('INSERT')}`,
@@ -1178,6 +1221,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1178
1221
  * @return {this} this
1179
1222
  */
1180
1223
  create(data) {
1224
+ if (!Object.keys(data).length)
1225
+ throw new Error('This method must be required');
1181
1226
  const query = this._queryInsert(data);
1182
1227
  this.$state.set('INSERT', [
1183
1228
  `${this.$constants('INSERT')}`,
@@ -1194,6 +1239,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1194
1239
  * @return {this} this this
1195
1240
  */
1196
1241
  createMultiple(data) {
1242
+ if (!Object.keys(data).length)
1243
+ throw new Error('This method must be required');
1197
1244
  const query = this._queryInsertMultiple(data);
1198
1245
  this.$state.set('INSERT', [
1199
1246
  `${this.$constants('INSERT')}`,
@@ -1210,6 +1257,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1210
1257
  * @return {this} this this
1211
1258
  */
1212
1259
  insertMultiple(data) {
1260
+ if (!Object.keys(data).length)
1261
+ throw new Error('This method must be required');
1213
1262
  const query = this._queryInsertMultiple(data);
1214
1263
  this.$state.set('INSERT', [
1215
1264
  `${this.$constants('INSERT')}`,
@@ -1224,14 +1273,24 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1224
1273
  * @return {string} return sql query
1225
1274
  */
1226
1275
  toString() {
1227
- return this._buildQuery();
1276
+ const sql = this._buildQueryStatement();
1277
+ if (this.$state.get('DEBUG'))
1278
+ this.$utils.consoleDebug(sql);
1279
+ return this.resultHandler(sql);
1228
1280
  }
1229
1281
  /**
1230
1282
  *
1231
1283
  * @return {string} return sql query
1232
1284
  */
1233
1285
  toSQL() {
1234
- return this._buildQuery();
1286
+ return this.toString();
1287
+ }
1288
+ /**
1289
+ *
1290
+ * @return {string}
1291
+ */
1292
+ toRawSQL() {
1293
+ return this.toString();
1235
1294
  }
1236
1295
  /**
1237
1296
  *
@@ -1479,6 +1538,66 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1479
1538
  return removeExcepts.flat();
1480
1539
  });
1481
1540
  }
1541
+ /**
1542
+ * handler query update for except some columns
1543
+ * @param {string} column
1544
+ * @param {string} value
1545
+ * @return {string} string
1546
+ */
1547
+ _updateHandler(column, value) {
1548
+ return DB_1.DB.raw([
1549
+ this.$constants('CASE'),
1550
+ this.$constants('WHEN'),
1551
+ `(\`${column}\` = "" ${this.$constants('OR')} \`${column}\` ${this.$constants('IS_NULL')})`,
1552
+ this.$constants('THEN'),
1553
+ `"${value !== null && value !== void 0 ? value : ""}" ${this.$constants('ELSE')} \`${column}\``,
1554
+ this.$constants('END')
1555
+ ].join(' '));
1556
+ }
1557
+ /**
1558
+ *
1559
+ * generate sql statements
1560
+ * @return {string} string generated query string
1561
+ */
1562
+ _buildQueryStatement() {
1563
+ let sql = [];
1564
+ while (true) {
1565
+ if (this.$state.get('INSERT')) {
1566
+ sql = [
1567
+ this.$state.get('INSERT')
1568
+ ];
1569
+ break;
1570
+ }
1571
+ if (this.$state.get('UPDATE')) {
1572
+ sql = [
1573
+ this.$state.get('UPDATE'),
1574
+ this.$state.get('WHERE')
1575
+ ];
1576
+ break;
1577
+ }
1578
+ if (this.$state.get('DELETE')) {
1579
+ sql = [
1580
+ this.$state.get('DELETE'),
1581
+ this.$state.get('WHERE')
1582
+ ];
1583
+ break;
1584
+ }
1585
+ sql = [
1586
+ this.$state.get('SELECT'),
1587
+ this.$state.get('FROM'),
1588
+ this.$state.get('TABLE_NAME'),
1589
+ this.$state.get('JOIN'),
1590
+ this.$state.get('WHERE'),
1591
+ this.$state.get('GROUP_BY'),
1592
+ this.$state.get('HAVING'),
1593
+ this.$state.get('ORDER_BY'),
1594
+ this.$state.get('LIMIT'),
1595
+ this.$state.get('OFFSET')
1596
+ ];
1597
+ break;
1598
+ }
1599
+ return sql.filter(s => s !== '' || s == null).join(' ');
1600
+ }
1482
1601
  /**
1483
1602
  * execute sql statements with raw sql query
1484
1603
  * @param {string} sql sql execute return data
@@ -1578,7 +1697,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1578
1697
  const nextPage = currentPage + 1;
1579
1698
  const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
1580
1699
  const offset = (page - 1) * limit;
1581
- let sql = this._buildQuery();
1700
+ let sql = this._buildQueryStatement();
1582
1701
  if (!sql.includes(this.$constants('LIMIT'))) {
1583
1702
  sql = [
1584
1703
  `${sql}`,
@@ -1666,7 +1785,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1666
1785
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
1667
1786
  this.select(...yield this.exceptColumns());
1668
1787
  this.limit(1);
1669
- let sql = this._buildQuery();
1788
+ let sql = this._buildQueryStatement();
1670
1789
  const result = yield this.queryStatement(sql);
1671
1790
  if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
1672
1791
  this._hiddenColumn(result);
@@ -1687,7 +1806,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1687
1806
  }
1688
1807
  /**
1689
1808
  *
1690
- * execute data return object | throw rror
1809
+ * execute data return object | null
1691
1810
  * @return {promise<object | null>}
1692
1811
  */
1693
1812
  findOne() {
@@ -1705,7 +1824,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1705
1824
  return __awaiter(this, void 0, void 0, function* () {
1706
1825
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
1707
1826
  this.select(...yield this.exceptColumns());
1708
- let sql = this._buildQuery();
1827
+ let sql = this._buildQueryStatement();
1709
1828
  if (!sql.includes(this.$constants('LIMIT')))
1710
1829
  sql = `${sql} ${this.$constants('LIMIT')} 1`;
1711
1830
  else
@@ -1752,14 +1871,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1752
1871
  /**
1753
1872
  *
1754
1873
  * execute data return Array
1755
- * @return {promise<Array<any>>}
1874
+ * @return {promise<any[]>}
1756
1875
  */
1757
1876
  get() {
1758
1877
  var _a, _b;
1759
1878
  return __awaiter(this, void 0, void 0, function* () {
1760
1879
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
1761
1880
  this.select(...yield this.exceptColumns());
1762
- const sql = this._buildQuery();
1881
+ const sql = this._buildQueryStatement();
1763
1882
  const result = yield this.queryStatement(sql);
1764
1883
  if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
1765
1884
  this._hiddenColumn(result);
@@ -1790,7 +1909,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1790
1909
  /**
1791
1910
  *
1792
1911
  * execute data return Array
1793
- * @return {promise<Array<any>>}
1912
+ * @return {promise<any[]>}
1794
1913
  */
1795
1914
  findMany() {
1796
1915
  return __awaiter(this, void 0, void 0, function* () {
@@ -1803,13 +1922,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1803
1922
  * @return {promise<string>}
1804
1923
  */
1805
1924
  toJSON() {
1806
- var _a, _b;
1807
1925
  return __awaiter(this, void 0, void 0, function* () {
1808
- if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
1809
- this.select(...yield this.exceptColumns());
1810
- const sql = this._buildQuery();
1926
+ const sql = this._buildQueryStatement();
1811
1927
  const result = yield this.queryStatement(sql);
1812
- if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
1928
+ if (this.$state.get('HIDDEN').length)
1813
1929
  this._hiddenColumn(result);
1814
1930
  return this.resultHandler(JSON.stringify(result));
1815
1931
  });
@@ -1822,8 +1938,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1822
1938
  */
1823
1939
  toArray(column = 'id') {
1824
1940
  return __awaiter(this, void 0, void 0, function* () {
1825
- this.$state.set('SELECT', `${this.$constants('SELECT')} ${column}`);
1826
- const sql = this._buildQuery();
1941
+ this.selectRaw(column);
1942
+ const sql = this._buildQueryStatement();
1827
1943
  const result = yield this.queryStatement(sql);
1828
1944
  const toArray = result.map((data) => data[column]);
1829
1945
  return this.resultHandler(toArray);
@@ -1831,42 +1947,38 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1831
1947
  }
1832
1948
  /**
1833
1949
  *
1834
- * execute data return number of results
1835
- * @param {string=} column [column=id]
1836
- * @return {promise<number>}
1950
+ * execute data return result is exists
1951
+ * @return {promise<boolean>}
1837
1952
  */
1838
- count(column = 'id') {
1953
+ exists() {
1839
1954
  var _a;
1840
1955
  return __awaiter(this, void 0, void 0, function* () {
1841
- this.$state.set('SELECT', [
1956
+ this.limit(1);
1957
+ const sql = this._buildQueryStatement();
1958
+ const result = yield this.queryStatement([
1842
1959
  `${this.$constants('SELECT')}`,
1843
- `${this.$constants('COUNT')}(${column})`,
1844
- `${this.$constants('AS')} \`total\``
1960
+ `${this.$constants('EXISTS')}`,
1961
+ `(${sql})`,
1962
+ `${this.$constants('AS')} \`aggregate\``
1845
1963
  ].join(' '));
1846
- const sql = this._buildQuery();
1847
- const result = yield this.queryStatement(sql);
1848
- this.$state.resetState();
1849
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.total) || 0));
1964
+ return Boolean(this.resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || false));
1850
1965
  });
1851
1966
  }
1852
1967
  /**
1853
1968
  *
1854
- * execute data return result is exists
1855
- * @return {promise<boolean>}
1969
+ * execute data return number of results
1970
+ * @param {string=} column [column=id]
1971
+ * @return {promise<number>}
1856
1972
  */
1857
- exists() {
1973
+ count(column = 'id') {
1858
1974
  var _a;
1859
1975
  return __awaiter(this, void 0, void 0, function* () {
1860
- const result = yield this.queryStatement([
1861
- `${this.$constants('SELECT')}`,
1862
- `${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
1863
- `*`,
1864
- `${this.$state.get('FROM')}`,
1865
- `${this.$state.get('TABLE_NAME')}`,
1866
- `${this.$state.get('WHERE')}`,
1867
- `${this.$constants('LIMIT')} 1) ${this.$constants('AS')} \`exists\``
1868
- ].join(' '));
1869
- return Boolean(this.resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.exists) || false));
1976
+ const distinct = this.$state.get('DISTINCT');
1977
+ column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
1978
+ this.selectRaw(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} \`aggregate\``);
1979
+ const sql = this._buildQueryStatement();
1980
+ const result = yield this.queryStatement(sql);
1981
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
1870
1982
  });
1871
1983
  }
1872
1984
  /**
@@ -1878,14 +1990,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1878
1990
  avg(column = 'id') {
1879
1991
  var _a;
1880
1992
  return __awaiter(this, void 0, void 0, function* () {
1881
- this.$state.set('SELECT', [
1882
- `${this.$constants('SELECT')}`,
1883
- `${this.$constants('AVG')}(${column})`,
1884
- `${this.$constants('AS')} \`avg\``
1885
- ].join(' '));
1886
- const sql = this._buildQuery();
1993
+ const distinct = this.$state.get('DISTINCT');
1994
+ column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
1995
+ this.selectRaw(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} \`aggregate\``);
1996
+ const sql = this._buildQueryStatement();
1887
1997
  const result = yield this.queryStatement(sql);
1888
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.avg) || 0));
1998
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
1889
1999
  });
1890
2000
  }
1891
2001
  /**
@@ -1897,10 +2007,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1897
2007
  sum(column = 'id') {
1898
2008
  var _a;
1899
2009
  return __awaiter(this, void 0, void 0, function* () {
1900
- this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('SUM')}(${column}) ${this.$constants('AS')} \`sum\``);
1901
- const sql = this._buildQuery();
2010
+ const distinct = this.$state.get('DISTINCT');
2011
+ column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2012
+ this.selectRaw(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2013
+ const sql = this._buildQueryStatement();
1902
2014
  const result = yield this.queryStatement(sql);
1903
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.sum) || 0));
2015
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
1904
2016
  });
1905
2017
  }
1906
2018
  /**
@@ -1912,10 +2024,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1912
2024
  max(column = 'id') {
1913
2025
  var _a;
1914
2026
  return __awaiter(this, void 0, void 0, function* () {
1915
- this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('MAX')}(${column}) ${this.$constants('AS')} \`max\``);
1916
- const sql = this._buildQuery();
2027
+ const distinct = this.$state.get('DISTINCT');
2028
+ column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2029
+ this.selectRaw(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2030
+ const sql = this._buildQueryStatement();
1917
2031
  const result = yield this.queryStatement(sql);
1918
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.max) || 0));
2032
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
1919
2033
  });
1920
2034
  }
1921
2035
  /**
@@ -1927,10 +2041,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1927
2041
  min(column = 'id') {
1928
2042
  var _a;
1929
2043
  return __awaiter(this, void 0, void 0, function* () {
1930
- this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('MIN')}(${column}) ${this.$constants('AS')} \`min\``);
1931
- const sql = this._buildQuery();
2044
+ const distinct = this.$state.get('DISTINCT');
2045
+ column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2046
+ this.selectRaw(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2047
+ const sql = this._buildQueryStatement();
1932
2048
  const result = yield this.queryStatement(sql);
1933
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.min) || 0));
2049
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
1934
2050
  });
1935
2051
  }
1936
2052
  /**
@@ -1990,7 +2106,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1990
2106
  `, ${this.$constants('GROUP_CONCAT')}(id)`,
1991
2107
  `${this.$constants('AS')} data`
1992
2108
  ].join(' '));
1993
- const sql = this._buildQuery();
2109
+ const sql = this._buildQueryStatement();
1994
2110
  const results = yield this.queryStatement(sql);
1995
2111
  let data = [];
1996
2112
  results.forEach((result) => {
@@ -2442,6 +2558,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2442
2558
  faker(rows = 1) {
2443
2559
  return __awaiter(this, void 0, void 0, function* () {
2444
2560
  let data = [];
2561
+ this.void();
2445
2562
  const sql = [
2446
2563
  `${this.$constants('SHOW')}`,
2447
2564
  `${this.$constants('FIELDS')}`,
@@ -2558,7 +2675,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2558
2675
  returnId: true
2559
2676
  });
2560
2677
  if (this.$state.get('VOID') || !result)
2561
- return this.resultHandler(null);
2678
+ return this.resultHandler(undefined);
2562
2679
  const sql = [
2563
2680
  `${this.$state.get('SELECT')}`,
2564
2681
  `${this.$state.get('FROM')}`,
@@ -2599,7 +2716,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2599
2716
  returnId: true
2600
2717
  });
2601
2718
  if (this.$state.get('VOID') || !result)
2602
- return this.resultHandler(null);
2719
+ return this.resultHandler(undefined);
2603
2720
  const sql = [
2604
2721
  `${this.$state.get('SELECT')}`,
2605
2722
  `${this.$state.get('FROM')}`,
@@ -2624,7 +2741,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2624
2741
  returnId: true
2625
2742
  });
2626
2743
  if (this.$state.get('VOID') || !result)
2627
- return this.resultHandler(null);
2744
+ return this.resultHandler(undefined);
2628
2745
  const arrayId = [...Array(result)].map((_, i) => i + id);
2629
2746
  const sql = [
2630
2747
  `${this.$state.get('SELECT')}`,
@@ -2663,7 +2780,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2663
2780
  returnId: true
2664
2781
  });
2665
2782
  if (this.$state.get('VOID') || !result)
2666
- return this.resultHandler(null);
2783
+ return this.resultHandler(undefined);
2667
2784
  const sql = [
2668
2785
  `${this.$state.get('SELECT')}`,
2669
2786
  `${this.$state.get('FROM')}`,
@@ -2721,7 +2838,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2721
2838
  returnId: true
2722
2839
  });
2723
2840
  if (this.$state.get('VOID') || !result)
2724
- return this.resultHandler(null);
2841
+ return this.resultHandler(undefined);
2725
2842
  const sql = [
2726
2843
  `${this.$state.get('SELECT')}`,
2727
2844
  `${this.$state.get('FROM')}`,
@@ -2772,7 +2889,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2772
2889
  ].join(' ')
2773
2890
  });
2774
2891
  if (this.$state.get('VOID') || !result)
2775
- return this.resultHandler(null);
2892
+ return this.resultHandler(undefined);
2776
2893
  const sql = [
2777
2894
  `${this.$state.get('SELECT')}`,
2778
2895
  `${this.$state.get('FROM')}`,
@@ -2866,45 +2983,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2866
2983
  return 0;
2867
2984
  return value;
2868
2985
  }
2869
- _buildQuery() {
2870
- let sql = [];
2871
- while (true) {
2872
- if (this.$state.get('INSERT')) {
2873
- sql = [
2874
- this.$state.get('INSERT')
2875
- ];
2876
- break;
2877
- }
2878
- if (this.$state.get('UPDATE')) {
2879
- sql = [
2880
- this.$state.get('UPDATE'),
2881
- this.$state.get('WHERE')
2882
- ];
2883
- break;
2884
- }
2885
- if (this.$state.get('DELETE')) {
2886
- sql = [
2887
- this.$state.get('DELETE'),
2888
- this.$state.get('WHERE')
2889
- ];
2890
- break;
2891
- }
2892
- sql = [
2893
- this.$state.get('SELECT'),
2894
- this.$state.get('FROM'),
2895
- this.$state.get('TABLE_NAME'),
2896
- this.$state.get('JOIN'),
2897
- this.$state.get('WHERE'),
2898
- this.$state.get('GROUP_BY'),
2899
- this.$state.get('HAVING'),
2900
- this.$state.get('ORDER_BY'),
2901
- this.$state.get('LIMIT'),
2902
- this.$state.get('OFFSET')
2903
- ];
2904
- break;
2905
- }
2906
- return sql.filter(s => s !== '' || s == null).join(' ');
2907
- }
2908
2986
  _initialConnection() {
2909
2987
  this.$utils = utils_1.utils;
2910
2988
  this.$pool = (() => {