tspace-mysql 1.9.0 → 1.9.1-beta.2

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 (118) hide show
  1. package/README.md +4580 -133
  2. package/dist/lib/config/index.d.ts +2 -2
  3. package/dist/lib/config/index.js +24 -16
  4. package/dist/lib/config/index.js.map +1 -1
  5. package/dist/lib/constants/index.d.ts +10 -1
  6. package/dist/lib/constants/index.js +11 -2
  7. package/dist/lib/constants/index.js.map +1 -1
  8. package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +27 -23
  9. package/dist/lib/core/Abstracts/AbstractBuilder.js +11 -13
  10. package/dist/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
  11. package/dist/lib/core/Abstracts/AbstractDB.d.ts +5 -2
  12. package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
  13. package/dist/lib/core/Abstracts/AbstractModel.d.ts +7 -2
  14. package/dist/lib/core/Abstracts/AbstractModel.js.map +1 -1
  15. package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
  16. package/dist/lib/core/Blueprint.d.ts +37 -17
  17. package/dist/lib/core/Blueprint.js +38 -14
  18. package/dist/lib/core/Blueprint.js.map +1 -1
  19. package/dist/lib/core/Builder.d.ts +314 -42
  20. package/dist/lib/core/Builder.js +686 -391
  21. package/dist/lib/core/Builder.js.map +1 -1
  22. package/dist/lib/core/Cache/DBCache.js +19 -20
  23. package/dist/lib/core/Cache/DBCache.js.map +1 -1
  24. package/dist/lib/core/Cache/MemoryCache.js +3 -3
  25. package/dist/lib/core/Cache/MemoryCache.js.map +1 -1
  26. package/dist/lib/core/Cache/RedisCache.js +27 -17
  27. package/dist/lib/core/Cache/RedisCache.js.map +1 -1
  28. package/dist/lib/core/Cache/index.js +37 -17
  29. package/dist/lib/core/Cache/index.js.map +1 -1
  30. package/dist/lib/core/Contracts/AlterTable.d.ts +152 -0
  31. package/dist/lib/core/Contracts/AlterTable.js +243 -0
  32. package/dist/lib/core/Contracts/AlterTable.js.map +1 -0
  33. package/dist/lib/core/Contracts/Audit.d.ts +1 -1
  34. package/dist/lib/core/Contracts/Audit.js +2 -5
  35. package/dist/lib/core/Contracts/Audit.js.map +1 -1
  36. package/dist/lib/core/Contracts/Logger.d.ts +1 -1
  37. package/dist/lib/core/Contracts/Logger.js.map +1 -1
  38. package/dist/lib/core/DB.d.ts +103 -11
  39. package/dist/lib/core/DB.js +158 -32
  40. package/dist/lib/core/DB.js.map +1 -1
  41. package/dist/lib/core/Decorator.d.ts +144 -16
  42. package/dist/lib/core/Decorator.js +156 -14
  43. package/dist/lib/core/Decorator.js.map +1 -1
  44. package/dist/lib/core/Driver/index.d.ts +50 -14
  45. package/dist/lib/core/Driver/index.js +13 -9
  46. package/dist/lib/core/Driver/index.js.map +1 -1
  47. package/dist/lib/core/Driver/mariadb/MariadbDriver.js +84 -29
  48. package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
  49. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +48 -12
  50. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +189 -28
  51. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
  52. package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +24 -0
  53. package/dist/lib/core/Driver/mongodb/MongodbDriver.js +261 -0
  54. package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -0
  55. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +141 -0
  56. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +571 -0
  57. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -0
  58. package/dist/lib/core/Driver/mysql/MysqlDriver.js +115 -97
  59. package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
  60. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +48 -12
  61. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +189 -28
  62. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
  63. package/dist/lib/core/Driver/postgres/PostgresDriver.js +106 -73
  64. package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
  65. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +53 -12
  66. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +318 -48
  67. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
  68. package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +21 -0
  69. package/dist/lib/core/Driver/sqlite/SqliteDriver.js +213 -0
  70. package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -0
  71. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +146 -0
  72. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +705 -0
  73. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -0
  74. package/dist/lib/core/Join.d.ts +63 -5
  75. package/dist/lib/core/Join.js +92 -15
  76. package/dist/lib/core/Join.js.map +1 -1
  77. package/dist/lib/core/JoinModel.d.ts +65 -9
  78. package/dist/lib/core/JoinModel.js +81 -52
  79. package/dist/lib/core/JoinModel.js.map +1 -1
  80. package/dist/lib/core/Meta.d.ts +22 -16
  81. package/dist/lib/core/Meta.js +28 -21
  82. package/dist/lib/core/Meta.js.map +1 -1
  83. package/dist/lib/core/Model.d.ts +644 -134
  84. package/dist/lib/core/Model.js +1361 -631
  85. package/dist/lib/core/Model.js.map +1 -1
  86. package/dist/lib/{tools/index.d.ts → core/Package.d.ts} +11 -3
  87. package/dist/lib/{tools/index.js → core/Package.js} +20 -7
  88. package/dist/lib/core/Package.js.map +1 -0
  89. package/dist/lib/core/Pool.js +42 -30
  90. package/dist/lib/core/Pool.js.map +1 -1
  91. package/dist/lib/core/Queue.d.ts +249 -0
  92. package/dist/lib/core/Queue.js +726 -0
  93. package/dist/lib/core/Queue.js.map +1 -0
  94. package/dist/lib/core/RelationManager.d.ts +4 -4
  95. package/dist/lib/core/RelationManager.js +168 -41
  96. package/dist/lib/core/RelationManager.js.map +1 -1
  97. package/dist/lib/core/Repository.d.ts +73 -207
  98. package/dist/lib/core/Repository.js +122 -234
  99. package/dist/lib/core/Repository.js.map +1 -1
  100. package/dist/lib/core/Schema.d.ts +211 -1
  101. package/dist/lib/core/Schema.js +370 -65
  102. package/dist/lib/core/Schema.js.map +1 -1
  103. package/dist/lib/core/StateManager.d.ts +31 -12
  104. package/dist/lib/core/StateManager.js +11 -5
  105. package/dist/lib/core/StateManager.js.map +1 -1
  106. package/dist/lib/core/UtilityTypes.d.ts +103 -81
  107. package/dist/lib/core/UtilityTypes.js.map +1 -1
  108. package/dist/lib/core/index.d.ts +4 -0
  109. package/dist/lib/core/index.js +6 -2
  110. package/dist/lib/core/index.js.map +1 -1
  111. package/dist/lib/types/decorator/index.d.ts +1 -1
  112. package/dist/lib/types/index.d.ts +120 -30
  113. package/dist/lib/types/repository/index.d.ts +68 -37
  114. package/dist/lib/utils/index.d.ts +13 -2
  115. package/dist/lib/utils/index.js +81 -2
  116. package/dist/lib/utils/index.js.map +1 -1
  117. package/package.json +14 -6
  118. package/dist/lib/tools/index.js.map +0 -1
@@ -24,7 +24,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
24
24
  }
25
25
  /**
26
26
  * The 'driver' method is used to get current driver
27
- * @returns {string} driver
27
+ * @static
28
+ * @returns {TDriver} driver
29
+ */
30
+ static driver() {
31
+ return new this().driver();
32
+ }
33
+ /**
34
+ * The 'driver' method is used to get current driver
35
+ * @returns {TDriver} driver
28
36
  */
29
37
  driver() {
30
38
  return this.$driver;
@@ -37,16 +45,83 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
37
45
  return this.$database;
38
46
  }
39
47
  /**
40
- * The 'rowLock' method is used to row level locks
48
+ * The 'rowLock' method is used to row level locks (`FOR UPDATE` or `FOR SHARE`) to the query.
41
49
  *
42
50
  * @param {string} mode
43
51
  * @returns {this} this
44
52
  */
