@decaf-ts/transactional-decorators 0.1.3 → 0.1.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.
- package/LICENSE.md +3 -2
- package/README.md +242 -17
- package/dist/transactional-decorators.cjs +2 -643
- package/dist/transactional-decorators.cjs.map +1 -0
- package/dist/transactional-decorators.js +2 -0
- package/dist/transactional-decorators.js.map +1 -0
- package/lib/Transaction.cjs +1 -1
- package/lib/Transaction.js.map +1 -0
- package/lib/constants.cjs +1 -1
- package/lib/constants.js.map +1 -0
- package/lib/decorators.cjs +1 -1
- package/lib/decorators.js.map +1 -0
- package/lib/esm/Transaction.js +1 -1
- package/lib/esm/Transaction.js.map +1 -0
- package/lib/esm/constants.js +1 -1
- package/lib/esm/constants.js.map +1 -0
- package/lib/esm/decorators.js +1 -1
- package/lib/esm/decorators.js.map +1 -0
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +2 -2
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/TransactionLock.js +1 -1
- package/lib/esm/interfaces/TransactionLock.js.map +1 -0
- package/lib/esm/interfaces/index.js +1 -1
- package/lib/esm/interfaces/index.js.map +1 -0
- package/lib/esm/locks/Lock.js +1 -1
- package/lib/esm/locks/Lock.js.map +1 -0
- package/lib/esm/locks/SyncronousLock.js +1 -1
- package/lib/esm/locks/SyncronousLock.js.map +1 -0
- package/lib/esm/locks/index.js +1 -1
- package/lib/esm/locks/index.js.map +1 -0
- package/lib/esm/types.js +1 -1
- package/lib/esm/types.js.map +1 -0
- package/lib/esm/utils.js +1 -1
- package/lib/esm/utils.js.map +1 -0
- package/lib/index.cjs +2 -2
- package/lib/index.d.ts +1 -1
- package/lib/index.js.map +1 -0
- package/lib/interfaces/TransactionLock.cjs +1 -1
- package/lib/interfaces/TransactionLock.js.map +1 -0
- package/lib/interfaces/index.cjs +1 -1
- package/lib/interfaces/index.js.map +1 -0
- package/lib/locks/Lock.cjs +1 -1
- package/lib/locks/Lock.js.map +1 -0
- package/lib/locks/SyncronousLock.cjs +1 -1
- package/lib/locks/SyncronousLock.js.map +1 -0
- package/lib/locks/index.cjs +1 -1
- package/lib/locks/index.js.map +1 -0
- package/lib/types.cjs +1 -1
- package/lib/types.js.map +1 -0
- package/lib/utils.cjs +1 -1
- package/lib/utils.js.map +1 -0
- package/package.json +28 -53
- package/dist/transactional-decorators.esm.cjs +0 -632
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2025 Tiago Venceslau
|
|
3
|
+
Copyright (c) 2025 Tiago Venceslau and Contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
22
|
+
|
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-

|
|
2
2
|
|
|
3
3
|
## Transactional Decorators
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
A comprehensive TypeScript library providing transaction management capabilities through decorators, locks, and utilities. This library enables atomic operations, concurrency control, and error handling in your TypeScript applications, ensuring data integrity and thread safety.
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|

|
|
@@ -20,12 +19,6 @@ Simple locking and transaction systems
|
|
|
20
19
|

|
|
21
20
|

|
|
22
21
|
|
|
23
|
-

|
|
24
|
-

|
|
25
|
-

|
|
26
|
-

|
|
27
|
-
|
|
28
|
-
|
|
29
22
|

|
|
30
23
|

|
|
31
24
|

