tspace-mysql 1.6.5 → 1.6.6-dev.1

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.
Files changed (52) hide show
  1. package/README.md +50 -3
  2. package/build/cli/models/make.js +2 -2
  3. package/build/cli/models/make.js.map +1 -1
  4. package/build/cli/models/model.d.ts +5 -1
  5. package/build/cli/models/model.js +31 -2
  6. package/build/cli/models/model.js.map +1 -1
  7. package/build/cli/tables/make.js +1 -1
  8. package/build/cli/tables/make.js.map +1 -1
  9. package/build/cli/tables/table.d.ts +5 -1
  10. package/build/cli/tables/table.js +32 -4
  11. package/build/cli/tables/table.js.map +1 -1
  12. package/build/lib/connection/index.d.ts +1 -0
  13. package/build/lib/connection/index.js +25 -16
  14. package/build/lib/connection/index.js.map +1 -1
  15. package/build/lib/connection/options.js +17 -15
  16. package/build/lib/connection/options.js.map +1 -1
  17. package/build/lib/constants/index.d.ts +96 -1
  18. package/build/lib/constants/index.js +3 -0
  19. package/build/lib/constants/index.js.map +1 -1
  20. package/build/lib/core/Abstracts/AbstractBuilder.d.ts +5 -7
  21. package/build/lib/core/Abstracts/AbstractBuilder.js +0 -5
  22. package/build/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
  23. package/build/lib/core/Abstracts/AbstractModel.d.ts +16 -16
  24. package/build/lib/core/Builder.d.ts +62 -3
  25. package/build/lib/core/Builder.js +188 -32
  26. package/build/lib/core/Builder.js.map +1 -1
  27. package/build/lib/core/DB.d.ts +15 -2
  28. package/build/lib/core/DB.js +17 -0
  29. package/build/lib/core/DB.js.map +1 -1
  30. package/build/lib/core/Handlers/Relation.d.ts +3 -3
  31. package/build/lib/core/Handlers/Relation.js +75 -50
  32. package/build/lib/core/Handlers/Relation.js.map +1 -1
  33. package/build/lib/core/Handlers/State.d.ts +130 -2
  34. package/build/lib/core/Handlers/State.js +4 -12
  35. package/build/lib/core/Handlers/State.js.map +1 -1
  36. package/build/lib/core/Model.d.ts +73 -14
  37. package/build/lib/core/Model.js +278 -123
  38. package/build/lib/core/Model.js.map +1 -1
  39. package/build/lib/core/Schema.js +1 -1
  40. package/build/lib/types.d.ts +3 -3
  41. package/build/lib/utils/index.d.ts +4 -1
  42. package/build/lib/utils/index.js +29 -4
  43. package/build/lib/utils/index.js.map +1 -1
  44. package/build/tests/01-Pool.test.js +2 -2
  45. package/build/tests/01-Pool.test.js.map +1 -1
  46. package/build/tests/02-DB.test.js.map +1 -1
  47. package/build/tests/03-Model.test.js +63 -17
  48. package/build/tests/03-Model.test.js.map +1 -1
  49. package/build/tests/schema-spec.d.ts +91 -26
  50. package/build/tests/schema-spec.js +18 -17
  51. package/build/tests/schema-spec.js.map +1 -1
  52. package/package.json +2 -1