45
- rowLock(mode) {
53
+ rowLock(mode, opts = {}) {
46
54
  if (!["FOR_UPDATE", "FOR_SHARE"].includes(mode)) {
47
55
  throw new Error(`Invalid lock mode: ${mode}`);
48
56
  }
49
- this.$state.set("ROW_LEVEL_LOCK", mode);
57
+ if (opts.skipLocked && opts.nowait) {
58
+ throw new Error("Cannot use skipLocked and nowait together");
59
+ }
60
+ this.$state.set("ROW_LEVEL_LOCK", {
61
+ mode,
62
+ skipLocked: !!opts.skipLocked,
63
+ nowait: !!opts.nowait
64
+ });
65
+ return this;
66
+ }
67
+ /**
68
+ * The 'forUpdate' method is used to applies a row-level exclusive lock (`FOR UPDATE`) to the query.
69
+ *
70
+ * This lock prevents other transactions from modifying or acquiring
71
+ * conflicting locks on the selected rows until the current transaction
72
+ * is committed or rolled back.
73
+ *
74
+ * Commonly used in queue systems or critical sections to safely
75
+ * select and update rows without race conditions.
76
+ *
77
+ * @param {Object} [opts] - Locking options
78
+ * @param {boolean} [opts.skipLocked=false] - If true, skips rows that are already locked by other transactions.
79
+ * Useful for building non-blocking workers (e.g., job queues).
80
+ * @param {boolean} [opts.nowait=false] - If true, throws an error immediately if the lock cannot be acquired.
81
+ *
82
+ * @throws {Error} If both `skipLocked` and `nowait` are enabled at the same time.
83
+ *
84
+ * @returns {this} Returns the query builder instance for chaining.
85
+ */
86
+ forUpdate(opts = {}) {
87
+ if (opts.skipLocked && opts.nowait) {
88
+ throw new Error("Cannot use skipLocked and nowait together");
89
+ }
90
+ this.$state.set("ROW_LEVEL_LOCK", {
91
+ mode: 'FOR_UPDATE',
92
+ skipLocked: !!opts.skipLocked,
93
+ nowait: !!opts.nowait
94
+ });
95
+ return this;
96
+ }
97
+ /**
98
+ * The 'forShare' method is used to applies a row-level exclusive lock (`FOR SHARE`) to the query.
99
+ *
100
+ * This lock prevents other transactions from modifying or acquiring
101
+ * conflicting locks on the selected rows until the current transaction
102
+ * is committed or rolled back.
103
+ *
104
+ * Commonly used in queue systems or critical sections to safely
105
+ * select and update rows without race conditions.
106
+ *
107
+ * @param {Object} [opts] - Locking options
108
+ * @param {boolean} [opts.skipLocked=false] - If true, skips rows that are already locked by other transactions.
109
+ * Useful for building non-blocking workers (e.g., job queues).
110
+ * @param {boolean} [opts.nowait=false] - If true, throws an error immediately if the lock cannot be acquired.
111
+ *
112
+ * @throws {Error} If both `skipLocked` and `nowait` are enabled at the same time.
113
+ *
114
+ * @returns {this} Returns the query builder instance for chaining.
115
+ */
116
+ forShare(opts = {}) {
117
+ if (opts.skipLocked && opts.nowait) {
118
+ throw new Error("Cannot use skipLocked and nowait together");
119
+ }
120
+ this.$state.set("ROW_LEVEL_LOCK", {
121
+ mode: 'FOR_SHARE',
122
+ skipLocked: !!opts.skipLocked,
123
+ nowait: !!opts.nowait
124
+ });
50
125
  return this;
51
126
  }
52
127
  /**
@@ -80,7 +155,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
80
155
  if (options?.offset != null && options.offset)
81
156
  this.$state.set("OFFSET", null);
82
157
  if (options?.alias != null && options.alias)
83
- this.$state.set("RAW_ALIAS", null);
158
+ this.$state.set("ALIAS", null);
159
+ this.$state.set("RAW_ALIAS", null);
84
160
  return this;
85
161
  }
86
162
  /**
@@ -138,6 +214,27 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
138
214
  this.$state.set("SELECT", select);
139
215
  return this;
140
216
  }
217
+ /**
218
+ * The 'addSelect' method is used to add columns you want to retrieve from a database table.
219
+ *
220
+ * It allows you to choose the specific columns that should be included in the result set of a database query.
221
+ * @param {string[]} ...columns
222
+ * @returns {this} this
223
+ */
224
+ addSelect(...columns) {
225
+ let select = columns.map((c) => {
226
+ const column = String(c);
227
+ if (column.includes(this.$constants("RAW"))) {
228
+ return column?.replace(this.$constants("RAW"), "").replace(/'/g, "");
229
+ }
230
+ return this.bindColumn(column);
231
+ });
232
+ this.$state.set("ADD_SELECT", [
233
+ ...select,
234
+ ...this.$state.get("ADD_SELECT"),
235
+ ]);
236
+ return this;
237
+ }
141
238
  /**
142
239
  * The 'selectRaw' method is used to specify which columns you want to retrieve from a database table.
143
240
  *
@@ -155,8 +252,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
155
252
  return column;
156
253
  if (column.includes("`*`"))
157
254
  return column.replace("`*`", "*");
158
- if (column.includes(this.$constants("RAW")))
159
- return column?.replace(this.$constants("RAW"), "").replace(/'/g, "");
255
+ if (column.includes(this.$constants("RAW"))) {
256
+ return column?.replace(this.$constants("RAW"), "");
257
+ }
160
258
  return column;
161
259
  });
162
260
  select = [...this.$state.get("SELECT"), ...select];
@@ -200,7 +298,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
200
298
  return this;
201
299
  }
202
300
  /**
203
- * The 'selectObject' method is used to specify which columns you want to retrieve from a database table.
301
+ * The 'selectArray' method is used to specify which columns you want to retrieve from a database table.
204
302
  *
205
303
  * It allows you to choose the specific columns that should be included in the result set to 'Object' of a database query.
206
304
  * @param {string} object table name
@@ -223,15 +321,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
223
321
  `\`${this.getTableName()}\`.\`${value}\``,
224
322
  ];
225
323
  }
226
- const json = `
227
- ${this.$constants("CASE")}
228
- ${this.$constants("WHEN")} COUNT(${Object.values(maping)[1]}) = 0 ${this.$constants("THEN")} ${this.$constants("JSON_ARRAY")}()
229
- ${this.$constants("ELSE")} ${this.$constants("JSON_ARRAYAGG")}(
230
- ${this.$constants("JSON_OBJECT")}(${maping.join(" , ")})
231
- )
232
- ${this.$constants("END")}
233
- ${this.$constants("AS")} \`${alias}\`
234
- `;
324
+ const json = [
325
+ `${this.$constants("CASE")}`,
326
+ `${this.$constants("WHEN")}`,
327
+ `${this.$constants("COUNT")}(${Object.values(maping)[1]}) = 0`,
328
+ `${this.$constants("THEN")}`,
329
+ `${this.$constants("JSON_ARRAY")}()`,
330
+ `${this.$constants("ELSE")}`,
331
+ `${this.$constants("JSON_ARRAYAGG")}(${this.$constants("JSON_OBJECT")}(${maping.join(", ")}))`,
332
+ `${this.$constants("END")}`,
333
+ `${this.$constants("AS")} \`${alias}\``
334
+ ].join(" ");
235
335
  this.$state.set("SELECT", [...this.$state.get("SELECT"), json]);
236
336
  return this;
237
337
  }
@@ -321,7 +421,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
321
421
  */
322
422
  sleep(second) {
323
423
  this.CTEs('sleep', (query) => {
324
- return query.selectRaw(this._queryBuilder().sleep(second)).from('');
424
+ return query
425
+ .selectRaw(`${this._queryBuilder().sleep(second)} ${this.$constants("AS")} delay`)
426
+ .from(null);
325
427
  });
326
428
  this.from(this.$state.get("TABLE_NAME"), {
327
429
  push: [`${this.$constants("RAW")}sleep`]
@@ -389,8 +491,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
389
491
  return this.whereObject(column);
390
492
  }
391
493
  [value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
392
- value = this.$utils.escape(value);
393
- value = this.$utils.transfromBooleanToNumber(value);
394
494
  if (value === null) {
395
495
  return this.whereNull(column);
396
496
  }
@@ -399,12 +499,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
399
499
  }
400
500
  this.$state.set("WHERE", [
401
501
  ...this.$state.get("WHERE"),
402
- [
403
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
404
- `${this.bindColumn(String(column))}`,
405
- `${operator}`,
406
- `${this.$utils.transfromValueHasRaw(value)}`,
407
- ].join(" "),
502
+ {
503
+ column: this.bindColumn(String(column)),
504
+ operator: operator,
505
+ value: this.$utils.formatQueryValue(value),
506
+ }
408
507
  ]);
409
508
  return this;
410
509
  }
@@ -421,8 +520,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
421
520
  */
422
521
  orWhere(column, operator, value) {
423
522
  [value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
424
- value = this.$utils.escape(value);
425
- value = this.$utils.transfromBooleanToNumber(value);
426
523
  if (value === null) {
427
524
  return this.orWhereNull(column);
428
525
  }
@@ -431,12 +528,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
431
528
  }
432
529
  this.$state.set("WHERE", [
433
530
  ...this.$state.get("WHERE"),
434
- [
435
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
436
- `${this.bindColumn(String(column))}`,
437
- `${operator}`,
438
- `${this.$utils.transfromValueHasRaw(value)}`,
439
- ].join(" "),
531
+ {
532
+ column: this.bindColumn(String(column)),
533
+ operator: operator,
534
+ value: this.$utils.formatQueryValue(value),
535
+ condition: 'OR'
536
+ }
440
537
  ]);
441
538
  return this;
442
539
  }
@@ -451,12 +548,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
451
548
  whereDay(column, day) {
452
549
  this.$state.set("WHERE", [
453
550
  ...this.$state.get("WHERE"),
454
- [
455
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
456
- `DAY(${this.bindColumn(String(column))})`,
457
- `=`,
458
- `'${`00${this.$utils.escape(day)}`.slice(-2)}'`,
459
- ].join(" "),
551
+ {
552
+ column: `DAY(${this.bindColumn(String(column))})`,
553
+ operator: '=',
554
+ value: `'${`00${this.$utils.escape(day)}`.slice(-2)}'`,
555
+ }
460
556
  ]);
461
557
  return this;
462
558
  }
@@ -471,12 +567,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
471
567
  whereMonth(column, month) {
472
568
  this.$state.set("WHERE", [
473
569
  ...this.$state.get("WHERE"),
474
- [
475
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
476
- `MONTH(${this.bindColumn(String(column))})`,
477
- `=`,
478
- `'${`00${this.$utils.escape(month)}`.slice(-2)}'`,
479
- ].join(" "),
570
+ {
571
+ column: `MONTH(${this.bindColumn(String(column))})`,
572
+ operator: '=',
573
+ value: `'${`00${this.$utils.formatQueryValue(month)}`.slice(-2)}'`,
574
+ }
480
575
  ]);
481
576
  return this;
482
577
  }
@@ -491,12 +586,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
491
586
  whereYear(column, year) {
492
587
  this.$state.set("WHERE", [
493
588
  ...this.$state.get("WHERE"),
494
- [
495
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
496
- `YEAR(${this.bindColumn(String(column))})`,
497
- `=`,
498
- `'${`0000${this.$utils.escape(year)}`.slice(-4)}'`,
499
- ].join(" "),
589
+ {
590
+ column: `YEAR(${this.bindColumn(String(column))})`,
591
+ operator: '=',
592
+ value: `'${`0000${this.$utils.formatQueryValue(year)}`.slice(-2)}'`,
593
+ }
500
594
  ]);
501
595
  return this;
502
596
  }
@@ -508,13 +602,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
508
602
  * @param {string} sql where column with raw sql
509
603
  * @returns {this} this
510
604
  */
511
- whereRaw(sql) {
605
+ whereRaw(sql, parameters = []) {
606
+ if (parameters.length) {
607
+ sql = this.$utils.bindingParameters(sql, parameters);
608
+ }
609
+ ;
512
610
  this.$state.set("WHERE", [
513
611
  ...this.$state.get("WHERE"),
514
- [
515
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
516
- `${sql}`,
517
- ].join(" "),
612
+ {
613
+ value: sql,
614
+ }
518
615
  ]);
519
616
  return this;
520
617
  }
@@ -526,13 +623,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
526
623
  * @param {string} sql where column with raw sql
527
624
  * @returns {this} this
528
625
  */
529
- orWhereRaw(sql) {
626
+ orWhereRaw(sql, parameters = []) {
627
+ if (parameters.length) {
628
+ sql = this.$utils.bindingParameters(sql, parameters);
629
+ }
630
+ ;
530
631
  this.$state.set("WHERE", [
531
632
  ...this.$state.get("WHERE"),
532
- [
533
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
534
- `${sql}`,
535
- ].join(" "),
633
+ {
634
+ value: sql,
635
+ condition: 'OR'
636
+ }
536
637
  ]);
537
638
  return this;
538
639
  }
@@ -618,16 +719,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
618
719
  * @returns {this}
619
720
  */
620
721
  whereJSON(column, { key, value, operator }) {
621
- value = this.$utils.escape(value);
622
- value = this.$utils.transfromBooleanToNumber(value);
623
722
  this.$state.set("WHERE", [
624
723
  ...this.$state.get("WHERE"),
625
- [
626
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
627
- `${this.bindColumn(column)}->>'$.${key}'`,
628
- `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
629
- `${this.$utils.transfromValueHasRaw(value)}`,
630
- ].join(" "),
724
+ {
725
+ column: `${this.bindColumn(String(column))}->>'$.${key}'`,
726
+ operator: `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
727
+ value: `${this.$utils.formatQueryValue(value)}`
728
+ }
631
729
  ]);
632
730
  return this;
633
731
  }
@@ -654,13 +752,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
654
752
  * @returns {this}
655
753
  */
656
754
  whereExists(sql) {
755
+ if (sql instanceof Builder && !sql.$state.get("SELECT").length) {
756
+ sql.select1();
757
+ }
657
758
  this.$state.set("WHERE", [
658
759
  ...this.$state.get("WHERE"),
659
- [
660
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
661
- `${this.$constants("EXISTS")}`,
662
- `(${sql})`,
663
- ].join(" "),
760
+ {
761
+ value: `${this.$constants("EXISTS")} (${sql})`
762
+ }
664
763
  ]);
665
764
  return this;
666
765
  }
@@ -675,11 +774,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
675
774
  whereNotExists(sql) {
676
775
  this.$state.set("WHERE", [
677
776
  ...this.$state.get("WHERE"),
678
- [
679
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
680
- `${this.$constants("NOT")} ${this.$constants("EXISTS")}`,
681
- `(${sql})`,
682
- ].join(" "),
777
+ {
778
+ value: `${this.$constants("NOT")} ${this.$constants("EXISTS")} (${sql})`
779
+ }
683
780
  ]);
684
781
  return this;
685
782
  }
@@ -694,11 +791,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
694
791
  orWhereExists(sql) {
695
792
  this.$state.set("WHERE", [
696
793
  ...this.$state.get("WHERE"),
697
- [
698
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
699
- `${this.$constants("EXISTS")}`,
700
- `(${sql})`,
701
- ].join(" "),
794
+ {
795
+ value: `${this.$constants("EXISTS")} (${sql})`,
796
+ condition: 'OR'
797
+ }
702
798
  ]);
703
799
  return this;
704
800
  }
@@ -713,11 +809,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
713
809
  orWhereNotExists(sql) {
714
810
  this.$state.set("WHERE", [
715
811
  ...this.$state.get("WHERE"),
716
- [
717
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
718
- `${this.$constants("NOT")} ${this.$constants("EXISTS")}`,
719
- `(${sql})`,
720
- ].join(" "),
812
+ {
813
+ value: `${this.$constants("NOT")} ${this.$constants("EXISTS")} (${sql})`,
814
+ condition: 'OR'
815
+ }
721
816
  ]);
722
817
  return this;
723
818
  }
@@ -729,10 +824,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
729
824
  whereId(id, column = "id") {
730
825
  this.$state.set("WHERE", [
731
826
  ...this.$state.get("WHERE"),
732
- [
733
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
734
- `${this.bindColumn(column)} = ${this.$utils.escape(id)}`,
735
- ].join(" "),
827
+ {
828
+ column: this.bindColumn(String(column)),
829
+ operator: '=',
830
+ value: this.$utils.escape(id),
831
+ }
736
832
  ]);
737
833
  return this;
738
834
  }
@@ -745,10 +841,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
745
841
  const column = "email";
746
842
  this.$state.set("WHERE", [
747
843
  ...this.$state.get("WHERE"),
748
- [
749
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
750
- `${this.bindColumn(column)} = ${this.$utils.escape(email)}`,
751
- ].join(" "),
844
+ {
845
+ column: this.bindColumn(String(column)),
846
+ operator: '=',
847
+ value: this.$utils.escape(email),
848
+ }
752
849
  ]);
753
850
  return this;
754
851
  }
@@ -761,10 +858,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
761
858
  whereUser(userId, column = "user_id") {
762
859
  this.$state.set("WHERE", [
763
860
  ...this.$state.get("WHERE"),
764
- [
765
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
766
- `${this.bindColumn(column)} = ${this.$utils.escape(userId)}`,
767
- ].join(" "),
861
+ {
862
+ column: this.bindColumn(String(column)),
863
+ operator: '=',
864
+ value: this.$utils.escape(userId),
865
+ }
768
866
  ]);
769
867
  return this;
770
868
  }
@@ -779,19 +877,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
779
877
  whereIn(column, array) {
780
878
  if (!Array.isArray(array))
781
879
  array = [array];
782
- const values = array.length
783
- ? `${array
784
- .map((value) => this.$utils.transfromValueHasRaw(this.$utils.escape(value)))
785
- .join(",")}`
786
- : this.$constants(this.$constants("NULL"));
880
+ if (!array.length) {
881
+ array = [DB_1.DB.raw(this.$constants("NULL"))];
882
+ }
787
883
  this.$state.set("WHERE", [
788
884
  ...this.$state.get("WHERE"),
789
- [
790
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
791
- `${this.bindColumn(column)}`,
792
- `${this.$constants("IN")}`,
793
- `(${values})`,
794
- ].join(" "),
885
+ {
886
+ column: this.bindColumn(String(column)),
887
+ operator: `${this.$constants("IN")}`,
888
+ value: array
889
+ .map((value) => {
890
+ return this.$utils.formatQueryValue(value);
891
+ })
892
+ }
795
893
  ]);
796
894
  return this;
797
895
  }
@@ -806,19 +904,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
806
904
  orWhereIn(column, array) {
807
905
  if (!Array.isArray(array))
808
906
  array = [array];
809
- const values = array.length
810
- ? `${array
811
- .map((value) => this.$utils.transfromValueHasRaw(this.$utils.escape(value)))
812
- .join(",")}`
813
- : this.$constants(this.$constants("NULL"));
907
+ if (!array.length) {
908
+ array = [DB_1.DB.raw(this.$constants("NULL"))];
909
+ }
814
910
  this.$state.set("WHERE", [
815
911
  ...this.$state.get("WHERE"),
816
- [
817
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
818
- `${this.bindColumn(column)}`,
819
- `${this.$constants("IN")}`,
820
- `(${values})`,
821
- ].join(" "),
912
+ {
913
+ column: this.bindColumn(String(column)),
914
+ operator: `${this.$constants("IN")}`,
915
+ condition: 'OR',
916
+ value: array
917
+ .map((value) => {
918
+ return this.$utils.formatQueryValue(value);
919
+ })
920
+ }
822
921
  ]);
823
922
  return this;
824
923
  }
@@ -833,19 +932,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
833
932
  whereNotIn(column, array) {
834
933
  if (!Array.isArray(array))
835
934
  array = [array];
836
- const values = array.length
837
- ? `${array
838
- .map((value) => this.$utils.transfromValueHasRaw(this.$utils.escape(value)))
839
- .join(",")}`
840
- : this.$constants(this.$constants("NULL"));
841
935
  this.$state.set("WHERE", [
842
936
  ...this.$state.get("WHERE"),
843
- [
844
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
845
- `${this.bindColumn(column)}`,
846
- `${this.$constants("NOT_IN")}`,
847
- `(${values})`,
848
- ].join(" "),
937
+ {
938
+ column: this.bindColumn(String(column)),
939
+ operator: `${this.$constants("NOT_IN")}`,
940
+ value: array
941
+ .map((value) => {
942
+ return this.$utils.formatQueryValue(value);
943
+ })
944
+ }
849
945
  ]);
850
946
  return this;
851
947
  }
@@ -860,19 +956,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
860
956
  orWhereNotIn(column, array) {
861
957
  if (!Array.isArray(array))
862
958
  array = [array];
863
- const values = array.length
864
- ? `${array
865
- .map((value) => this.$utils.transfromValueHasRaw(this.$utils.escape(value)))
866
- .join(",")}`
867
- : this.$constants(this.$constants("NULL"));
868
959
  this.$state.set("WHERE", [
869
960
  ...this.$state.get("WHERE"),
870
- [
871
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
872
- `${this.bindColumn(column)}`,
873
- `${this.$constants("NOT_IN")}`,
874
- `(${values})`,
875
- ].join(" "),
961
+ {
962
+ column: this.bindColumn(String(column)),
963
+ operator: `${this.$constants("IN")}`,
964
+ condition: 'OR',
965
+ value: array
966
+ .map((value) => {
967
+ return this.$utils.formatQueryValue(value);
968
+ })
969
+ }
876
970
  ]);
877
971
  return this;
878
972
  }
@@ -892,12 +986,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
892
986
  }
893
987
  this.$state.set("WHERE", [
894
988
  ...this.$state.get("WHERE"),
895
- [
896
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
897
- `${this.bindColumn(column)}`,
898
- options.operator,
899
- `(${subQuery})`,
900
- ].join(" "),
989
+ {
990
+ column: this.bindColumn(String(column)),
991
+ operator: options.operator,
992
+ value: `(${subQuery})`
993
+ }
901
994
  ]);
902
995
  return this;
903
996
  }
@@ -917,13 +1010,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
917
1010
  }
918
1011
  this.$state.set("WHERE", [
919
1012
  ...this.$state.get("WHERE"),
920
- [
921
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
922
- `${this.bindColumn(column)}`,
923
- options.operator,
924
- ,
925
- `(${subQuery})`,
926
- ].join(" "),
1013
+ {
1014
+ column: this.bindColumn(String(column)),
1015
+ operator: options.operator,
1016
+ value: `(${subQuery})`
1017
+ }
927
1018
  ]);
928
1019
  return this;
929
1020
  }
