tspace-mysql 1.9.4 → 1.9.5

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 (121) hide show
  1. package/README.md +376 -72
  2. package/dist/cli/dump/db.js +3 -15
  3. package/dist/cli/dump/db.js.map +1 -1
  4. package/dist/cli/index.js +0 -3
  5. package/dist/cli/index.js.map +1 -1
  6. package/dist/cli/migrations/make-db.js +1 -1
  7. package/dist/cli/migrations/make-db.js.map +1 -1
  8. package/dist/cli/migrations/make-model.js +1 -6
  9. package/dist/cli/migrations/make-model.js.map +1 -1
  10. package/dist/lib/config/index.d.ts +6 -5
  11. package/dist/lib/config/index.js +18 -8
  12. package/dist/lib/config/index.js.map +1 -1
  13. package/dist/lib/constants/index.d.ts +1 -1
  14. package/dist/lib/constants/index.js +1 -1
  15. package/dist/lib/constants/index.js.map +1 -1
  16. package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +2 -1
  17. package/dist/lib/core/Abstracts/AbstractDB.d.ts +5 -3
  18. package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
  19. package/dist/lib/core/Abstracts/AbstractModel.d.ts +1 -2
  20. package/dist/lib/core/Abstracts/AbstractModel.js +0 -1
  21. package/dist/lib/core/Abstracts/AbstractModel.js.map +1 -1
  22. package/dist/lib/core/Abstracts/AbstractView.d.ts +2 -1
  23. package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
  24. package/dist/lib/core/Blueprint.d.ts +13 -3
  25. package/dist/lib/core/Blueprint.js +16 -1
  26. package/dist/lib/core/Blueprint.js.map +1 -1
  27. package/dist/lib/core/Builder.d.ts +172 -19
  28. package/dist/lib/core/Builder.js +564 -197
  29. package/dist/lib/core/Builder.js.map +1 -1
  30. package/dist/lib/core/Cache/DBCache.js +6 -5
  31. package/dist/lib/core/Cache/DBCache.js.map +1 -1
  32. package/dist/lib/core/Cache/index.js +1 -1
  33. package/dist/lib/core/Cache/index.js.map +1 -1
  34. package/dist/lib/core/Contracts/Audit.d.ts +5 -3
  35. package/dist/lib/core/Contracts/Audit.js +18 -2
  36. package/dist/lib/core/Contracts/Audit.js.map +1 -1
  37. package/dist/lib/core/DB.d.ts +148 -190
  38. package/dist/lib/core/DB.js +459 -525
  39. package/dist/lib/core/DB.js.map +1 -1
  40. package/dist/lib/core/Driver/index.d.ts +40 -15
  41. package/dist/lib/core/Driver/index.js +3 -0
  42. package/dist/lib/core/Driver/index.js.map +1 -1
  43. package/dist/lib/core/Driver/mariadb/MariadbDriver.d.ts +3 -2
  44. package/dist/lib/core/Driver/mariadb/MariadbDriver.js +7 -5
  45. package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
  46. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +42 -17
  47. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +281 -154
  48. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
  49. package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +4 -3
  50. package/dist/lib/core/Driver/mongodb/MongodbDriver.js +7 -5
  51. package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -1
  52. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +40 -15
  53. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +205 -157
  54. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -1
  55. package/dist/lib/core/Driver/mysql/MysqlDriver.d.ts +3 -2
  56. package/dist/lib/core/Driver/mysql/MysqlDriver.js +6 -14
  57. package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
  58. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +42 -17
  59. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +251 -144
  60. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
  61. package/dist/lib/core/Driver/postgres/PostgresDriver.d.ts +3 -2
  62. package/dist/lib/core/Driver/postgres/PostgresDriver.js +7 -5
  63. package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
  64. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +45 -19
  65. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +471 -287
  66. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
  67. package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +3 -2
  68. package/dist/lib/core/Driver/sqlite/SqliteDriver.js +8 -6
  69. package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -1
  70. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +47 -19
  71. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +331 -203
  72. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -1
  73. package/dist/lib/core/Model.d.ts +255 -52
  74. package/dist/lib/core/Model.js +459 -117
  75. package/dist/lib/core/Model.js.map +1 -1
  76. package/dist/lib/core/Package.d.ts +5 -0
  77. package/dist/lib/core/Package.js +7 -2
  78. package/dist/lib/core/Package.js.map +1 -1
  79. package/dist/lib/core/Pool.d.ts +1 -1
  80. package/dist/lib/core/Pool.js +53 -75
  81. package/dist/lib/core/Pool.js.map +1 -1
  82. package/dist/lib/core/Queue/index.d.ts +2 -11
  83. package/dist/lib/core/Queue/index.js +3 -3
  84. package/dist/lib/core/Queue/index.js.map +1 -1
  85. package/dist/lib/core/Queue/types.d.ts +32 -3
  86. package/dist/lib/core/Queue/worker-lock.d.ts +17 -0
  87. package/dist/lib/core/Queue/worker-lock.js +28 -0
  88. package/dist/lib/core/Queue/worker-lock.js.map +1 -0
  89. package/dist/lib/core/Queue/worker.d.ts +37 -23
  90. package/dist/lib/core/Queue/worker.js +385 -171
  91. package/dist/lib/core/Queue/worker.js.map +1 -1
  92. package/dist/lib/core/RelationManager.js +1 -1
  93. package/dist/lib/core/RelationManager.js.map +1 -1
  94. package/dist/lib/core/Repository.d.ts +164 -29
  95. package/dist/lib/core/Repository.js +181 -40
  96. package/dist/lib/core/Repository.js.map +1 -1
  97. package/dist/lib/core/Schema.d.ts +11 -7
  98. package/dist/lib/core/Schema.js +249 -80
  99. package/dist/lib/core/Schema.js.map +1 -1
  100. package/dist/lib/core/SqlLike.js +4 -4
  101. package/dist/lib/core/SqlLike.js.map +1 -1
  102. package/dist/lib/core/StateManager.d.ts +19 -0
  103. package/dist/lib/core/StateManager.js +9 -4
  104. package/dist/lib/core/StateManager.js.map +1 -1
  105. package/dist/lib/core/UtilityTypes.d.ts +17 -3
  106. package/dist/lib/core/UtilityTypes.js.map +1 -1
  107. package/dist/lib/core/View.d.ts +3 -1
  108. package/dist/lib/core/View.js +18 -25
  109. package/dist/lib/core/View.js.map +1 -1
  110. package/dist/lib/core/index.d.ts +2 -0
  111. package/dist/lib/core/index.js +3 -1
  112. package/dist/lib/core/index.js.map +1 -1
  113. package/dist/lib/types/index.d.ts +77 -90
  114. package/dist/lib/types/repository/index.d.ts +18 -26
  115. package/dist/lib/utils/index.d.ts +7 -3
  116. package/dist/lib/utils/index.js +99 -37
  117. package/dist/lib/utils/index.js.map +1 -1
  118. package/package.json +1 -2
  119. package/dist/cli/dump/table.d.ts +0 -4
  120. package/dist/cli/dump/table.js +0 -41
  121. package/dist/cli/dump/table.js.map +0 -1
