@breautek/storm 4.6.3 → 5.0.0

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 (101) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/docs/README.md +7 -5
  3. package/docs/classes/Application.md +26 -26
  4. package/docs/classes/BackendAuthenticationMiddleware.md +2 -2
  5. package/docs/classes/CORSMiddleware.md +6 -6
  6. package/docs/classes/ConfigLoader.md +1 -1
  7. package/docs/classes/Database.md +12 -12
  8. package/docs/classes/DatabaseCastObject.md +3 -3
  9. package/docs/classes/DatabaseConnection.md +21 -21
  10. package/docs/classes/DatabaseQueryError.md +10 -10
  11. package/docs/classes/DeadLockError.md +10 -10
  12. package/docs/classes/DiskSpaceError.md +10 -10
  13. package/docs/classes/DropTemporaryTableQuery.md +10 -10
  14. package/docs/classes/DumpStream.md +1 -1
  15. package/docs/classes/DuplicateEntryError.md +10 -10
  16. package/docs/classes/EntityNotFoundError.md +10 -10
  17. package/docs/classes/ExpiredTokenError.md +10 -10
  18. package/docs/classes/Handler.md +13 -34
  19. package/docs/classes/InternalError.md +10 -10
  20. package/docs/classes/InvalidCredentialsError.md +10 -10
  21. package/docs/classes/InvalidValueError.md +10 -10
  22. package/docs/classes/LineString.md +4 -4
  23. package/docs/classes/ManagedDatabaseConnection.md +19 -19
  24. package/docs/classes/Middleware.md +3 -3
  25. package/docs/classes/MissingConfigError.md +10 -10
  26. package/docs/classes/MissingParameterError.md +10 -10
  27. package/docs/classes/MySQLConnection.md +22 -22
  28. package/docs/classes/MySQLDatabase.md +13 -13
  29. package/docs/classes/NotImplementedError.md +10 -10
  30. package/docs/classes/Point.md +4 -4
  31. package/docs/classes/Polygon.md +4 -4
  32. package/docs/classes/Query.md +34 -10
  33. package/docs/classes/RawError.md +10 -10
  34. package/docs/classes/RawQuery.md +10 -10
  35. package/docs/classes/Request.md +18 -18
  36. package/docs/classes/Response.md +11 -11
  37. package/docs/classes/ResponseData.md +7 -7
  38. package/docs/classes/ServiceProvider.md +15 -15
  39. package/docs/classes/ServiceResponse.md +19 -4
  40. package/docs/classes/SetSessionVariableQuery.md +8 -8
  41. package/docs/classes/StormError.md +10 -10
  42. package/docs/classes/TemporaryTableQuery.md +8 -8
  43. package/docs/classes/Token.md +2 -2
  44. package/docs/classes/TokenManager.md +4 -4
  45. package/docs/classes/Transaction.md +161 -0
  46. package/docs/classes/UnauthorizedAccessError.md +10 -10
  47. package/docs/enums/ErrorCode.md +9 -9
  48. package/docs/enums/ExitCode.md +2 -2
  49. package/docs/enums/HTTPMethod.md +4 -4
  50. package/docs/enums/IsolationLevel.md +4 -4
  51. package/docs/enums/JWTError.md +2 -2
  52. package/docs/enums/StatusCode.md +48 -48
  53. package/docs/interfaces/IConfig.md +8 -8
  54. package/docs/interfaces/IDatabaseConfig.md +6 -6
  55. package/docs/interfaces/IDatabaseConnection.md +16 -16
  56. package/docs/interfaces/IErrorResponse.md +5 -5
  57. package/docs/interfaces/IFormData.md +2 -2
  58. package/docs/interfaces/IHandler.md +1 -1
  59. package/docs/interfaces/IInsertQueryResult.md +3 -3
  60. package/docs/interfaces/IJWTVerifyOptions.md +1 -1
  61. package/docs/interfaces/IQueryable.md +119 -0
  62. package/docs/interfaces/IRequestResponse.md +2 -2
  63. package/docs/interfaces/ISetSessionVariableQueryInput.md +2 -2
  64. package/docs/interfaces/ITemporaryTableQueryInput.md +2 -2
  65. package/docs/interfaces/IUpdateQueryResult.md +2 -2
  66. package/jest.config.js +9 -5
  67. package/lib/DatabaseConnection.d.ts +3 -3
  68. package/lib/DatabaseConnection.js.map +1 -1
  69. package/lib/Handler.d.ts +0 -1
  70. package/lib/Handler.js +0 -2
  71. package/lib/Handler.js.map +1 -1
  72. package/lib/IDatabaseConnection.d.ts +3 -3
  73. package/lib/IQueryable.d.ts +8 -0
  74. package/lib/IQueryable.js +18 -0
  75. package/lib/IQueryable.js.map +1 -0
  76. package/lib/MySQLConnection.js +15 -4
  77. package/lib/MySQLConnection.js.map +1 -1
  78. package/lib/Query.d.ts +3 -2
  79. package/lib/Query.js.map +1 -1
  80. package/lib/ServiceResponse.d.ts +2 -0
  81. package/lib/ServiceResponse.js +3 -0
  82. package/lib/ServiceResponse.js.map +1 -1
  83. package/lib/SetSessionVariableQuery.js +2 -2
  84. package/lib/SetSessionVariableQuery.js.map +1 -1
  85. package/lib/Transaction.d.ts +24 -0
  86. package/lib/Transaction.js +82 -0
  87. package/lib/Transaction.js.map +1 -0
  88. package/lib/api.d.ts +2 -0
  89. package/lib/api.js +4 -2
  90. package/lib/api.js.map +1 -1
  91. package/package.json +12 -12
  92. package/src/DatabaseConnection.ts +4 -4
  93. package/src/Handler.ts +0 -4
  94. package/src/IDatabaseConnection.ts +3 -3
  95. package/src/IQueryable.ts +25 -0
  96. package/src/MySQLConnection.ts +15 -4
  97. package/src/Query.ts +3 -2
  98. package/src/ServiceResponse.ts +5 -0
  99. package/src/SetSessionVariableQuery.ts +2 -2
  100. package/src/Transaction.ts +97 -0
  101. package/src/api.ts +2 -0