@@ -943,12 +1034,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
943
1034
  }
944
1035
  this.$state.set("WHERE", [
945
1036
  ...this.$state.get("WHERE"),
946
- [
947
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
948
- `${this.bindColumn(column)}`,
949
- options.operator,
950
- `(${subQuery})`,
951
- ].join(" "),
1037
+ {
1038
+ column: this.bindColumn(String(column)),
1039
+ operator: options.operator,
1040
+ condition: 'OR',
1041
+ value: `(${subQuery})`
1042
+ }
952
1043
  ]);
953
1044
  return this;
954
1045
  }
@@ -968,12 +1059,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
968
1059
  }
969
1060
  this.$state.set("WHERE", [
970
1061
  ...this.$state.get("WHERE"),
971
- [
972
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
973
- `${this.bindColumn(column)}`,
974
- options.operator,
975
- `(${subQuery})`,
976
- ].join(" "),
1062
+ {
1063
+ column: this.bindColumn(String(column)),
1064
+ operator: options.operator,
1065
+ condition: 'OR',
1066
+ value: `(${subQuery})`
1067
+ }
977
1068
  ]);
978
1069
  return this;
979
1070
  }
@@ -987,30 +1078,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
987
1078
  */
988
1079
  whereBetween(column, array) {
989
1080
  if (!array.length) {
990
- this.$state.set("WHERE", [
991
- ...this.$state.get("WHERE"),
992
- [
993
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
994
- `${this.bindColumn(column)}`,
995
- `${this.$constants("BETWEEN")}`,
996
- `${this.$constants(this.$constants("NULL"))}`,
997
- `${this.$constants("AND")}`,
998
- `${this.$constants(this.$constants("NULL"))}`,
999
- ].join(" "),
1000
- ]);
1001
- return this;
1081
+ return this.whereBetween(column, [DB_1.DB.raw('NULL'), DB_1.DB.raw('NULL')]);
1002
1082
  }
1003
1083
  const [value1, value2] = array;
1004
1084
  this.$state.set("WHERE", [
1005
1085
  ...this.$state.get("WHERE"),
1006
- [
1007
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1008
- `${this.bindColumn(column)}`,
1009
- `${this.$constants("BETWEEN")}`,
1010
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value1))}`,
1011
- `${this.$constants("AND")}`,
1012
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value2))}`,
1013
- ].join(" "),
1086
+ {
1087
+ column: this.bindColumn(String(column)),
1088
+ operator: `${this.$constants("BETWEEN")}`,
1089
+ value: [
1090
+ `${this.$utils.formatQueryValue(value1)}`,
1091
+ `${this.$constants("AND")}`,
1092
+ `${this.$utils.formatQueryValue(value2)}`,
1093
+ ].join(" ")
1094
+ }
1014
1095
  ]);