@@ -3,7 +3,7 @@ import { DB } from "./DB";
3
3
  import { Join } from "./Join";
4
4
  import { CONSTANTS } from "../constants";
5
5
  import { QueryBuilder } from "./Driver";
6
- import type { TPagination, TConnectionOptions, TPoolConnected, TConnectionTransaction, TDriver, TSaveBuilderResult, TAction } from "../types";
6
+ import type { TPagination, TConnectionOptions, TPoolConnected, TDriver, TSaveBuilderResult, TAction, TCursorPagination, TExplain, TConnection } from "../types";
7
7
  declare class Builder<TA extends TAction = null> extends AbstractBuilder {
8
8
  constructor();
9
9
  /**
@@ -28,6 +28,37 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
28
28
  * @returns {string} database
29
29
  */
30
30
  database(): string;
31
+ /**
32
+ * The 'useNode' method is used to selects the database node used to execute the query.
33
+ *
34
+ * - `primary`: Uses the primary database node.
35
+ * - `replica`: Uses a replica node. If `node` is provided, the specified
36
+ * replica is used; otherwise, a replica is selected randomly.
37
+ *
38
+ * @param type - The database node type to use.
39
+ * @param options - Node selection options.
40
+ * @param options.node - 1-based replica node number. Only applicable when
41
+ * `type` is `replica`.
42
+ *
43
+ * @throws {Error} If node selection is requested outside cluster mode.
44
+ * @throws {Error} If a replica node is requested but no replicas are available.
45
+ *
46
+ * @example
47
+ * // Use the primary node
48
+ * query.useNode('primary');
49
+ *
50
+ * @example
51
+ * // Use a random replica
52
+ * query.useNode('replica');
53
+ *
54
+ * @example
55
+ * // Use replica node 2
56
+ * query.useNode('replica', { node: 2 });
57
+ */
58
+ useNode(type: 'primary'): this;
59
+ useNode(type: 'replica', options?: {
60
+ node?: number;
61
+ }): this;
31
62
  /**
32
63
  * The 'rowLock' method is used to row level locks (`FOR UPDATE` or `FOR SHARE`) to the query.
33
64
  *
@@ -35,8 +66,14 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
35
66
  * @returns {this} this
36
67
  */
37
68
  rowLock(mode: "FOR_UPDATE" | "FOR_SHARE", opts?: {
38
- skipLocked?: boolean;
39
- nowait?: boolean;
69
+ skipLocked: true;
70
+ nowait?: false;
71
+ } | {
72
+ skipLocked?: false;
73
+ nowait: true;
74
+ } | {
75
+ skipLocked?: false;
76
+ nowait?: false;
40
77
  }): this;
41
78
  /**
42
79
  * The 'forUpdate' method is used to applies a row-level exclusive lock (`FOR UPDATE`) to the query.
@@ -58,8 +95,14 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
58
95
  * @returns {this} Returns the query builder instance for chaining.
59
96
  */
60
97
  forUpdate(opts?: {
61
- skipLocked?: boolean;
62
- nowait?: boolean;
98
+ skipLocked: true;
99
+ nowait?: false;
100
+ } | {
101
+ skipLocked?: false;
102
+ nowait: true;
103
+ } | {
104
+ skipLocked?: false;
105
+ nowait?: false;
63
106
  }): this;
64
107
  /**
65
108
  * The 'forShare' method is used to applies a row-level exclusive lock (`FOR SHARE`) to the query.
@@ -81,8 +124,14 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
81
124
  * @returns {this} Returns the query builder instance for chaining.
82
125
  */
83
126
  forShare(opts?: {
84
- skipLocked?: boolean;
85
- nowait?: boolean;
127
+ skipLocked: true;
128
+ nowait?: false;
129
+ } | {
130
+ skipLocked?: false;
131
+ nowait: true;
132
+ } | {
133
+ skipLocked?: false;
134
+ nowait?: false;
86
135
  }): this;
87
136
  /**
88
137
  * The 'unset' method is used to drop a property as desired.
@@ -244,7 +293,7 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
244
293
  *
245
294
  * @returns {this} this
246
295
  */
247
- void(): this;
296
+ void(): Builder<void>;
248
297
  /**
249
298
  * The 'when' method is used to specify if condition should be true will be next to the actions
250
299
  * @param {string | number | undefined | null | Boolean} condition when condition true will return query callback
@@ -1188,6 +1237,8 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
1188
1237
  RefTable: string;
1189
1238
  RefColumn: string;
1190
1239
  Column: string;
1240
+ OnDelete: string;
1241
+ OnUpdate: string;
1191
1242
  }[]>;
1192
1243
  /**
1193
1244
  * The `hasFK` method is used to determine whether a specific foreign key
@@ -1513,11 +1564,11 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
1513
1564
  */
1514
1565
  connection(options: TConnectionOptions): this;
1515
1566
  loadOptionsEnv(env?: string): {
1516
- readonly cluster: string | false;
1567
+ readonly cluster: boolean;
1517
1568
  readonly driver: string;
1518
1569
  readonly host: string | undefined;
1519
1570
  readonly port: string | 3306;
1520
- readonly username: string | undefined;
1571
+ readonly username: string;
1521
1572
  readonly password: string;
1522
1573
  readonly database: string | undefined;
1523
1574
  } | null;
@@ -1532,7 +1583,7 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
1532
1583
  * @param {object} connection pool database
1533
1584
  * @returns {this} this
1534
1585
  */
1535
- bind(connection: TPoolConnected | TConnectionTransaction): this;
1586
+ bind(connection: TPoolConnected | TConnection | TConnection): this;
1536
1587
  /**
1537
1588
  * This 'union' method is used to union statement sql
1538
1589
  *
@@ -1625,6 +1676,42 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
1625
1676
  page?: number;
1626
1677
  distinct?: boolean;
1627
1678
  }): Promise<TPagination>;
1679
+ /**
1680
+ * The 'cursorPagination' method is used to perform cursor-based pagination
1681
+ * on a set of database query results obtained through the Query Builder.
1682
+ *
1683
+ * It uses cursor-based pagination instead of offset pagination, making it
1684
+ * more efficient when navigating through large datasets.
1685
+ *
1686
+ * @param {Object?} opts query opts
1687
+ * @param {number?} opts.limit number of records per page, default is 15
1688
+ * @param {string?} opts.cursor cursor used to retrieve the next page
1689
+ * @param {boolean?} opts.distinct whether to retrieve distinct records
1690
+ * @returns {Promise<TCursorPagination>}
1691
+ */
1692
+ cursorPagination(opts?: {
1693
+ limit?: number;
1694
+ cursor?: string;
1695
+ distinct?: boolean;
1696
+ }): Promise<TCursorPagination>;
1697
+ /**
1698
+ * The 'cursorPaginate' method is used to perform cursor-based pagination
1699
+ * on a set of database query results obtained through the Query Builder.
1700
+ *
1701
+ * It uses cursor-based pagination instead of offset pagination, making it
1702
+ * more efficient when navigating through large datasets.
1703
+ *
1704
+ * @param {Object?} opts query opts
1705
+ * @param {number?} opts.limit number of records per page, default is 15
1706
+ * @param {string?} opts.cursor cursor used to retrieve the next page
1707
+ * @param {boolean?} opts.distinct whether to retrieve distinct records
1708
+ * @returns {Promise<TCursorPagination>}
1709
+ */
1710
+ cursorPaginate(opts?: {
1711
+ limit?: number;
1712
+ cursor?: string;
1713
+ distinct?: boolean;
1714
+ }): Promise<TCursorPagination>;
1628
1715
  /**
1629
1716
  *
1630
1717
  * The 'first' method is used to retrieve the first record that matches the query conditions.
@@ -1679,15 +1766,77 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
1679
1766
  */
1680
1767
  findMany(cb?: Function): Promise<any[]>;
1681
1768
  /**
1682
- * The 'toAsyncIterable' method is used to execute a database query and return the result set as an asynchronous iterable.
1769
+ * The 'explain' method is used to analyze the query execution plan
1770
+ * without executing the query itself.
1683
1771
  *
1684
- * It retrieves multiple records from a database table based on the criteria specified in the query.
1685
- * The optional callback function can be used to modify the generated SQL query before execution.
1772
+ * It retrieves the execution plan from the database and analyzes it
1773
+ * for potential performance issues such as full table scans, filesorts,
1774
+ * and temporary tables.
1775
+ * @param {string?} sql query for explain
1776
+ * @returns {Promise<TExplain>} query execution plan and analysis
1777
+ */
1778
+ explain(sql?: string): Promise<TExplain>;
1779
+ /**
1780
+ * The 'lazy' method is used to retrieve and process records incrementally
1781
+ * without loading the entire result set into memory at once.
1686
1782
  *
1687
- * @param {Function?} cb callback function return query sql
1688
- * @returns {AsyncIterable<any>}
1783
+ * It retrieves records in batches based on the specified limit and invokes
1784
+ * the callback for each record. The process continues until all records
1785
+ * have been retrieved and processed.
1786
+ *
1787
+ * The optional delayMs can be used to add a delay between each batch query,
1788
+ * which can help reduce database load when processing a large number of records.
1789
+ *
1790
+ * @param {Function} callback callback function that receives each retrieved record
1791
+ * @param {Object?} opts query opts
1792
+ * @param {number?} opts.limit number of records to retrieve per query
1793
+ * @param {number?} opts.offset number of records to skip before processing
1794
+ * @param {number?} opts.delayMs delay in milliseconds between each batch
1795
+ * @returns {Promise<void>}
1796
+ */
1797
+ lazy(callback: (result: any) => Promise<void> | void, opts?: {
1798
+ limit?: number;
1799
+ offset?: number;
1800
+ delayMs?: number;
1801
+ }): Promise<void>;
1802
+ /**
1803
+ * The 'chunk' method is used to retrieve records from the database in chunks
1804
+ * and process each chunk through a callback function.
1805
+ *
1806
+ * It retrieves records in batches based on the specified limit and invokes
1807
+ * the callback with each batch of records. The process continues until all
1808
+ * records have been retrieved and processed.
1809
+ *
1810
+ * The optional delayMs can be used to add a delay between each batch query,
1811
+ * which can help reduce database load when processing a large number of records.
1812
+ *
1813
+ * @param {Function} callback callback function that receives the retrieved records
1814
+ * @param {Object?} opts query opts
1815
+ * @param {number?} opts.limit number of records to retrieve per chunk
1816
+ * @param {number?} opts.offset number of records to skip before processing
1817
+ * @param {number?} opts.delayMs delay in milliseconds between each batch
1818
+ * @returns {Promise<void>}
1819
+ */
1820
+ chunk(callback: (results: any[]) => Promise<void> | void, opts?: {
1821
+ limit?: number;
1822
+ offset?: number;
1823
+ delayMs?: number;
1824
+ }): Promise<void>;
1825
+ /**
1826
+ * The 'stream' method is used to executes a SQL statement and asynchronously yields result rows.
1827
+ *
1828
+ * @returns {AsyncGenerator<T>} An async generator that yields result rows.
1829
+ *
1830
+ * @example
1831
+ * ```ts
1832
+ * import { DB } from "tspace-mysql";
1833
+ *
1834
+ * for await (const row of new DB('users').stream()) {
1835
+ * console.log(row);
1836
+ * }
1837
+ * ```
1689
1838
  */
1690
- toAsyncIterable(cb?: Function): AsyncIterable<any>;
1839
+ stream(): AsyncGenerator<any>;
1691
1840
  /**
1692
1841
  * The 'toArray' method is used to execute a database query and retrieve the result set that matches the query conditions.
1693
1842
  *
@@ -1924,7 +2073,7 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
1924
2073
  select?: boolean;
1925
2074
  having?: boolean;
1926
2075
  }): Builder;
1927
- protected _queryBuilder(): QueryBuilder;
2076
+ getQueryBuilder(): QueryBuilder;
1928
2077
  protected _buildQueryStatement(): any;
1929
2078
  protected _resultHandler(data: any): any;
1930
2079
  protected _resultHandlerExists(data: any): any;
@@ -1940,7 +2089,11 @@ declare class Builder<TA extends TAction = null> extends AbstractBuilder {
1940
2089
  private _queryInsert;
1941
2090
  private _queryInsertMultiple;
1942
2091
  protected _handleJoin(type: "INNER_JOIN" | "LEFT_JOIN" | "RIGHT_JOIN" | "CROSS_JOIN" | undefined, localKey: `${string}.${string}` | ((join: Join) => Join), referenceKey?: `${string}.${string}`): this;
1943
- private _initialConnection;
2092
+ private _initClusterPool;
2093
+ private _initSinglePool;
2094
+ private _initPool;
2095
+ private _initConstants;
2096
+ private _initBuilder;
1944
2097
  }
1945
2098
  export { Builder };
1946
2099
  export default Builder;