@@ -0,0 +1,97 @@
1
+ /*
2
+ Copyright 2017-2023 Norman Breau
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { IDatabaseConnection } from './IDatabaseConnection';
18
+ import {IQueryable} from './IQueryable';
19
+ import { Application } from './Application';
20
+ import { IsolationLevel } from './IsolationLevel';
21
+ import { InternalError } from './InternalError';
22
+ import { DeadLockError } from './DeadLockError';
23
+ import { InvalidValueError } from './InvalidValueError';
24
+
25
+ const TAG: string = 'Transaction';
26
+
27
+ export type ITransactionExecutor = (connection: IDatabaseConnection) => Promise<void>;
28
+
29
+ /**
30
+ * A class encapsulating an entire transaction from beginning to commitment.
31
+ *
32
+ * This encapsulates a routine to conduct for the transaction.
33
+ * Should the transaction fail due to a deadlock, the transaction will automatically
34
+ * be tried.
35
+ */
36
+ export class Transaction implements IQueryable<void> {
37
+ private $retryLimit: number;
38
+ private $application: Application;
39
+ private $isolationLevel: IsolationLevel;
40
+ private $executor: ITransactionExecutor;
41
+
42
+ public constructor(app: Application, executor: ITransactionExecutor, retryLimit: number = Infinity, isolationLevel: IsolationLevel = IsolationLevel.REPEATABLE_READ) {
43
+ this.$application = app;
44
+ this.$executor = executor;
45
+
46
+ if (retryLimit === null || retryLimit === undefined) {
47
+ retryLimit = Infinity;
48
+ }
49
+ else if (retryLimit <= 0) {
50
+ throw new InvalidValueError('retryLimit', 'integer > 0', retryLimit);
51
+ }
52
+
53
+ this.$retryLimit = retryLimit;
54
+ this.$isolationLevel = isolationLevel;
55
+ }
56
+
57
+ public async onPreQuery(connection: IDatabaseConnection): Promise<void> {}
58
+
59
+ public getQuery(connection: IDatabaseConnection): string {
60
+ return null;
61
+ }
62
+ public getParametersForQuery(): Record<string, any> {
63
+ return null;
64
+ }
65
+ public async onPostProcess(connection: IDatabaseConnection, results: any): Promise<void> {
66
+ return null;
67
+ }
68
+
69
+ public async execute(connection: IDatabaseConnection): Promise<void> {
70
+ if (connection.isTransaction()) {
71
+ throw new InternalError('Connection must not be in an active transaction. Commit your current transaction first.');
72
+ }
73
+
74
+ let attemptCount: number = 0;
75
+ do {
76
+ attemptCount++;
77
+ this.$application.getLogger().info(TAG, `Starting transaction attempt ${attemptCount} of ${this.$retryLimit === Infinity ? 'Infinity' : this.$retryLimit.toString()}`);
78
+ await connection.startTransaction(this.$isolationLevel);
79
+ try {
80
+ await this.$executor(connection);
81
+ await connection.commit();
82
+
83
+ // If we made it here, we can break out of our retry loop
84
+ break;
85
+ }
86
+ catch (ex) {
87
+ if (attemptCount < this.$retryLimit && ex instanceof DeadLockError) {
88
+ this.$application.getLogger().warn(TAG, `Deadlock received... retrying transaction`);
89
+ }
90
+ else {
91
+ await connection.rollback();
92
+ throw ex;
93
+ }
94
+ }
95
+ } while (attemptCount < this.$retryLimit);
96
+ }
97
+ }
package/src/api.ts CHANGED
@@ -36,6 +36,7 @@ export {IDeleteQueryResult} from './IDeleteQueryResult';
36
36
  export {IUpdateQueryResult} from './IUpdateQueryResult';
37
37
  export {SetSessionVariableQuery, ISetSessionVariableQueryInput} from './SetSessionVariableQuery';
38
38
  export {IsolationLevel} from './IsolationLevel';
39
+ export {Transaction} from './Transaction';
39
40
 
40
41
  // Errors
41
42
  export {ErrorCode} from './ErrorCode';
@@ -83,6 +84,7 @@ export {IFormData} from './IFormData';
83
84
  export {IDatabaseConnection} from './IDatabaseConnection';
84
85
  export {IServiceHeaders} from './IServiceHeaders';
85
86
  export {IAuthTokenData} from './IAuthTokenData';
87
+ export {IQueryable} from './IQueryable';
86
88
 
87
89
  // Token
88
90
  export {Token} from './Token';