1015
1096
  return this;
1016
1097
  }
@@ -1024,30 +1105,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1024
1105
  */
1025
1106
  orWhereBetween(column, array) {
1026
1107
  if (!array.length) {
1027
- this.$state.set("WHERE", [
1028
- ...this.$state.get("WHERE"),
1029
- [
1030
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1031
- `${this.bindColumn(column)}`,
1032
- `${this.$constants("BETWEEN")}`,
1033
- `${this.$constants(this.$constants("NULL"))}`,
1034
- `${this.$constants("AND")}`,
1035
- `${this.$constants(this.$constants("NULL"))}`,
1036
- ].join(" "),
1037
- ]);
1038
- return this;
1108
+ return this.orWhereBetween(column, ['NULL', 'NULL']);
1039
1109
  }
1040
1110
  const [value1, value2] = array;
1041
1111
  this.$state.set("WHERE", [
1042
1112
  ...this.$state.get("WHERE"),
1043
- [
1044
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1045
- `${this.bindColumn(column)}`,
1046
- `${this.$constants("BETWEEN")}`,
1047
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value1))}`,
1048
- `${this.$constants("AND")}`,
1049
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value2))}`,
1050
- ].join(" "),
1113
+ {
1114
+ column: this.bindColumn(String(column)),
1115
+ operator: `${this.$constants("BETWEEN")}`,
1116
+ condition: 'OR',
1117
+ value: [
1118
+ `${this.$utils.formatQueryValue(value1)}`,
1119
+ `${this.$constants("AND")}`,
1120
+ `${this.$utils.formatQueryValue(value2)}`,
1121
+ ].join(" ")
1122
+ }
1051
1123
  ]);
1052
1124
  return this;
1053
1125
  }
@@ -1063,30 +1135,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1063
1135
  if (!Array.isArray(array))
1064
1136
  throw new Error("Value is't array");
1065
1137
  if (!array.length) {
1066
- this.$state.set("WHERE", [
1067
- ...this.$state.get("WHERE"),
1068
- [
1069
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1070
- `${this.bindColumn(column)}`,
1071
- `${this.$constants("NOT_BETWEEN")}`,
1072
- `${this.$constants(this.$constants("NULL"))}`,
1073
- `${this.$constants("AND")}`,
1074
- `${this.$constants(this.$constants("NULL"))}`,
1075
- ].join(" "),
1076
- ]);
1077
- return this;
1138
+ return this.whereNotBetween(column, ['NULL', 'NULL']);
1078
1139
  }
1079
1140
  const [value1, value2] = array;
1080
1141
  this.$state.set("WHERE", [
1081
1142
  ...this.$state.get("WHERE"),
1082
- [
1083
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1084
- `${this.bindColumn(column)}`,
1085
- `${this.$constants("NOT_BETWEEN")}`,
1086
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value1))}`,
1087
- `${this.$constants("AND")}`,
1088
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value2))}`,
1089
- ].join(" "),
1143
+ {
1144
+ column: this.bindColumn(String(column)),
1145
+ operator: `${this.$constants("NOT_BETWEEN")}`,
1146
+ value: [
1147
+ `${this.$utils.formatQueryValue(value1)}`,
1148
+ `${this.$constants("AND")}`,
1149
+ `${this.$utils.formatQueryValue(value2)}`,
1150
+ ].join(" ")
1151
+ }
1090
1152
  ]);
1091
1153
  return this;
1092
1154
  }
@@ -1102,30 +1164,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1102
1164
  if (!Array.isArray(array))
1103
1165
  throw new Error("Value is't array");
1104
1166
  if (!array.length) {
1105
- this.$state.set("WHERE", [
1106
- ...this.$state.get("WHERE"),
1107
- [
1108
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1109
- `${this.bindColumn(column)}`,
1110
- `${this.$constants("NOT_BETWEEN")}`,
1111
- `${this.$constants(this.$constants("NULL"))}`,
1112
- `${this.$constants("AND")}`,
1113
- `${this.$constants(this.$constants("NULL"))}`,
1114
- ].join(" "),
1115
- ]);
1116
- return this;
1167
+ return this.orWhereNotBetween(column, ['NULL', 'NULL']);
1117
1168
  }
1118
1169
  const [value1, value2] = array;