@@ -117,6 +117,45 @@ class Model extends AbstractModel_1.AbstractModel {
117
117
  static get instance() {
118
118
  return new this();
119
119
  }
120
+ globalScope(callback) {
121
+ const model = new Model().table(this.getTableName());
122
+ const repository = callback(model);
123
+ if (repository instanceof Promise)
124
+ throw new Error('"whereQuery" is not supported a Promise');
125
+ if (!(repository instanceof Model))
126
+ throw new Error(`Unknown callback query: '${repository}'`);
127
+ this.$state.set('GLOBAL_SCOPE_QUERY', () => {
128
+ const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || [];
129
+ const select = (repository === null || repository === void 0 ? void 0 : repository.$state.get('SELECT')) || [];
130
+ const orderBy = (repository === null || repository === void 0 ? void 0 : repository.$state.get('ORDER_BY')) || [];
131
+ const limit = (repository === null || repository === void 0 ? void 0 : repository.$state.get('LIMIT')) || null;
132
+ if (where.length) {
133
+ this.$state.set('WHERE', [
134
+ ...this.$state.get('WHERE'),
135
+ [
136
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
137
+ ...where
138
+ ].join(' ')
139
+ ]);
140
+ }
141
+ if (select.length) {
142
+ this.$state.set('SELECT', [
143
+ ...this.$state.get('SELECT'),
144
+ ...select
145
+ ]);
146
+ }
147
+ if (orderBy.length) {
148
+ this.$state.set('ORDER_BY', [
149
+ ...this.$state.get('ORDER_BY'),
150
+ ...orderBy
151
+ ]);
152
+ }
153
+ if (limit != null) {
154
+ this.$state.set('LIMIT', limit);
155
+ }
156
+ });
157
+ return this;
158
+ }
120
159
  /**
121
160
  * The 'define' method is a special method that you can define within a model.
122
161
  * @example
@@ -722,6 +761,16 @@ class Model extends AbstractModel_1.AbstractModel {
722
761
  this.$state.set('SELECT', select);
723
762
  return this;
724
763
  }
764
+ /**
765
+ *
766
+ * @override
767
+ * @param {...string} columns
768
+ * @returns {this} this
769
+ */
770
+ hidden(...columns) {
771
+ this.$state.set('HIDDEN', columns);
772
+ return this;
773
+ }
725
774
  /**
726
775
  *
727
776
  * @override
@@ -774,15 +823,16 @@ class Model extends AbstractModel_1.AbstractModel {
774
823
  * @returns {this}
775
824
  */
776
825
  orderBy(column, order = 'ASC') {
777
- let c = String(column);
778
- if (c.includes(this.$constants('RAW')) || /\./.test(c)) {
779
- c = c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
826
+ const orderBy = [column].map(c => {
780
827
  if (/\./.test(c))
781
- c = this.bindColumn(c);
782
- }
828
+ return this.bindColumn(c.replace(/'/g, ''));
829
+ if (c.includes(this.$constants('RAW')))
830
+ return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
831
+ return this.bindColumn(c);
832
+ }).join(', ');
783
833
  this.$state.set('ORDER_BY', [
784
834
  ...this.$state.get('ORDER_BY'),
785
- `\`${c}\` ${order.toUpperCase()}`
835
+ `${orderBy} ${order.toUpperCase()}`
786
836
  ]);
787
837
  return this;
788
838
  }
@@ -797,10 +847,10 @@ class Model extends AbstractModel_1.AbstractModel {
797
847
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
798
848
  orderBy = columns.map(c => {
799
849
  if (/\./.test(c))
800
- return this.bindColumn(c);
850
+ return this.bindColumn(c.replace(/'/g, ''));
801
851
  if (c.includes(this.$constants('RAW')))
802
852
  return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
803
- return `\`${c}\``;
853
+ return this.bindColumn(c);
804
854
  }).join(', ');
805
855
  }
806
856
  this.$state.set('ORDER_BY', [
@@ -820,10 +870,10 @@ class Model extends AbstractModel_1.AbstractModel {
820
870
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
821
871
  orderBy = columns.map(c => {
822
872
  if (/\./.test(c))
823
- return this.bindColumn(c);
873
+ return this.bindColumn(c.replace(/'/g, ''));
824
874
  if (c.includes(this.$constants('RAW')))
825
875
  return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
826
- return `\`${c}\``;
876
+ return this.bindColumn(c);
827
877
  }).join(', ');
828
878
  }
829
879
  this.$state.set('ORDER_BY', [
@@ -843,10 +893,10 @@ class Model extends AbstractModel_1.AbstractModel {
843
893
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
844
894
  groupBy = columns.map(c => {
845
895
  if (/\./.test(c))
846
- return this.bindColumn(c);
896
+ return this.bindColumn(c.replace(/'/g, ''));
847
897
  if (c.includes(this.$constants('RAW')))
848
898
  return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
849
- return `\`${c}\``;
899
+ return this.bindColumn(c);
850
900
  }).join(', ');
851
901
  }
852
902
  this.$state.set('GROUP_BY', [
@@ -1046,7 +1096,7 @@ class Model extends AbstractModel_1.AbstractModel {
1046
1096
  if ((options === null || options === void 0 ? void 0 : options.offset) == null || !options.offset)
1047
1097
  newInstance.$state.set('OFFSET', '');
1048
1098
  if ((options === null || options === void 0 ? void 0 : options.groupBy) == null || !options.groupBy)
1049
- newInstance.$state.set('GROUP_BY', '');
1099
+ newInstance.$state.set('GROUP_BY', []);
1050
1100
  if ((options === null || options === void 0 ? void 0 : options.orderBy) == null || !options.orderBy)
1051
1101
  newInstance.$state.set('ORDER_BY', []);
1052
1102
  if ((options === null || options === void 0 ? void 0 : options.select) == null || !options.select)
@@ -1064,38 +1114,46 @@ class Model extends AbstractModel_1.AbstractModel {
1064
1114
  * @param {string} sql
1065
1115
  * @returns {this} this
1066
1116
  */
1067
- _queryStatement(sql) {
1068
- return __awaiter(this, void 0, void 0, function* () {
1117
+ _queryStatement(sql_1) {
1118
+ return __awaiter(this, arguments, void 0, function* (sql, { retry = false } = {}) {
1069
1119
  var _a;
1070
1120
  try {
1071
- if (this.$state.get('DEBUG'))
1072
- this.$utils.consoleDebug(sql);
1073
- if (this.$state.get('LOGGER')) {
1121
+ const logger = (results) => __awaiter(this, void 0, void 0, function* () {
1074
1122
  const selectRegex = /^SELECT\b/i;
1075
1123
  const loggerOptions = this.$state.get('LOGGER_OPTIONS');
1076
- if (selectRegex.test(sql) && loggerOptions.selected) {
1077
- yield this._checkTableLoggerIsExists().catch(err => null);
1078
- const result = yield this.$pool.query(sql);
1079
- yield new DB_1.DB(this.$state.get('TABLE_LOGGER'))
1080
- .create({
1081
- uuid: DB_1.DB.generateUUID(),
1082
- model: this.$state.get('MODEL_NAME'),
1083
- query: sql,
1084
- action: 'SELECT',
1085
- data: result.length
1086
- ? JSON.stringify(result.length === 1 ? result[0] : result)
1087
- : null,
1088
- changed: null,
1089
- createdAt: this.$utils.timestamp(),
1090
- updatedAt: this.$utils.timestamp()
1091
- })
1092
- .void()
1093
- .save()
1094
- .catch(_ => null);
1095
- return result;
1096
- }
1124
+ if (!(selectRegex.test(sql) && loggerOptions.selected))
1125
+ return;
1126
+ yield this._checkTableLoggerIsExists().catch(_ => null);
1127
+ yield new DB_1.DB(this.$state.get('TABLE_LOGGER'))
1128
+ .create({
1129
+ uuid: DB_1.DB.generateUUID(),
1130
+ model: this.$state.get('MODEL_NAME'),
1131
+ query: sql,
1132
+ action: 'SELECT',
1133
+ data: results.length
1134
+ ? JSON.stringify(results.length === 1 ? results[0] : results)
1135
+ : null,
1136
+ changed: null,
1137
+ createdAt: this.$utils.timestamp(),
1138
+ updatedAt: this.$utils.timestamp()
1139
+ })
1140
+ .void()
1141
+ .save()
1142
+ .catch(_ => null);
1143
+ });
1144
+ if (this.$state.get('DEBUG')) {
1145
+ this.$utils.consoleDebug(sql, retry);
1146
+ const startTime = +new Date();
1147
+ const result = yield this.$pool.query(sql);
1148
+ const endTime = +new Date();
1149
+ this.$utils.consoleExec(startTime, endTime);
1150
+ if (this.$state.get('LOGGER'))
1151
+ yield logger(result);
1152
+ return result;
1097
1153
  }
1098
1154
  const result = yield this.$pool.query(sql);
1155
+ if (this.$state.get('LOGGER'))
1156
+ yield logger(result);
1099
1157
  return result;
1100
1158
  }
1101
1159
  catch (error) {
@@ -1104,7 +1162,7 @@ class Model extends AbstractModel_1.AbstractModel {
1104
1162
  const retry = Number(this.$state.get('RETRY'));
1105
1163
  yield this._checkSchemaOrNextError(error, retry);
1106
1164
  this.$state.set('RETRY', retry + 1);
1107
- return yield this._queryStatement(sql);
1165
+ return yield this._queryStatement(sql, { retry: true });
1108
1166
  }
1109
1167
  });
1110
1168
  }
@@ -1251,7 +1309,8 @@ class Model extends AbstractModel_1.AbstractModel {
1251
1309
  });
1252
1310
  }
1253
1311
  /**
1254
- * Assign table name
1312
+ * The 'table' method is used to set the table name.
1313
+ *
1255
1314
  * @param {string} table table name
1256
1315
  * @returns {this} this
1257
1316
  */
@@ -1260,8 +1319,19 @@ class Model extends AbstractModel_1.AbstractModel {
1260
1319
  return this;
1261
1320
  }
1262
1321
  /**
1263
- * Assign ignore delete_at in model
1264
- * @param {boolean} condition
1322
+ * The 'from' method is used to set the table name.
1323
+ *
1324
+ * @param {string} table table name
1325
+ * @returns {this} this
1326
+ */
1327
+ from(table) {
1328
+ this.$state.set('TABLE_NAME', `\`${table}\``);
1329
+ return this;
1330
+ }
1331
+ /**
1332
+ * The 'disableSoftDelete' method is used to disable the soft delete.
1333
+ *
1334
+ * @param {boolean} condition
1265
1335
  * @returns {this} this
1266
1336
  */
1267
1337
  disableSoftDelete(condition = false) {
@@ -1269,7 +1339,16 @@ class Model extends AbstractModel_1.AbstractModel {
1269
1339
  return this;
1270
1340
  }
1271
1341
  /**
1272
- * The 'disableVoid' method is used to ignore void.
1342
+ * The 'ignoreSoftDelete' method is used to disable the soft delete.
1343
+ * @param {boolean} condition
1344
+ * @returns {this} this
1345
+ */
1346
+ ignoreSoftDelete(condition = false) {
1347
+ this.$state.set('SOFT_DELETE', condition);
1348
+ return this;
1349
+ }
1350
+ /**
1351
+ * The 'disableVoid' method is used to disable void.
1273
1352
  *
1274
1353
  * @returns {this} this
1275
1354
  */
@@ -1278,12 +1357,30 @@ class Model extends AbstractModel_1.AbstractModel {
1278
1357
  return this;
1279
1358
  }
1280
1359
  /**
1281
- * Assign ignore delete_at in model
1282
- * @param {boolean} condition
1360
+ * The 'ignoreVoid' method is used to ignore void.
1361
+ *
1283
1362
  * @returns {this} this
1284
1363
  */
1285
- ignoreSoftDelete(condition = false) {
1286
- this.$state.set('SOFT_DELETE', condition);
1364
+ ignoreVoid() {
1365
+ this.$state.set('VOID', false);
1366
+ return this;
1367
+ }
1368
+ /**
1369
+ * The 'disabledGlobalScope' method is used to disable globalScope.
1370
+ *
1371
+ * @returns {this} this
1372
+ */
1373
+ disabledGlobalScope(condition = false) {
1374
+ this.$state.set('GLOBAL_SCOPE', condition);
1375
+ return this;
1376
+ }
1377
+ /**
1378
+ * The 'ignoreGlobalScope' method is used to disable globalScope.
1379
+ *
1380
+ * @returns {this} this
1381
+ */
1382
+ ignoreGlobalScope(condition = false) {
1383
+ this.$state.set('GLOBAL_SCOPE', condition);
1287
1384
  return this;
1288
1385
  }
1289
1386
  /**
@@ -1324,10 +1421,9 @@ class Model extends AbstractModel_1.AbstractModel {
1324
1421
  *
1325
1422
  */
1326
1423
  with(...nameRelations) {
1327
- var _a;
1328
1424
  if (!nameRelations.length)
1329
1425
  return this;
1330
- this.$state.set('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'default'));
1426
+ this.$state.set('RELATIONS', this.$relation.apply(nameRelations, 'default'));
1331
1427
  return this;
1332
1428
  }
1333
1429
  /**
@@ -1370,10 +1466,9 @@ class Model extends AbstractModel_1.AbstractModel {
1370
1466
  * @returns {this} this
1371
1467
  */
1372
1468
  withAll(...nameRelations) {
1373
- var _a;
1374
1469
  if (!nameRelations.length)
1375
1470
  return this;
1376
- this.$state.set('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'all'));
1471
+ this.$state.set('RELATIONS', this.$relation.apply(nameRelations, 'all'));
1377
1472
  return this;
1378
1473
  }
1379
1474
  /**
@@ -1395,10 +1490,9 @@ class Model extends AbstractModel_1.AbstractModel {
1395
1490
  * @returns {this} this
1396
1491
  */
1397
1492
  withCount(...nameRelations) {
1398
- var _a;
1399
1493
  if (!nameRelations.length)
1400
1494
  return this;
1401
- this.$state.set('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'count'));
1495
+ this.$state.set('RELATIONS', this.$relation.apply(nameRelations, 'count'));
1402
1496
  return this;
1403
1497
  }
1404
1498
  /**
@@ -1408,10 +1502,9 @@ class Model extends AbstractModel_1.AbstractModel {
1408
1502
  * @returns {this} this
1409
1503
  */
1410
1504
  relationsCount(...nameRelations) {
1411
- var _a;
1412
1505
  if (!nameRelations.length)
1413
1506
  return this;
1414
- this.$state.set('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'count'));
1507
+ this.$state.set('RELATIONS', this.$relation.apply(nameRelations, 'count'));
1415
1508
  return this;
1416
1509
  }
1417
1510
  /**
@@ -1424,10 +1517,9 @@ class Model extends AbstractModel_1.AbstractModel {
1424
1517
  * @returns {this} this
1425
1518
  */
1426
1519
  withTrashed(...nameRelations) {
1427
- var _a;
1428
1520
  if (!nameRelations.length)
1429
1521
  return this;
1430
- this.$state.set('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'trashed'));
1522
+ this.$state.set('RELATIONS', this.$relation.apply(nameRelations, 'trashed'));
1431
1523
  return this;
1432
1524
  }
1433
1525
  /**
@@ -1470,11 +1562,10 @@ class Model extends AbstractModel_1.AbstractModel {
1470
1562
  * await new User().withExists('posts').findMany()
1471
1563
  */
1472
1564
  withExists(...nameRelations) {
1473
- var _a;
1474
1565
  if (!nameRelations.length)
1475
1566
  return this;
1476
1567
  this.$state.set('RELATIONS_EXISTS', true);
1477
- this.$state.set('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'exists'));
1568
+ this.$state.set('RELATIONS', this.$relation.apply(nameRelations, 'exists'));
1478
1569
  return this;
1479
1570
  }
1480
1571
  /**
@@ -1587,13 +1678,12 @@ class Model extends AbstractModel_1.AbstractModel {
1587
1678
  * @returns {this} this
1588
1679
  */
1589
1680
  withQuery(nameRelation, callback, options = { pivot: false }) {
1590
- var _a, _b;
1591
1681
  this.with(nameRelation);
1592
1682
  if (options.pivot) {
1593
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.callbackPivot(String(nameRelation), callback);
1683
+ this.$relation.callbackPivot(String(nameRelation), callback);
1594
1684
  return this;
1595
1685
  }
1596
- (_b = this.$relation) === null || _b === void 0 ? void 0 : _b.callback(String(nameRelation), callback);
1686
+ this.$relation.callback(String(nameRelation), callback);
1597
1687
  return this;
1598
1688
  }
1599
1689
  /**
@@ -1630,12 +1720,12 @@ class Model extends AbstractModel_1.AbstractModel {
1630
1720
  * }
1631
1721
  *
1632
1722
  * await new User().relations('posts')
1633
- * .relationsQuery('posts', (query : Post) => {
1723
+ * .relationQuery('posts', (query : Post) => {
1634
1724
  * return query.relations('comments','user')
1635
- * .relationsQuery('comments', (query : Comment) => {
1725
+ * .relationQuery('comments', (query : Comment) => {
1636
1726
  * return query.relations('user','post')
1637
1727
  * })
1638
- * .relationsQuery('user', (query : User) => {
1728
+ * .relationQuery('user', (query : User) => {
1639
1729
  * return query.relations('posts').relationsQuery('posts',(query : Post)=> {
1640
1730
  * return query.relations('comments','user')
1641
1731
  * // relation n, n, ...n
@@ -1657,8 +1747,7 @@ class Model extends AbstractModel_1.AbstractModel {
1657
1747
  * @returns {Model} model instance
1658
1748
  */
1659
1749
  findWithQuery(nameRelation) {
1660
- var _a;
1661
- const instanceCallback = (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.returnCallback(nameRelation);
1750
+ const instanceCallback = this.$relation.returnCallback(String(nameRelation));
1662
1751
  return instanceCallback == null ? null : instanceCallback;
1663
1752
  }
1664
1753
  /**
@@ -1678,8 +1767,7 @@ class Model extends AbstractModel_1.AbstractModel {
1678
1767
  * @returns {this} this
1679
1768
  */
1680
1769
  hasOne({ name, as, model, localKey, foreignKey, freezeTable }) {
1681
- var _a;
1682
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.hasOne({
1770
+ this.$relation.hasOne({
1683
1771
  name,
1684
1772
  as,
1685
1773
  model,
@@ -1706,8 +1794,7 @@ class Model extends AbstractModel_1.AbstractModel {
1706
1794
  * @returns {this} this
1707
1795
  */
1708
1796
  hasMany({ name, as, model, localKey, foreignKey, freezeTable }) {
1709
- var _a;
1710
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.hasMany({
1797
+ this.$relation.hasMany({
1711
1798
  name,
1712
1799
  as,
1713
1800
  model,
@@ -1734,8 +1821,7 @@ class Model extends AbstractModel_1.AbstractModel {
1734
1821
  * @returns {this} this
1735
1822
  */
1736
1823
  belongsTo({ name, as, model, localKey, foreignKey, freezeTable }) {
1737
- var _a;
1738
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.belongsTo({
1824
+ this.$relation.belongsTo({
1739
1825
  name,
1740
1826
  as,
1741
1827
  model,
@@ -1765,8 +1851,7 @@ class Model extends AbstractModel_1.AbstractModel {
1765
1851
  * @returns {this} this
1766
1852
  */
1767
1853
  belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot }) {
1768
- var _a;
1769
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.belongsToMany({
1854
+ this.$relation.belongsToMany({
1770
1855
  name,
1771
1856
  as,
1772
1857
  model,
@@ -1794,8 +1879,7 @@ class Model extends AbstractModel_1.AbstractModel {
1794
1879
  * @returns {this} this
1795
1880
  */
1796
1881
  hasOneBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
1797
- var _a;
1798
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.hasOneBuilder({
1882
+ this.$relation.hasOneBuilder({
1799
1883
  name,
1800
1884
  as,
1801
1885
  model,
@@ -1820,8 +1904,7 @@ class Model extends AbstractModel_1.AbstractModel {
1820
1904
  * @returns {this} this
1821
1905
  */
1822
1906
  hasManyBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
1823
- var _a;
1824
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.hasManyBuilder({
1907
+ this.$relation.hasManyBuilder({
1825
1908
  name,
1826
1909
  as,
1827
1910
  model,
@@ -1845,8 +1928,7 @@ class Model extends AbstractModel_1.AbstractModel {
1845
1928
  * @returns {this} this
1846
1929
  */
1847
1930
  belongsToBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
1848
- var _a;
1849
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.belongsToBuilder({
1931
+ this.$relation.belongsToBuilder({
1850
1932
  name,
1851
1933
  as,
1852
1934
  model,
@@ -1871,8 +1953,7 @@ class Model extends AbstractModel_1.AbstractModel {
1871
1953
  * @returns {this} this
1872
1954
  */
1873
1955
  belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot }, callback) {
1874
- var _a;
1875
- (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.belongsToManyBuilder({
1956
+ this.$relation.belongsToManyBuilder({
1876
1957
  name,
1877
1958
  as,
1878
1959
  model,
@@ -2005,6 +2086,60 @@ class Model extends AbstractModel_1.AbstractModel {
2005
2086
  ]);
2006
2087
  return this;
2007
2088
  }
2089
+ /**
2090
+ * @override
2091
+ * @param {string} column
2092
+ * @param {number} day
2093
+ * @returns {this}
2094
+ */
2095
+ whereDay(column, day) {
2096
+ this.$state.set('WHERE', [
2097
+ ...this.$state.get('WHERE'),
2098
+ [
2099
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
2100
+ `DAY(${this.bindColumn(String(column))})`,
2101
+ `=`,
2102
+ `'${`00${this.$utils.escape(day)}`.slice(-2)}'`
2103
+ ].join(' ')
2104
+ ]);
2105
+ return this;
2106
+ }
2107
+ /**
2108
+ * @override
2109
+ * @param {string} column
2110
+ * @param {number} month
2111
+ * @returns {this}
2112
+ */
2113
+ whereMonth(column, month) {
2114
+ this.$state.set('WHERE', [
2115
+ ...this.$state.get('WHERE'),
2116
+ [
2117
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
2118
+ `MONTH(${this.bindColumn(String(column))})`,
2119
+ `=`,
2120
+ `'${`00${this.$utils.escape(month)}`.slice(-2)}'`
2121
+ ].join(' ')
2122
+ ]);
2123
+ return this;
2124
+ }
2125
+ /**
2126
+ * @override
2127
+ * @param {string} column
2128
+ * @param {number} year
2129
+ * @returns {this}
2130
+ */
2131
+ whereYear(column, year) {
2132
+ this.$state.set('WHERE', [
2133
+ ...this.$state.get('WHERE'),
2134
+ [
2135
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
2136
+ `YEAR(${this.bindColumn(String(column))})`,
2137
+ `=`,
2138
+ `'${`0000${this.$utils.escape(year)}`.slice(-4)}'`
2139
+ ].join(' ')
2140
+ ]);
2141
+ return this;
2142
+ }
2008
2143
  /**
2009
2144
  * @override
2010
2145
  * @param {Object} columns
@@ -2791,7 +2926,7 @@ class Model extends AbstractModel_1.AbstractModel {
2791
2926
  */
2792
2927
  first(cb) {
2793
2928
  return __awaiter(this, void 0, void 0, function* () {
2794
- var _a, _b;
2929
+ var _a;
2795
2930
  this._validateMethod('first');
2796
2931
  if (this.$state.get('VOID'))
2797
2932
  return this._resultHandler(undefined);
@@ -2800,7 +2935,7 @@ class Model extends AbstractModel_1.AbstractModel {
2800
2935
  this.limit(1);
2801
2936
  let sql = this._queryBuilder().select();
2802
2937
  if (this.$state.get('RELATIONS_EXISTS'))
2803
- sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
2938
+ sql = String(this.$relation.loadExists());
2804
2939
  if (cb) {
2805
2940
  const callbackSql = cb(sql);
2806
2941
  if (callbackSql == null || callbackSql === '') {
@@ -2830,18 +2965,18 @@ class Model extends AbstractModel_1.AbstractModel {
2830
2965
  */
2831
2966
  firstOrError(message, options) {
2832
2967
  return __awaiter(this, void 0, void 0, function* () {
2833
- var _a, _b;
2968
+ var _a;
2834
2969
  this._validateMethod('firstOrError');
2835
2970
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2836
2971
  this.select(...yield this.exceptColumns());
2837
2972
  this.limit(1);
2838
2973
  let sql = this._queryBuilder().select();
2839
2974
  if (this.$state.get('RELATIONS_EXISTS'))
2840
- sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
2975
+ sql = String(this.$relation.loadExists());
2841
2976
  return yield this._execute({
2842
2977
  sql,
2843
2978
  type: 'FIRST_OR_ERROR',
2844
- message,
2979
+ message: message == null ? 'The data does not exist.' : message,
2845
2980
  options
2846
2981
  });
2847
2982
  });
@@ -2864,7 +2999,7 @@ class Model extends AbstractModel_1.AbstractModel {
2864
2999
  */
2865
3000
  get(cb) {
2866
3001
  return __awaiter(this, void 0, void 0, function* () {
2867
- var _a, _b;
3002
+ var _a;
2868
3003
  this._validateMethod('get');
2869
3004
  if (this.$state.get('VOID'))
2870
3005
  return [];
@@ -2872,7 +3007,7 @@ class Model extends AbstractModel_1.AbstractModel {
2872
3007
  this.select(...yield this.exceptColumns());
2873
3008
  let sql = this._queryBuilder().select();
2874
3009
  if (this.$state.get('RELATIONS_EXISTS'))
2875
- sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
3010
+ sql = String(this.$relation.loadExists());
2876
3011
  if (cb) {
2877
3012
  const callbackSql = cb(sql);
2878
3013
  if (callbackSql == null || callbackSql === '') {
@@ -2906,7 +3041,7 @@ class Model extends AbstractModel_1.AbstractModel {
2906
3041
  */
2907
3042
  pagination(paginationOptions) {
2908
3043
  return __awaiter(this, void 0, void 0, function* () {
2909
- var _a, _b;
3044
+ var _a;
2910
3045
  this._validateMethod('pagination');
2911
3046
  let limit = 15;
2912
3047
  let page = 1;
@@ -2914,7 +3049,6 @@ class Model extends AbstractModel_1.AbstractModel {
2914
3049
  limit = ((paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit);
2915
3050
  page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
2916
3051
  }
2917
- limit = limit > 1000 ? 1000 : limit;
2918
3052
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2919
3053
  this.select(...yield this.exceptColumns());
2920
3054
  const offset = (page - 1) * limit;
@@ -2924,7 +3058,7 @@ class Model extends AbstractModel_1.AbstractModel {
2924
3058
  this.offset(offset);
2925
3059
  let sql = this._queryBuilder().select();
2926
3060
  if (this.$state.get('RELATIONS_EXISTS'))
2927
- sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
3061
+ sql = String(this.$relation.loadExists());
2928
3062
  return yield this._execute({
2929
3063
  sql,
2930
3064
  type: 'PAGINATION'
@@ -3379,7 +3513,9 @@ class Model extends AbstractModel_1.AbstractModel {
3379
3513
  */
3380
3514
  faker(rows, callback) {
3381
3515
  return __awaiter(this, void 0, void 0, function* () {
3382
- const data = [];
3516
+ if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
3517
+ throw this._assertError("Unknow table.");
3518
+ }
3383
3519
  const schemaModel = this.getSchemaModel();
3384
3520
  const fields = schemaModel == null
3385
3521
  ? yield this.getSchema()
@@ -3389,25 +3525,34 @@ class Model extends AbstractModel_1.AbstractModel {
3389
3525
  Type: value.type
3390
3526
  };
3391
3527
  });
3392
- if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
3393
- throw this._assertError("Unknow table.");
3394
- }
3528
+ const fakers = [];
3529
+ const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
3530
+ const uuid = this.$state.get('UUID_FORMAT');
3531
+ const passed = (field) => ['id', '_id', deletedAt].some(p => field === p);
3395
3532
  for (let row = 0; row < rows; row++) {
3396
3533
  let columnAndValue = {};
3397
3534
  for (const { Field: field, Type: type } of fields) {
3398
- const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
3399
- const passed = ['id', '_id', 'uuid', deletedAt].some(p => field === p);
3400
- if (passed)
3535
+ if (passed(field))
3401
3536
  continue;
3402
- columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
3537
+ columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: field === uuid
3538
+ ? this.$utils.faker('uuid')
3539
+ : this.$utils.faker(type) });
3403
3540
  }
3404
3541
  if (callback) {
3405
- data.push(callback(columnAndValue, row));
3542
+ fakers.push(callback(columnAndValue, row));
3406
3543
  continue;
3407
3544
  }
3408
- data.push(columnAndValue);
3545
+ fakers.push(columnAndValue);
3546
+ }
3547
+ const chunkedData = this.$utils.chunkArray([...fakers], 500);
3548
+ const promises = [];
3549
+ const table = this.getTableName();
3550
+ for (const data of chunkedData) {
3551
+ promises.push(() => {
3552
+ return new Model().table(table).debug(this.$state.get('DEBUG')).createMultiple([...data]).void().save();
3553
+ });
3409
3554
  }
3410
- yield this.createMultiple(data).void().save();
3555
+ yield Promise.allSettled(promises.map((v) => v()));
3411
3556
  return;
3412
3557
  });
3413
3558
  }
@@ -3586,18 +3731,26 @@ class Model extends AbstractModel_1.AbstractModel {
3586
3731
  return this;
3587
3732
  }
3588
3733
  _handleSoftDelete() {
3589
- if (this.$state.get('SOFT_DELETE')) {
3590
- const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
3591
- const wheres = this.$state.get('WHERE');
3592
- const softDeleteIsNull = [
3593
- this.bindColumn(`${this.getTableName()}.${deletedAt}`),
3594
- this.$constants('IS_NULL')
3595
- ].join(' ');
3596
- if (!wheres.some((where) => where.includes(softDeleteIsNull))) {
3597
- this.whereNull(deletedAt);
3598
- return this;
3599
- }
3734
+ if (!this.$state.get('SOFT_DELETE'))
3600
3735
  return this;
3736
+ const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
3737
+ const wheres = this.$state.get('WHERE');
3738
+ const softDeleteIsNull = [
3739
+ this.bindColumn(`${this.getTableName()}.${deletedAt}`),
3740
+ this.$constants('IS_NULL')
3741
+ ].join(' ');
3742
+ if (!wheres.some((where) => where.includes(softDeleteIsNull))) {
3743
+ this.whereNull(deletedAt);
3744
+ return this;
3745
+ }
3746
+ return this;
3747
+ }
3748
+ _handleGlobalScope() {
3749
+ if (!this.$state.get('GLOBAL_SCOPE'))
3750
+ return this;
3751
+ const globalScopeQuery = this.$state.get('GLOBAL_SCOPE_QUERY');
3752
+ if (globalScopeQuery != null && typeof globalScopeQuery === 'function') {
3753
+ globalScopeQuery();
3601
3754
  }
3602
3755
  return this;
3603
3756
  }
@@ -3629,8 +3782,9 @@ class Model extends AbstractModel_1.AbstractModel {
3629
3782
  * @override
3630
3783
  */
3631
3784
  _queryBuilder() {
3632
- this._handleSoftDelete();
3785
+ this._handleGlobalScope();
3633
3786
  this._handleSelect();
3787
+ this._handleSoftDelete();
3634
3788
  return this._buildQueryStatement();
3635
3789
  }
3636
3790
  _showOnly(data) {
@@ -3742,13 +3896,14 @@ class Model extends AbstractModel_1.AbstractModel {
3742
3896
  }
3743
3897
  _execute(_a) {
3744
3898
  return __awaiter(this, arguments, void 0, function* ({ sql, type, message, options }) {
3745
- var _b, _c;
3899
+ var _b;
3746
3900
  let result = yield this._queryStatement(sql);
3747
3901
  if (!result.length)
3748
3902
  return this._returnEmpty(type, result, message, options);
3749
3903
  const relations = this.$state.get('RELATIONS');
3750
3904
  for (const relation of relations) {
3751
- result = (_c = yield ((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.load(result, relation))) !== null && _c !== void 0 ? _c : [];
3905
+ const loaded = (_b = yield this.$relation.load(result, relation)) !== null && _b !== void 0 ? _b : [];
3906
+ result = loaded;
3752
3907
  }
3753
3908
  if (this.$state.get('HIDDEN').length)
3754
3909
  this._hiddenColumnModel(result);
@@ -3877,10 +4032,10 @@ class Model extends AbstractModel_1.AbstractModel {
3877
4032
  for (const d of data) {
3878
4033
  for (const r of this.$state.get('RELATION')) {
3879
4034
  d[`$${r.name}`] = (cb) => __awaiter(this, void 0, void 0, function* () {
3880
- var _f, _g;
4035
+ var _f;
3881
4036
  const query = cb ? cb(new r.model()) : new r.model();
3882
4037
  r.query = query;
3883
- const dataFromRelation = (_g = yield ((_f = this.$relation) === null || _f === void 0 ? void 0 : _f.load([d], r))) !== null && _g !== void 0 ? _g : [];
4038
+ const dataFromRelation = (_f = yield this.$relation.load([d], r)) !== null && _f !== void 0 ? _f : [];
3884
4039
  const relationIsHasOneOrBelongsTo = [
3885
4040
  this.$constants('RELATIONSHIP').hasOne,
3886
4041
  this.$constants('RELATIONSHIP').belongsTo
@@ -4139,11 +4294,11 @@ class Model extends AbstractModel_1.AbstractModel {
4139
4294
  var _a;
4140
4295
  let values = [];
4141
4296
  let columns = Object.keys((_a = [...data]) === null || _a === void 0 ? void 0 : _a.shift()).map((column) => column);
4297
+ const hasTimestamp = this.$state.get('TIMESTAMP');
4298
+ const format = this.$state.get('TIMESTAMP_FORMAT');
4142
4299
  for (let objects of data) {
4143
4300
  this.$utils.covertDataToDateIfDate(data);
4144
- const hasTimestamp = this.$state.get('TIMESTAMP');
4145
4301
  if (hasTimestamp) {
4146
- const format = this.$state.get('TIMESTAMP_FORMAT');
4147
4302
  const createdAt = this._valuePattern(format.CREATED_AT);
4148
4303
  const updatedAt = this._valuePattern(format.UPDATED_AT);
4149
4304
  objects = Object.assign(Object.assign({}, objects), { [createdAt]: this.$utils.timestamp(), [updatedAt]: this.$utils.timestamp() });