|
|
@@ -37,19 +30,251 @@ Documentation available [here](https://decaf-ts.github.io/transactional-decorato
|
|
|
37
30
|
|
|
38
31
|
### Description
|
|
39
32
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
-
|
|
45
|
-
|
|
33
|
+
The Transactional Decorators library is a standalone module that provides a robust implementation for handling concurrency and transaction management in TypeScript applications. It offers a comprehensive set of tools for ensuring data integrity and thread safety in your code.
|
|
34
|
+
|
|
35
|
+
#### Core Components
|
|
36
|
+
|
|
37
|
+
- **Transaction Class**: The central class that manages the lifecycle of transactions, including creation, execution, and cleanup. It provides mechanisms for binding transactions to objects and methods, ensuring proper transaction context propagation.
|
|
38
|
+
|
|
39
|
+
- **Lock System**: A flexible locking mechanism for controlling access to shared resources:
|
|
40
|
+
- `Lock`: A base class providing fundamental locking capabilities with support for queuing and executing functions when the lock is available.
|
|
41
|
+
- `TransactionLock`: An interface defining the contract for transaction lock implementations that manage transaction execution order and concurrency.
|
|
42
|
+
- `SyncronousLock`: A default implementation of TransactionLock that processes transactions one at a time in the order they are submitted.
|
|
43
|
+
|
|
44
|
+
- **Decorators**:
|
|
45
|
+
- `@transactional()`: A method decorator that enables transactional behavior by wrapping the original method in a transaction context that handles transaction creation, binding, and error handling.
|
|
46
|
+
- `transactionalSuperCall()`: A utility function for handling super calls in transactional methods, ensuring transaction continuity through the inheritance chain.
|
|
47
|
+
|
|
48
|
+
#### Key Features
|
|
49
|
+
|
|
50
|
+
- **Simple yet powerful locking**: The library provides a flexible locking system that can be customized to suit your application's needs.
|
|
51
|
+
- **Method decoration with `@transactional()`**: Easily add transactional behavior to your methods with a simple decorator.
|
|
52
|
+
- **Instance proxying**: The Transaction class can bind to objects, creating proxies that maintain transaction context across method calls.
|
|
53
|
+
- **Transaction chaining**: Transactions can be linked together, allowing you to group multiple operations into a single atomic transaction.
|
|
54
|
+
- **Customizable Transaction Lock**: You can implement your own TransactionLock to customize how transactions are processed.
|
|
55
|
+
- **Error handling**: The library includes built-in error handling to ensure transactions are properly released even when errors occur.
|
|
56
|
+
- **Seamless integration with `db-decorators`**: The library works well with the db-decorators package for database operations.
|
|
57
|
+
|
|
58
|
+
This library is ideal for applications that need to ensure data consistency and handle concurrent operations safely, such as database applications, financial systems, or any application where atomic operations are important.
|
|
59
|
+
|
|
46
60
|
|
|
47
61
|
### How to Use
|
|
48
62
|
|
|
49
63
|
- [Initial Setup](./tutorials/For%20Developers.md#_initial-setup_)
|
|
50
64
|
- [Installation](./tutorials/For%20Developers.md#installation)
|
|
51
65
|
|
|
52
|
-
|
|
66
|
+
#### Using the @transactional Decorator
|
|
67
|
+
|
|
68
|
+
The `@transactional` decorator is the simplest way to add transactional behavior to your methods.
|
|
69
|
+
|
|
70
|
+
**Description**: Add transactional behavior to a class method, ensuring that the method executes within a transaction context.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { transactional } from '@decaf-ts/transactional-decorators';
|
|
74
|
+
|
|
75
|
+
class UserService {
|
|
76
|
+
@transactional()
|
|
77
|
+
async createUser(userData: any): Promise<any> {
|
|
78
|
+
// This method will be executed within a transaction
|
|
79
|
+
// If an error occurs, the transaction will be released with the error
|
|
80
|
+
const user = await this.userRepository.save(userData);
|
|
81
|
+
return user;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@transactional(['custom', 'metadata'])
|
|
85
|
+
async updateUser(userId: string, userData: any): Promise<any> {
|
|
86
|
+
// You can pass custom metadata to the transaction
|
|
87
|
+
const user = await this.userRepository.findById(userId);
|
|
88
|
+
Object.assign(user, userData);
|
|
89
|
+
return await this.userRepository.save(user);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Using the transactional method
|
|
94
|
+
const userService = new UserService();
|
|
95
|
+
const newUser = await userService.createUser({ name: 'John Doe' });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### Using the Transaction Class Directly
|
|
99
|
+
|
|
100
|
+
For more control over the transaction lifecycle, you can use the Transaction class directly.
|
|
101
|
+
|
|
102
|
+
**Description**: Create and manage transactions manually for complex scenarios or when you need fine-grained control.
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { Transaction } from '@decaf-ts/transactional-decorators';
|
|
106
|
+
|
|
107
|
+
// Creating a transaction
|
|
108
|
+
const transaction = new Transaction(
|
|
109
|
+
'UserService', // Source
|
|
110
|
+
'createUser', // Method name
|
|
111
|
+
async () => {
|
|
112
|
+
// Transaction logic here
|
|
113
|
+
const user = await userRepository.save({ name: 'John Doe' });
|
|
114
|
+
return user;
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// Submitting the transaction for execution
|
|
119
|
+
Transaction.submit(transaction);
|
|
120
|
+
|
|
121
|
+
// Using the Transaction.push method for callback-style APIs
|
|
122
|
+
Transaction.push(
|
|
123
|
+
userService, // The object instance
|
|
124
|
+
userService.createUserWithCallback, // The method to call
|
|
125
|
+
{ name: 'John Doe' }, // Arguments
|
|
126
|
+
(err, user) => {
|
|
127
|
+
if (err) {
|
|
128
|
+
console.error('Error creating user:', err);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
console.log('User created:', user);
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### Handling Super Calls in Transactional Methods
|
|
137
|
+
|
|
138
|
+
When extending a class with transactional methods, you can use the `transactionalSuperCall` utility to ensure transaction continuity.
|
|
139
|
+
|
|
140
|
+
**Description**: Maintain transaction context when calling a superclass method that is also transactional.
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { transactional, transactionalSuperCall } from '@decaf-ts/transactional-decorators';
|
|
144
|
+
|
|
145
|
+
class BaseRepository {
|
|
146
|
+
@transactional()
|
|
147
|
+
async save(entity: any): Promise<any> {
|
|
148
|
+
// Base save implementation
|
|
149
|
+
return entity;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class UserRepository extends BaseRepository {
|
|
154
|
+
@transactional()
|
|
155
|
+
async save(user: any): Promise<any> {
|
|
156
|
+
// Pre-processing
|
|
157
|
+
user.updatedAt = new Date();
|
|
158
|
+
|
|
159
|
+
// Call the super method with transaction context
|
|
160
|
+
const result = await transactionalSuperCall(super.save.bind(this), user);
|
|
161
|
+
|
|
162
|
+
// Post-processing
|
|
163
|
+
console.log('User saved:', result);
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### Customizing the Transaction Lock
|
|
170
|
+
|
|
171
|
+
You can implement your own TransactionLock to customize how transactions are processed.
|
|
172
|
+
|
|
173
|
+
**Description**: Create a custom transaction lock implementation for specialized concurrency control.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
import { TransactionLock, Transaction } from '@decaf-ts/transactional-decorators';
|
|
177
|
+
|
|
178
|
+
// Custom transaction lock that logs transactions
|
|
179
|
+
class LoggingTransactionLock implements TransactionLock {
|
|
180
|
+
currentTransaction?: Transaction;
|
|
181
|
+
private pendingTransactions: Transaction[] = [];
|
|
182
|
+
|
|
183
|
+
submit(transaction: Transaction): void {
|
|
184
|
+
console.log(`Submitting transaction: ${transaction.toString()}`);
|
|
185
|
+
|
|
186
|
+
if (this.currentTransaction) {
|
|
187
|
+
this.pendingTransactions.push(transaction);
|
|
188
|
+
console.log(`Transaction queued. Queue length: ${this.pendingTransactions.length}`);
|
|
189
|
+
} else {
|
|
190
|
+
this.currentTransaction = transaction;
|
|
191
|
+
console.log(`Executing transaction immediately`);
|
|
192
|
+
transaction.fire();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async release(err?: Error): Promise<void> {
|
|
197
|
+
if (err) {
|
|
198
|
+
console.error(`Transaction error: ${err.message}`);
|
|
199
|
+
} else {
|
|
200
|
+
console.log(`Transaction completed successfully`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this.currentTransaction = undefined;
|
|
204
|
+
|
|
205
|
+
if (this.pendingTransactions.length > 0) {
|
|
206
|
+
const nextTransaction = this.pendingTransactions.shift()!;
|
|
207
|
+
console.log(`Processing next transaction: ${nextTransaction.toString()}`);
|
|
208
|
+
this.currentTransaction = nextTransaction;
|
|
209
|
+
nextTransaction.fire();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return Promise.resolve();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Set the custom lock as the default
|
|
217
|
+
Transaction.setLock(new LoggingTransactionLock());
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
#### Using the Lock Class
|
|
221
|
+
|
|
222
|
+
The Lock class provides a basic locking mechanism that you can use independently of the transaction system.
|
|
223
|
+
|
|
224
|
+
**Description**: Use the Lock class for simple concurrency control in non-transactional contexts.
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { Lock } from '@decaf-ts/transactional-decorators';
|
|
228
|
+
|
|
229
|
+
// Create a lock for a shared resource
|
|
230
|
+
const resourceLock = new Lock();
|
|
231
|
+
|
|
232
|
+
// Execute a function with exclusive access to the resource
|
|
233
|
+
async function accessSharedResource() {
|
|
234
|
+
const result = await resourceLock.execute(async () => {
|
|
235
|
+
// This code will run with exclusive access to the resource
|
|
236
|
+
const data = await fetchDataFromDatabase();
|
|
237
|
+
const processedData = processData(data);
|
|
238
|
+
await saveDataToDatabase(processedData);
|
|
239
|
+
return processedData;
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Alternatively, you can manually acquire and release the lock
|
|
246
|
+
async function manualLockHandling() {
|
|
247
|
+
await resourceLock.acquire();
|
|
248
|
+
try {
|
|
249
|
+
// Critical section with exclusive access
|
|
250
|
+
const data = await fetchDataFromDatabase();
|
|
251
|
+
const processedData = processData(data);
|
|
252
|
+
await saveDataToDatabase(processedData);
|
|
253
|
+
return processedData;
|
|
254
|
+
} finally {
|
|
255
|
+
// Always release the lock, even if an error occurs
|
|
256
|
+
resourceLock.release();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
## Coding Principles
|
|
263
|
+
|
|
264
|
+
- group similar functionality in folders (analog to namespaces but without any namespace declaration)
|
|
265
|
+
- one class per file;
|
|
266
|
+
- one interface per file (unless interface is just used as a type);
|
|
267
|
+
- group types as other interfaces in a types.ts file per folder;
|
|
268
|
+
- group constants or enums in a constants.ts file per folder;
|
|
269
|
+
- group decorators in a decorators.ts file per folder;
|
|
270
|
+
- always import from the specific file, never from a folder or index file (exceptions for dependencies on other packages);
|
|
271
|
+
- prefer the usage of established design patters where applicable:
|
|
272
|
+
- Singleton (can be an anti-pattern. use with care);
|
|
273
|
+
- factory;
|
|
274
|
+
- observer;
|
|
275
|
+
- strategy;
|
|
276
|
+
- builder;
|
|
277
|
+
- etc;
|
|
53
278
|
|
|
54
279
|
|
|
55
280
|
### Related
|
|
@@ -94,4 +319,4 @@ So if you can, if this project in any way. either by learning something or simpl
|
|
|
94
319
|
|
|
95
320
|
This project is released under the [MIT License](./LICENSE.md).
|
|
96
321
|
|
|
97
|
-
By developers, for developers...
|
|
322
|
+
By developers, for developers...
|