1119
1170
  this.$state.set("WHERE", [
1120
1171
  ...this.$state.get("WHERE"),
1121
- [
1122
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1123
- `${this.bindColumn(column)}`,
1124
- `${this.$constants("NOT_BETWEEN")}`,
1125
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value1))}`,
1126
- `${this.$constants("AND")}`,
1127
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value2))}`,
1128
- ].join(" "),
1172
+ {
1173
+ column: this.bindColumn(String(column)),
1174
+ operator: `${this.$constants("NOT_BETWEEN")}`,
1175
+ condition: 'OR',
1176
+ value: [
1177
+ `${this.$utils.formatQueryValue(value1)}`,
1178
+ `${this.$constants("AND")}`,
1179
+ `${this.$utils.formatQueryValue(value2)}`,
1180
+ ].join(" ")
1181
+ }
1129
1182
  ]);
1130
1183
  return this;
1131
1184
  }
@@ -1139,11 +1192,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1139
1192
  whereNull(column) {
1140
1193
  this.$state.set("WHERE", [
1141
1194
  ...this.$state.get("WHERE"),
1142
- [
1143
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1144
- `${this.bindColumn(column)}`,
1145
- `${this.$constants("IS_NULL")}`,
1146
- ].join(" "),
1195
+ {
1196
+ column: this.bindColumn(String(column)),
1197
+ operator: `${this.$constants("IS_NULL")}`,
1198
+ }
1147
1199
  ]);
1148
1200
  return this;
1149
1201
  }
@@ -1157,11 +1209,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1157
1209
  orWhereNull(column) {
1158
1210
  this.$state.set("WHERE", [
1159
1211
  ...this.$state.get("WHERE"),
1160
- [
1161
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1162
- `${this.bindColumn(column)}`,
1163
- `${this.$constants("IS_NULL")}`,
1164
- ].join(" "),
1212
+ {
1213
+ column: this.bindColumn(String(column)),
1214
+ operator: `${this.$constants("IS_NULL")}`,
1215
+ condition: 'OR'
1216
+ }
1165
1217
  ]);
1166
1218
  return this;
1167
1219
  }
@@ -1175,11 +1227,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1175
1227
  whereNotNull(column) {
1176
1228
  this.$state.set("WHERE", [
1177
1229
  ...this.$state.get("WHERE"),
1178
- [
1179
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1180
- `${this.bindColumn(column)}`,
1181
- `${this.$constants("IS_NOT_NULL")}`,
1182
- ].join(" "),
1230
+ {
1231
+ column: this.bindColumn(String(column)),
1232
+ operator: `${this.$constants("IS_NOT_NULL")}`,
1233
+ }
1183
1234
  ]);
1184
1235
  return this;
1185
1236
  }
@@ -1193,11 +1244,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1193
1244
  orWhereNotNull(column) {
1194
1245
  this.$state.set("WHERE", [
1195
1246
  ...this.$state.get("WHERE"),
1196
- [
1197
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1198
- `${this.bindColumn(column)}`,
1199
- `${this.$constants("IS_NOT_NULL")}`,
1200
- ].join(" "),
1247
+ {
1248
+ column: this.bindColumn(String(column)),
1249
+ operator: `${this.$constants("IS_NOT_NULL")}`,
1250
+ }
1201
1251
  ]);
1202
1252
  return this;
1203
1253
  }
@@ -1214,17 +1264,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1214
1264
  */
1215
1265
  whereSensitive(column, operator, value) {
1216
1266
  [value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
1217
- value = this.$utils.escape(value);
1218
- value = this.$utils.transfromBooleanToNumber(value);
1219
1267
  this.$state.set("WHERE", [
1220
1268
  ...this.$state.get("WHERE"),
1221
- [
1222
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1223
- `${this.$constants("BINARY")}`,
1224
- `${this.bindColumn(column)}`,
1225
- `${operator}`,
1226
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value))}`,
1227
- ].join(" "),
1269
+ {
1270
+ column: `${this.$constants("BINARY")} ${this.bindColumn(String(column))}`,
1271
+ operator: `${this.$constants("IS_NULL")}`,
1272
+ value: `${this.$utils.formatQueryValue(value)}`,
1273
+ }
1228
1274
  ]);
1229
1275
  return this;
1230
1276
  }
@@ -1255,17 +1301,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1255
1301
  */
1256
1302
  orWhereSensitive(column, operator, value) {
1257
1303
  [value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
1258
- value = this.$utils.escape(value);
1259
- value = this.$utils.transfromBooleanToNumber(value);
1260
1304
  this.$state.set("WHERE", [
1261
1305
  ...this.$state.get("WHERE"),
1262
- [
1263
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1264
- `${this.$constants("BINARY")}`,
1265
- `${this.bindColumn(column)}`,
1266
- `${operator}`,
1267
- `${this.$utils.transfromValueHasRaw(this.$utils.escape(value))}`,
1268
- ].join(" "),
1306
+ {
1307
+ column: `${this.$constants("BINARY")} ${this.bindColumn(String(column))}`,
1308
+ operator,
1309
+ condition: 'OR',
1310
+ value: `${this.$utils.formatQueryValue(value)}`,
1311
+ }
1269
1312
  ]);
1270
1313
  return this;
1271
1314
  }
@@ -1289,10 +1332,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1289
1332
  const query = where.join(" ");
1290
1333
  this.$state.set("WHERE", [
1291
1334
  ...this.$state.get("WHERE"),
1292
- [
1293
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1294
- `(${query})`,
1295
- ].join(" "),
1335
+ ...where
1296
1336
  ]);
1297
1337
  return this;
1298
1338
  }
@@ -1323,13 +1363,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1323
1363
  const where = repository?.$state.get("WHERE") || [];
1324
1364
  if (!where.length)
1325
1365
  return this;
1326
- const query = where.join(" ");
1327
1366
  this.$state.set("WHERE", [
1328
1367
  ...this.$state.get("WHERE"),
1329
- [
1330
- this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
1331
- `(${query})`,
1332
- ].join(" "),
1368
+ ...where
1333
1369
  ]);
1334
1370
  return this;
1335
1371
  }
@@ -1355,8 +1391,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1355
1391
  */
1356
1392
  whereAny(columns, operator, value) {
1357
1393
  [value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
1358
- value = this.$utils.escape(value);
1359
- value = this.$utils.transfromBooleanToNumber(value);
1360
1394
  this.whereQuery((query) => {
1361
1395
  for (const index in columns) {
1362
1396
  const column = columns[index];
@@ -1383,8 +1417,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1383
1417
  */
1384
1418
  whereAll(columns, operator, value) {
1385
1419
  [value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
1386
- value = this.$utils.escape(value);
1387
- value = this.$utils.transfromBooleanToNumber(value);
1388
1420
  this.whereQuery((query) => {
1389
1421
  for (const column of columns)
1390
1422
  query.where(column, operator, value);
@@ -1412,7 +1444,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1412
1444
  return `${this.$constants("WHEN")} ${when} ${this.$constants("THEN")} ${then}`;
1413
1445
  });
1414
1446
  const whereClause = [
1415
- this.$state.get("WHERE").length ? this.$constants("AND") : "",
1416
1447
  "(",
1417
1448
  this.$constants("CASE"),
1418
1449
  ...query,
@@ -1422,7 +1453,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1422
1453
  ]
1423
1454
  .filter(Boolean)
1424
1455
  .join(" ");
1425
- this.$state.set("WHERE", [...this.$state.get("WHERE"), whereClause]);
1456
+ this.whereRaw(whereClause);
1426
1457
  return this;
1427
1458
  }
1428
1459
  /**
@@ -1445,7 +1476,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1445
1476
  return `${this.$constants("WHEN")} ${when} ${this.$constants("THEN")} ${then}`;
1446
1477
  });
1447
1478
  const whereClause = [
1448
- this.$state.get("WHERE").length ? this.$constants("OR") : "",
1449
1479
  "(",
1450
1480
  this.$constants("CASE"),
1451
1481
  ...query,
@@ -1455,17 +1485,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1455
1485
  ]
1456
1486
  .filter(Boolean)
1457
1487
  .join(" ");
1458
- this.$state.set("WHERE", [...this.$state.get("WHERE"), whereClause]);
1488
+ this.orWhereRaw(whereClause);
1459
1489
  return this;
1460
1490
  }
1461
1491
  whereReference(tableAndLocalKey, tableAndForeignKey) {
1462
- this.$state.set("WHERE", [
1463
- ...this.$state.get("WHERE"),
1464
- [
1465
- this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
1466
- `${tableAndLocalKey} = ${tableAndForeignKey}`,
1467
- ].join(" "),
1468
- ]);
1492
+ this.whereRaw(`${tableAndLocalKey} = ${tableAndForeignKey}`);
1469
1493
  return this;
1470
1494
  }
1471
1495
  /**
@@ -1613,10 +1637,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1613
1637
  ...this.$state.get("ORDER_BY"),
1614
1638
  `${orderBy} ${order.toUpperCase()}`,
1615
1639
  ]);
1616
- this.$state.set("ORDER_BY", [
1617
- ...this.$state.get("ORDER_BY"),
1618
- `\`${column}\` ${order.toUpperCase()}`,
1619
- ]);
1620
1640
  return this;
1621
1641
  }
1622
1642
  /**
@@ -2143,8 +2163,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2143
2163
  * @returns {this} this this
2144
2164
  */
2145
2165
  insertNotExists(data) {
2146
- this.createNotExists(data);
2147
- return this;
2166
+ return this.createNotExists(data);
2148
2167
  }
2149
2168
  /**
2150
2169
  * The 'createOrSelect' method to insert data into a database table while select any duplicate key constraint violations.
@@ -2168,8 +2187,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2168
2187
  * @returns {this} this this
2169
2188
  */
2170
2189
  insertOrSelect(data) {
2171
- this.createOrSelect(data);
2172
- return this;
2190
+ return this.createOrSelect(data);
2173
2191
  }
2174
2192
  /**
2175
2193
  * The 'updateOrCreate' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
@@ -2195,8 +2213,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2195
2213
  * @returns {this} this this
2196
2214
  */
2197
2215
  updateOrInsert(data) {
2198
- this.updateOrCreate(data);
2199
- return this;
2216
+ return this.updateOrCreate(data);
2200
2217
  }
2201
2218
  /**
2202
2219
  * The 'insertOrUpdate' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
@@ -2207,8 +2224,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2207
2224
  * @returns {this} this this
2208
2225
  */
2209
2226
  insertOrUpdate(data) {
2210
- this.updateOrCreate(data);
2211
- return this;
2227
+ return this.updateOrCreate(data);
2212
2228
  }
2213
2229
  /**
2214
2230
  *
@@ -2220,8 +2236,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2220
2236
  * @returns {this} this this
2221
2237
  */
2222
2238
  createOrUpdate(data) {
2223
- this.updateOrCreate(data);
2224
- return this;
2239
+ return this.updateOrCreate(data);
2225
2240
  }
2226
2241
  /**
2227
2242
  * The 'toString' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
@@ -2364,11 +2379,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2364
2379
  *
2365
2380
  */
2366
2381
  async getFKs(table) {
2367
- const sql = this._queryBuilder().getFKs({
2382
+ const fks = await this.rawQuery(this._queryBuilder().getFKs({
2368
2383
  database: this.$database,
2369
2384
  table: table ?? String(this.$state.get("TABLE_NAME")),
2370
- });
2371
- return await this.rawQuery(sql);
2385
+ }));
2386
+ return fks;
2372
2387
  }
2373
2388
  /**
2374
2389
  * The `hasFK` method is used to determine whether a specific foreign key
@@ -2381,12 +2396,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2381
2396
  *
2382
2397
  */
2383
2398
  async hasFK({ constraint, table }) {
2384
- const sql = this._queryBuilder().hasFK({
2399
+ const result = await this.rawQuery(this._queryBuilder().hasFK({
2385
2400
  database: this.$database,
2386
2401
  table: table ?? String(this.$state.get("TABLE_NAME")),
2387
2402
  constraint
2388
- });
2389
- const result = await this.rawQuery(sql);
2403
+ }));
2390
2404
  return Boolean(result[0]?.IS_EXISTS ?? false);
2391
2405
  }
2392
2406
  /**
@@ -2405,11 +2419,49 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2405
2419
  *
2406
2420
  */
2407
2421
  async getChildFKs(table) {
2408
- const sql = this._queryBuilder().getChildFKs({
2422
+ const childFks = await this.rawQuery(this._queryBuilder().getChildFKs({
2409
2423
  database: this.$database,
2410
2424
  table: table ?? String(this.$state.get("TABLE_NAME")),
2411
- });
2412
- return await this.rawQuery(sql);
2425
+ }));
2426
+ return childFks;
2427
+ }
2428
+ /**
2429
+ * Adds a foreign key constraint to a table.
2430
+ *
2431
+ * This method checks whether the specified foreign key constraint already exists
2432
+ * on the table before attempting to create it. If the constraint exists, an error
2433
+ * will be thrown to prevent duplication.
2434
+ *
2435
+ * @async
2436
+ * @method addFK
2437
+ * @param {Object} params - Parameters for creating the foreign key.
2438
+ * @param {string} params.table - The name of the table where the foreign key will be added.
2439
+ * @param {string} params.tableRef - The referenced table name.
2440
+ * @param {string} params.key - The column in the current table that will act as the foreign key.
2441
+ * @param {string} params.constraint - The name of the foreign key constraint.
2442
+ * @param {Object} params.foreign - Foreign key configuration.
2443
+ * @param {string} params.foreign.references - The referenced column in the referenced table.
2444
+ * @param {string} params.foreign.onDelete - Action when a referenced row is deleted (e.g. CASCADE, SET NULL).
2445
+ * @param {string} params.foreign.onUpdate - Action when a referenced row is updated (e.g. CASCADE, RESTRICT).
2446
+ *
2447
+ * @throws {Error} If the specified foreign key constraint already exists.
2448
+ *
2449
+ * @returns {Promise<void>} Resolves when the foreign key constraint is successfully created.
2450
+ */
2451
+ async addFK({ table, tableRef, key, constraint, foreign }) {
2452
+ const exists = await this.hasFK({ constraint, table });
2453
+ if (exists) {
2454
+ throw new Error('The specified foreign key constraint already exists.');
2455
+ }
2456
+ await this.rawQuery(this._queryBuilder()
2457
+ .addFK({
2458
+ table: table ?? this.getTableName(),
2459
+ tableRef,
2460
+ key,
2461
+ constraint,
2462
+ foreign
2463
+ }));
2464
+ return;
2413
2465
  }
2414
2466
  /**
2415
2467
  * The `dropFK` method is used to remove a foreign key constraint from the database.
@@ -2422,11 +2474,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2422
2474
  * @returns {Promise<void>}
2423
2475
  */
2424
2476
  async dropFK({ constraint, table }) {
2425
- const sql = this._queryBuilder().dropFK({
2426
- table: table ?? String(this.$state.get("TABLE_NAME")),
2477
+ const exists = await this.hasFK({ constraint, table });
2478
+ if (!exists) {
2479
+ throw new Error('The specified foreign key constraint was not found.');
2480
+ }
2481
+ await this.rawQuery(this._queryBuilder()
2482
+ .dropFK({
2483
+ table: table ?? this.getTableName(),
2427
2484
  constraint
2428
- });
2429
- await this.rawQuery(sql);
2485
+ }));
2430
2486
  return;
2431
2487
  }
2432
2488
  /**
@@ -2445,11 +2501,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2445
2501
  *
2446
2502
  */
2447
2503
  async getIndexes(table) {
2448
- const sql = this._queryBuilder().getIndexes({
2504
+ const indexes = await this.rawQuery(this._queryBuilder().getIndexes({
2449
2505
  database: this.$database,
2450
- table: table ?? String(this.$state.get("TABLE_NAME")),
2451
- });
2452
- return await this.rawQuery(sql);
2506
+ table: table ?? this.getTableName()
2507
+ }));
2508
+ return indexes;
2453
2509
  }
2454
2510
  /**
2455
2511
  * The `hasIndex` method is used to determine whether a specific index exists
@@ -2461,15 +2517,226 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2461
2517
  * @returns {Promise<boolean>}
2462
2518
  *
2463
2519
  */
2464
- async hasIndex({ index, table }) {
2465
- const sql = this._queryBuilder().hasIndex({
2520
+ async hasIndex({ name, table }) {
2521
+ const result = await this.rawQuery(this._queryBuilder().hasIndex({
2466
2522
  database: this.$database,
2467
- table: table ?? String(this.$state.get("TABLE_NAME")),
2468
- index
2469
- });
2470
- const result = await this.rawQuery(sql);
2523
+ table: table ?? this.getTableName(),
2524
+ name
2525
+ }));
2471
2526
  return Boolean(result[0]?.IS_EXISTS ?? false);
2472
2527
  }
2528
+ /**
2529
+ * Adds an index to a table.
2530
+ *
2531
+ * This method checks whether the specified index already exists on the table
2532
+ * before attempting to create it. If the index exists, an error will be thrown
2533
+ * to prevent duplication.
2534
+ *
2535
+ * @async
2536
+ * @method addIndex
2537
+ * @param {Object} params - Parameters for creating the index.
2538
+ * @param {string} params.name - The name of the index.
2539
+ * @param {string} [params.table] - The table where the index will be created. Defaults to the current model table.
2540
+ * @param {string[]} params.columns - The columns included in the index.
2541
+ *
2542
+ * @throws {Error} If the specified index already exists.
2543
+ *
2544
+ * @returns {Promise<void>} Resolves when the index has been successfully created.
2545
+ */
2546
+ async addIndex({ name, table, columns }) {
2547
+ const exists = await this.hasIndex({ name, table });
2548
+ if (exists) {
2549
+ throw new Error('The specified index already exists.');
2550
+ }
2551
+ await this.rawQuery(this._queryBuilder().addIndex({
2552
+ table: table ?? this.getTableName(),
2553
+ name,
2554
+ columns
2555
+ }));
2556
+ return;
2557
+ }
2558
+ /**
2559
+ * Drops an index from a table.
2560
+ *
2561
+ * This method checks whether the specified index exists on the table
2562
+ * before attempting to drop it. If the index does not exist, an error
2563
+ * will be thrown.
2564
+ *
2565
+ * @async
2566
+ * @method dropIndex
2567
+ * @param {Object} params - Parameters for dropping the index.
2568
+ * @param {string} params.name - The name of the index to drop.
2569
+ * @param {string} [params.table] - The table from which the index will be removed. Defaults to the current model table.
2570
+ *
2571
+ * @throws {Error} If the specified index was not found.
2572
+ *
2573
+ * @returns {Promise<void>} Resolves when the index has been successfully dropped.
2574
+ */
2575
+ async dropIndex({ name, table }) {
2576
+ const exists = await this.hasIndex({ name, table });
2577
+ if (!exists) {
2578
+ throw new Error('The specified index was not found');
2579
+ }
2580
+ await this.rawQuery(this._queryBuilder().dropIndex({
2581
+ table: table ?? this.getTableName(),
2582
+ name,
2583
+ }));
2584
+ return;
2585
+ }
2586
+ /**
2587
+ * Checks whether a unique constraint exists on a table.
2588
+ *
2589
+ * This method queries the database metadata to determine if the specified
2590
+ * unique constraint exists on the given table.
2591
+ *
2592
+ * @async
2593
+ * @method hasUnique
2594
+ * @param {Object} params - Parameters for checking the unique constraint.
2595
+ * @param {string} params.name - The name of the unique constraint.
2596
+ * @param {string} [params.table] - The table to check. Defaults to the current model table.
2597
+ *
2598
+ * @returns {Promise<boolean>} Returns `true` if the unique constraint exists, otherwise `false`.
2599
+ */
2600
+ async hasUnique({ name, table }) {
2601
+ const result = await this.rawQuery(this._queryBuilder().hasUnique({
2602
+ database: this.$database,
2603
+ table: table ?? this.getTableName(),
2604
+ name
2605
+ }));
2606
+ return Boolean(result[0]?.IS_EXISTS ?? false);
2607
+ }
2608
+ /**
2609
+ * Adds a unique constraint to a table.
2610
+ *
2611
+ * This method checks whether the specified unique constraint already exists
2612
+ * on the table before attempting to create it. If the constraint already exists,
2613
+ * an error will be thrown to prevent duplication.
2614
+ *
2615
+ * @async
2616
+ * @method addUnique
2617
+ * @param {Object} params - Parameters for creating the unique constraint.
2618
+ * @param {string} params.name - The name of the unique constraint.
2619
+ * @param {string} [params.table] - The table where the unique constraint will be added. Defaults to the current model table.
2620
+ * @param {string[]} params.columns - The columns that will be included in the unique constraint.
2621
+ *
2622
+ * @throws {Error} If the specified unique constraint already exists.
2623
+ *
2624
+ * @returns {Promise<void>} Resolves when the unique constraint has been successfully created.
2625
+ */
2626
+ async addUnique({ name, table, columns }) {
2627
+ const exists = await this.hasUnique({ name, table });
2628
+ if (exists) {
2629
+ throw new Error('The specified unique constraint already exists.');
2630
+ }
2631
+ await this.rawQuery(this._queryBuilder().addUnique({
2632
+ table: table ?? this.getTableName(),
2633
+ name,
2634
+ columns
2635
+ }));
2636
+ return;
2637
+ }
2638
+ /**
2639
+ * Drops a unique constraint from a table.
2640
+ *
2641
+ * This method checks whether the specified unique constraint exists
2642
+ * on the table before attempting to remove it. If the constraint does
2643
+ * not exist, an error will be thrown.
2644
+ *
2645
+ * @async
2646
+ * @method dropUnique
2647
+ * @param {Object} params - Parameters for dropping the unique constraint.
2648
+ * @param {string} params.name - The name of the unique constraint to drop.
2649
+ * @param {string} [params.table] - The table from which the unique constraint will be removed. Defaults to the current model table.
2650
+ *
2651
+ * @throws {Error} If the specified unique constraint was not found.
2652
+ *
2653
+ * @returns {Promise<void>} Resolves when the unique constraint has been successfully dropped.
2654
+ */
2655
+ async dropUnique({ name, table }) {
2656
+ const exists = await this.hasUnique({ name, table });
2657
+ if (!exists) {
2658
+ throw new Error('The specified unique constraint was not found.');
2659
+ }
2660
+ await this.rawQuery(this._queryBuilder().dropUnique({
2661
+ table: table ?? this.getTableName(),
2662
+ name,
2663
+ }));
2664
+ return;
2665
+ }
2666
+ /**
2667
+ * Checks whether a primary key constraint exists on a table.
2668
+ *
2669
+ * This method queries the database metadata to determine if the specified
2670
+ * table contains a primary key constraint.
2671
+ *
2672
+ * @async
2673
+ * @method hasPrimaryKey
2674
+ * @param {Object} [params={}] - Parameters for checking the primary key.
2675
+ * @param {string} [params.table] - The table to check. Defaults to the current model table.
2676
+ *
2677
+ * @returns {Promise<boolean>} Returns `true` if the table has a primary key constraint,
2678
+ * otherwise `false`.
2679
+ */
2680
+ async hasPrimaryKey({ table } = {}) {
2681
+ const result = await this.rawQuery(this._queryBuilder().hasPrimaryKey({
2682
+ database: this.$database,
2683
+ table: table ?? this.getTableName()
2684
+ }));
2685
+ return Boolean(result[0]?.IS_EXISTS ?? false);
2686
+ }
2687
+ /**
2688
+ * Adds a primary key constraint to a table.
2689
+ *
2690
+ * This method checks whether the table already has a primary key
2691
+ * before attempting to create one. If a primary key already exists,
2692
+ * an error will be thrown.
2693
+ *
2694
+ * @async
2695
+ * @method addPrimaryKey
2696
+ * @param {Object} params - Parameters for creating the primary key.
2697
+ * @param {string} [params.table] - The table where the primary key will be added. Defaults to the current model table.
2698
+ * @param {string[]} params.columns - The column(s) that will form the primary key.
2699
+ *
2700
+ * @throws {Error} If the table already has a primary key constraint.
2701
+ *
2702
+ * @returns {Promise<void>} Resolves when the primary key has been successfully created.
2703
+ */
2704
+ async addPrimaryKey({ table, columns }) {
2705
+ const exists = await this.hasPrimaryKey({ table });
2706
+ if (exists) {
2707
+ throw new Error('The specified primary constraint already exists.');
2708
+ }
2709
+ await this.rawQuery(this._queryBuilder().addPrimaryKey({
2710
+ table: table ?? String(this.getTableName()),
2711
+ columns
2712
+ }));
2713
+ return;
2714
+ }
2715
+ /**
2716
+ * Drops the primary key constraint from a table.
2717
+ *
2718
+ * This method checks whether the table has a primary key before
2719
+ * attempting to remove it. If no primary key exists, an error will be thrown.
2720
+ *
2721
+ * @async
2722
+ * @method dropPrimaryKey
2723
+ * @param {Object} [params={}] - Parameters for dropping the primary key.
2724
+ * @param {string} [params.table] - The table from which the primary key will be removed. Defaults to the current model table.
2725
+ *
2726
+ * @throws {Error} If the specified primary key constraint was not found.
2727
+ *
2728
+ * @returns {Promise<void>} Resolves when the primary key constraint has been successfully removed.
2729
+ */
2730
+ async dropPrimaryKey({ table } = {}) {
2731
+ const exists = await this.hasPrimaryKey({ table });
2732
+ if (!exists) {
2733
+ throw new Error('The specified primary constraint was not found.');
2734
+ }
2735
+ await this.rawQuery(this._queryBuilder().dropPrimaryKey({
2736
+ table: table ?? String(this.getTableName())
2737
+ }));
2738
+ return;
2739
+ }
2473
2740
  /**
2474
2741
  * The 'bindColumn' method is used to concat table and column -> `users`.`id`
2475
2742
  * @param {string} column
@@ -2683,11 +2950,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2683
2950
  * The 'find' method is used to retrieve a single record from a database table by its primary key.
2684
2951
  *
2685
2952
  * This method allows you to quickly fetch a specific record by specifying the primary key value, which is typically an integer id.
2686
- * @param {number} id
2953
+ * @param {number | string} primaryKey
2687
2954
  * @returns {promise<any>}
2688
2955
  */
2689
- async find(id) {
2690
- this.where("id", id);
2956
+ async find(primaryKey) {
2957
+ this.where("id", primaryKey);
2691
2958
  return await this.first();
2692
2959
  }
2693
2960
  /**
@@ -2695,17 +2962,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2695
2962
  *
2696
2963
  * It allows you to split a large set of query results into smaller, more manageable pages,
2697
2964
  * making it easier to display data in a web application and improve user experience
2698
- * @param {?object} paginationOptions
2699
- * @param {number} paginationOptions.limit default 15
2700
- * @param {number} paginationOptions.page default 1
2965
+ * @param {object?} opts by default page = 1 , limit = 15
2966
+ * @property {number?} opts.limit
2967
+ * @property {number?} opts.page
2968
+ * @property {boolean?} opts.distinct
2701
2969
  * @returns {promise<Pagination>}
2702
2970
  */
2703
- async pagination(paginationOptions) {
2971
+ async pagination(opts) {
2704
2972
  let limit = 15;
2705
2973
  let page = 1;
2706
- if (paginationOptions != null) {
2707
- limit = paginationOptions?.limit || limit;
2708
- page = paginationOptions?.page || page;
2974
+ if (opts?.distinct) {
2975
+ this.distinct();
2976
+ }
2977
+ if (opts != null) {
2978
+ limit = opts?.limit || limit;
2979
+ page = opts?.page || page;
2709
2980
  }
2710
2981
  const currentPage = page;
2711
2982
  const nextPage = currentPage + 1;
@@ -2717,7 +2988,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2717
2988
  const result = await this._queryStatement(sql);
2718
2989
  if (!result.length)
2719
2990
  return {
2720
- //@ts-ignore
2721
2991
  meta: {
2722
2992
  total: 0,
2723
2993
  limit,
@@ -2726,6 +2996,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2726
2996
  last_page: 0,
2727
2997
  next_page: 0,
2728
2998
  prev_page: 0,
2999
+ page: {
3000
+ prev: 0,
3001
+ next: 0,
3002
+ current: currentPage,
3003
+ last: 0,
3004
+ },
2729
3005
  },
2730
3006
  data: [],
2731
3007
  };
@@ -2733,13 +3009,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2733
3009
  .copyBuilder(this, { where: true, join: true })
2734
3010
  .bind(this.$pool.get())
2735
3011
  .debug(this.$state.get("DEBUG"))
2736
- .unset({ alias: true })
2737
3012
  .count();
2738
3013
  let lastPage = Math.ceil(total / limit) || 0;
2739
3014
  lastPage = lastPage > 1 ? lastPage : 1;
2740
3015
  const count = result?.length ?? 0;
2741
3016
  return {
2742
- //@ts-ignore
2743
3017
  meta: {
2744
3018
  total: total,
2745
3019
  limit: limit,
@@ -2748,6 +3022,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2748
3022
  last_page: lastPage,
2749
3023
  next_page: nextPage,
2750
3024
  prev_page: prevPage,
3025
+ page: {
3026
+ prev: prevPage,
3027
+ next: nextPage,
3028
+ current: currentPage,
3029
+ last: lastPage,
3030
+ },
2751
3031
  },
2752
3032
  data: result ?? [],
2753
3033
  };
@@ -2757,19 +3037,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2757
3037
  *
2758
3038
  * It allows you to split a large set of query results into smaller, more manageable pages,
2759
3039
  * making it easier to display data in a web application and improve user experience
2760
- * @param {?object} paginationOptions
2761
- * @param {number} paginationOptions.limit
2762
- * @param {number} paginationOptions.page
3040
+ * @param {object?} opts by default page = 1 , limit = 15
3041
+ * @property {number?} opts.limit
3042
+ * @property {number?} opts.page
3043
+ * @property {boolean?} opts.distinct
2763
3044
  * @returns {promise<Pagination>}
2764
3045
  */
2765
- async paginate(paginationOptions) {
2766
- let limit = 15;
2767
- let page = 1;
2768
- if (paginationOptions != null) {
2769
- limit = paginationOptions?.limit || limit;
2770
- page = paginationOptions?.page || page;
2771
- }
2772
- return await this.pagination({ limit, page });
3046
+ async paginate(opts) {
3047
+ return await this.pagination(opts);
2773
3048
  }
2774
3049
  /**
2775
3050
  *
@@ -2942,7 +3217,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2942
3217
  : distinct
2943
3218
  ? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
2944
3219
  : `${this.bindColumn(column)}`;
2945
- this.selectRaw(`${this.$constants("COUNT")}(${column}) ${this.$constants("AS")} \`aggregate\``);
3220
+ this.selectRaw(`${this.$constants("COUNT")}(${column}) ${this.$constants(`${this.$constants("AS")}`)} \`aggregate\``);
2946
3221
  const sql = this._queryBuilder().select();
2947
3222
  const result = await this._queryStatement(sql);
2948
3223
  return Number(this._resultHandler(result.reduce((prev, cur) => prev + Number(cur?.aggregate ?? 0), 0) || 0));
@@ -3264,6 +3539,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3264
3539
  .map((table) => String(Object.values(table)[0]))
3265
3540
  .filter((d) => d != null || d !== "");
3266
3541
  }
3542
+ /**
3543
+ * The `getTable` method is used to retrieve a list of all table names
3544
+ * in the current database.
3545
+ *
3546
+ * @returns {Promise<string[]>} A promise that resolves to an array of table names.
3547
+ *
3548
+ * @async
3549
+ */
3550
+ async hasTable(table) {
3551
+ const result = await this._queryStatement(this._queryBuilder().hasTable({ database: this.database(), table }));
3552
+ return Boolean(result[0]?.IS_EXISTS ?? false);
3553
+ }
3267
3554
  /**
3268
3555
  *
3269
3556
  * The 'showColumns' method is used to show columns table.
@@ -3388,10 +3675,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3388
3675
  ].join('\n'));
3389
3676
  return false;
3390
3677
  }
3391
- const sql = [
3392
- `${this.$constants("TRUNCATE_TABLE")}`,
3393
- `${table}`,
3394
- ].join(" ");
3678
+ const sql = this._queryBuilder().truncate(this.getTableName());
3395
3679
  if (!force) {
3396
3680
  console.log([
3397
3681
  `\x1b[33m`,
@@ -3402,7 +3686,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3402
3686
  ].join('\n'));
3403
3687
  return false;
3404
3688
  }
3405
- const childFKs = await this.getChildFKs();
3689
+ const childFKs = await this.getChildFKs().catch(() => []);
3406
3690
  if (childFKs.length && !dropFK) {
3407
3691
  console.log([
3408
3692
  `\x1b[33m`,
@@ -3553,7 +3837,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3553
3837
  return data;
3554
3838
  }
3555
3839
  async _queryStatement(sql) {
3556
- sql = this._queryBuilder().format([sql]);
3557
3840
  try {
3558
3841
  const startTime = +new Date();
3559
3842
  const result = await this.$pool.query(sql);
@@ -3609,7 +3892,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3609
3892
  await this.$utils.wait(this.$state.get("AFTER_SAVE"));
3610
3893
  const data = await new Builder()
3611
3894
  .copyBuilder(this, { select: true })
3612
- .whereIn("id", result.$meta.insertIds)
3895
+ .whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
3896
+ .debug(this.$state.get('DEBUG'))
3613
3897
  .bind(this.$pool.get())
3614
3898
  .first();
3615
3899
  return this._resultHandler(data);
@@ -3625,7 +3909,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3625
3909
  await this.$utils.wait(this.$state.get("AFTER_SAVE"));
3626
3910
  const results = await new Builder()
3627
3911
  .copyBuilder(this, { select: true })
3628
- .whereIn("id", result.$meta.insertIds)
3912
+ .whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
3913
+ .debug(this.$state.get('DEBUG'))
3629
3914
  .bind(this.$pool.get())
3630
3915
  .first();
3631
3916
  return this._resultHandler(results);
@@ -3637,7 +3922,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3637
3922
  await this.$utils.wait(this.$state.get("AFTER_SAVE"));
3638
3923
  const resultData = await new Builder()
3639
3924
  .copyBuilder(this, { select: true, limit: true })
3640
- .whereIn("id", result.$meta.insertIds)
3925
+ .whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
3926
+ .debug(this.$state.get('DEBUG'))
3641
3927
  .bind(this.$pool.get())
3642
3928
  .get();
3643
3929
  return this._resultHandler(resultData);
@@ -3663,7 +3949,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3663
3949
  await this.$utils.wait(this.$state.get("AFTER_SAVE"));
3664
3950
  const data = await new Builder()
3665
3951
  .copyBuilder(this, { select: true })
3666
- .whereIn("id", result.$meta.insertIds)
3952
+ .whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
3953
+ .debug(this.$state.get('DEBUG'))
3667
3954
  .bind(this.$pool.get())
3668
3955
  .first();
3669
3956
  const resultData = data == null ? null : { ...data, $action: "insert" };
@@ -3709,7 +3996,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3709
3996
  await this.$utils.wait(this.$state.get("AFTER_SAVE"));
3710
3997
  const data = await new Builder()
3711
3998
  .copyBuilder(this, { select: true })
3712
- .whereIn("id", result.$meta.insertIds)
3999
+ .whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
4000
+ .debug(this.$state.get('DEBUG'))
3713
4001
  .bind(this.$pool.get())
3714
4002
  .first();
3715
4003
  const resultData = data == null ? null : { ...data, $action: "insert" };
@@ -3865,7 +4153,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3865
4153
  const isReaded = [
3866
4154
  this.$constants("SELECT"),
3867
4155
  this.$constants("SHOW"),
3868
- this.$constants("DESCRIBE"),
4156
+ this.$constants("DESCRIBE")
3869
4157
  ].includes(first) &&
3870
4158
  (!sql
3871
4159
  .toUpperCase()
@@ -3903,10 +4191,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3903
4191
  ? poolCluster?.queryBuilder
3904
4192
  : poolCluster?.masters[0]?.queryBuilder;
3905
4193
  },
4194
+ transaction: async () => {
4195
+ return await poolCluster.masters[0].connection();
4196
+ },
3906
4197
  };
3907
4198
  }
3908
4199
  let pool = Pool_1.Pool.connect();
3909
4200
  return {
4201
+ transaction: async () => {
4202
+ return await pool.connection();
4203
+ },
3910
4204
  query: async (sql) => {
3911
4205
  return pool.query(sql);
3912
4206
  },
@@ -3922,13 +4216,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3922
4216
  };
3923
4217
  })();
3924
4218
  this.$state = new StateManager_1.StateManager("default");
3925
- this.$constants = (name) => {
4219
+ this.$constants = ((name) => {
3926
4220
  if (name == null)
3927
4221
  return constants_1.CONSTANTS;
3928
- if (!constants_1.CONSTANTS.hasOwnProperty(name))
3929
- throw new Error(`Not found that constant : '${name}'`);
4222
+ if (!Object.prototype.hasOwnProperty.call(constants_1.CONSTANTS, name)) {
4223
+ throw new Error(`Not found that constant : '${String(name)}'`);
4224
+ }
3930
4225
  return constants_1.CONSTANTS[name];
3931
- };
4226
+ });
3932
4227
  }
3933
4228
  }
3934
4229
  exports.Builder = Builder;