@decaf-ts/transactional-decorators 0.1.2 → 0.1.4
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 +17 -152
- package/README.md +242 -17
- package/dist/transactional-decorators.cjs +2 -544
- 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 +87 -39
- package/lib/Transaction.d.ts +86 -38
- package/lib/Transaction.js.map +1 -0
- package/lib/constants.cjs +14 -1
- package/lib/constants.d.ts +13 -0
- package/lib/constants.js.map +1 -0
- package/lib/decorators.cjs +35 -11
- package/lib/decorators.d.ts +34 -10
- package/lib/decorators.js.map +1 -0
- package/lib/esm/Transaction.d.ts +86 -38
- package/lib/esm/Transaction.js +90 -42
- package/lib/esm/Transaction.js.map +1 -0
- package/lib/esm/constants.d.ts +13 -0
- package/lib/esm/constants.js +14 -1
- package/lib/esm/constants.js.map +1 -0
- package/lib/esm/decorators.d.ts +34 -10
- package/lib/esm/decorators.js +37 -13
- package/lib/esm/decorators.js.map +1 -0
- package/lib/esm/index.d.ts +7 -13
- package/lib/esm/index.js +14 -20
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/TransactionLock.d.ts +13 -11
- package/lib/esm/interfaces/TransactionLock.js +1 -1
- package/lib/esm/interfaces/TransactionLock.js.map +1 -0
- package/lib/esm/interfaces/index.d.ts +5 -0
- package/lib/esm/interfaces/index.js +7 -2
- package/lib/esm/interfaces/index.js.map +1 -0
- package/lib/esm/locks/Lock.d.ts +13 -5
- package/lib/esm/locks/Lock.js +14 -6
- package/lib/esm/locks/Lock.js.map +1 -0
- package/lib/esm/locks/SyncronousLock.d.ts +3 -4
- package/lib/esm/locks/SyncronousLock.js +14 -2
- package/lib/esm/locks/SyncronousLock.js.map +1 -0
- package/lib/esm/locks/index.d.ts +5 -0
- package/lib/esm/locks/index.js +8 -3
- package/lib/esm/locks/index.js.map +1 -0
- package/lib/esm/types.d.ts +13 -3
- 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 +8 -14
- package/lib/index.d.ts +7 -13
- package/lib/index.js.map +1 -0
- package/lib/interfaces/TransactionLock.cjs +1 -1
- package/lib/interfaces/TransactionLock.d.ts +13 -11
- package/lib/interfaces/TransactionLock.js.map +1 -0
- package/lib/interfaces/index.cjs +6 -1
- package/lib/interfaces/index.d.ts +5 -0
- package/lib/interfaces/index.js.map +1 -0
- package/lib/locks/Lock.cjs +14 -6
- package/lib/locks/Lock.d.ts +13 -5
- package/lib/locks/Lock.js.map +1 -0
- package/lib/locks/SyncronousLock.cjs +13 -1
- package/lib/locks/SyncronousLock.d.ts +3 -4
- package/lib/locks/SyncronousLock.js.map +1 -0
- package/lib/locks/index.cjs +6 -1
- package/lib/locks/index.d.ts +5 -0
- package/lib/locks/index.js.map +1 -0
- package/lib/types.cjs +1 -1
- package/lib/types.d.ts +13 -3
- package/lib/types.js.map +1 -0
- package/lib/utils.cjs +1 -1
- package/lib/utils.js.map +1 -0
- package/package.json +24 -25
- package/dist/transactional-decorators.esm.cjs +0 -533
package/LICENSE.md
CHANGED
|
@@ -1,157 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) 2025 Tiago Venceslau and Contributors
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
14
22
|
|
|
15
|
-
## 0. Additional Definitions.
|
|
16
|
-
|
|
17
|
-
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
18
|
-
General Public License, and the "GNU GPL" refers to version 3 of the
|
|
19
|
-
GNU General Public License.
|
|
20
|
-
|
|
21
|
-
"The Library" refers to a covered work governed by this License, other
|
|
22
|
-
than an Application or a Combined Work as defined below.
|
|
23
|
-
|
|
24
|
-
An "Application" is any work that makes use of an interface provided
|
|
25
|
-
by the Library, but which is not otherwise based on the Library.
|
|
26
|
-
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
-
of using an interface provided by the Library.
|
|
28
|
-
|
|
29
|
-
A "Combined Work" is a work produced by combining or linking an
|
|
30
|
-
Application with the Library. The particular version of the Library
|
|
31
|
-
with which the Combined Work was made is also called the "Linked
|
|
32
|
-
Version".
|
|
33
|
-
|
|
34
|
-
The "Minimal Corresponding Source" for a Combined Work means the
|
|
35
|
-
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
-
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
-
based on the Application, and not on the Linked Version.
|
|
38
|
-
|
|
39
|
-
The "Corresponding Application Code" for a Combined Work means the
|
|
40
|
-
object code and/or source code for the Application, including any data
|
|
41
|
-
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
-
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
-
|
|
44
|
-
## 1. Exception to Section 3 of the GNU GPL.
|
|
45
|
-
|
|
46
|
-
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
-
without being bound by section 3 of the GNU GPL.
|
|
48
|
-
|
|
49
|
-
## 2. Conveying Modified Versions.
|
|
50
|
-
|
|
51
|
-
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
-
facility refers to a function or data to be supplied by an Application
|
|
53
|
-
that uses the facility (other than as an argument passed when the
|
|
54
|
-
facility is invoked), then you may convey a copy of the modified
|
|
55
|
-
version:
|
|
56
|
-
|
|
57
|
-
- a) under this License, provided that you make a good faith effort
|
|
58
|
-
to ensure that, in the event an Application does not supply the
|
|
59
|
-
function or data, the facility still operates, and performs
|
|
60
|
-
whatever part of its purpose remains meaningful, or
|
|
61
|
-
- b) under the GNU GPL, with none of the additional permissions of
|
|
62
|
-
this License applicable to that copy.
|
|
63
|
-
|
|
64
|
-
## 3. Object Code Incorporating Material from Library Header Files.
|
|
65
|
-
|
|
66
|
-
The object code form of an Application may incorporate material from a
|
|
67
|
-
header file that is part of the Library. You may convey such object
|
|
68
|
-
code under terms of your choice, provided that, if the incorporated
|
|
69
|
-
material is not limited to numerical parameters, data structure
|
|
70
|
-
layouts and accessors, or small macros, inline functions and templates
|
|
71
|
-
(ten or fewer lines in length), you do both of the following:
|
|
72
|
-
|
|
73
|
-
- a) Give prominent notice with each copy of the object code that
|
|
74
|
-
the Library is used in it and that the Library and its use are
|
|
75
|
-
covered by this License.
|
|
76
|
-
- b) Accompany the object code with a copy of the GNU GPL and this
|
|
77
|
-
license document.
|
|
78
|
-
|
|
79
|
-
## 4. Combined Works.
|
|
80
|
-
|
|
81
|
-
You may convey a Combined Work under terms of your choice that, taken
|
|
82
|
-
together, effectively do not restrict modification of the portions of
|
|
83
|
-
the Library contained in the Combined Work and reverse engineering for
|
|
84
|
-
debugging such modifications, if you also do each of the following:
|
|
85
|
-
|
|
86
|
-
- a) Give prominent notice with each copy of the Combined Work that
|
|
87
|
-
the Library is used in it and that the Library and its use are
|
|
88
|
-
covered by this License.
|
|
89
|
-
- b) Accompany the Combined Work with a copy of the GNU GPL and this
|
|
90
|
-
license document.
|
|
91
|
-
- c) For a Combined Work that displays copyright notices during
|
|
92
|
-
execution, include the copyright notice for the Library among
|
|
93
|
-
these notices, as well as a reference directing the user to the
|
|
94
|
-
copies of the GNU GPL and this license document.
|
|
95
|
-
- d) Do one of the following:
|
|
96
|
-
- 0) Convey the Minimal Corresponding Source under the terms of
|
|
97
|
-
this License, and the Corresponding Application Code in a form
|
|
98
|
-
suitable for, and under terms that permit, the user to
|
|
99
|
-
recombine or relink the Application with a modified version of
|
|
100
|
-
the Linked Version to produce a modified Combined Work, in the
|
|
101
|
-
manner specified by section 6 of the GNU GPL for conveying
|
|
102
|
-
Corresponding Source.
|
|
103
|
-
- 1) Use a suitable shared library mechanism for linking with
|
|
104
|
-
the Library. A suitable mechanism is one that (a) uses at run
|
|
105
|
-
time a copy of the Library already present on the user's
|
|
106
|
-
computer system, and (b) will operate properly with a modified
|
|
107
|
-
version of the Library that is interface-compatible with the
|
|
108
|
-
Linked Version.
|
|
109
|
-
- e) Provide Installation Information, but only if you would
|
|
110
|
-
otherwise be required to provide such information under section 6
|
|
111
|
-
of the GNU GPL, and only to the extent that such information is
|
|
112
|
-
necessary to install and execute a modified version of the
|
|
113
|
-
Combined Work produced by recombining or relinking the Application
|
|
114
|
-
with a modified version of the Linked Version. (If you use option
|
|
115
|
-
4d0, the Installation Information must accompany the Minimal
|
|
116
|
-
Corresponding Source and Corresponding Application Code. If you
|
|
117
|
-
use option 4d1, you must provide the Installation Information in
|
|
118
|
-
the manner specified by section 6 of the GNU GPL for conveying
|
|
119
|
-
Corresponding Source.)
|
|
120
|
-
|
|
121
|
-
## 5. Combined Libraries.
|
|
122
|
-
|
|
123
|
-
You may place library facilities that are a work based on the Library
|
|
124
|
-
side by side in a single library together with other library
|
|
125
|
-
facilities that are not Applications and are not covered by this
|
|
126
|
-
License, and convey such a combined library under terms of your
|
|
127
|
-
choice, if you do both of the following:
|
|
128
|
-
|
|
129
|
-
- a) Accompany the combined library with a copy of the same work
|
|
130
|
-
based on the Library, uncombined with any other library
|
|
131
|
-
facilities, conveyed under the terms of this License.
|
|
132
|
-
- b) Give prominent notice with the combined library that part of it
|
|
133
|
-
is a work based on the Library, and explaining where to find the
|
|
134
|
-
accompanying uncombined form of the same work.
|
|
135
|
-
|
|
136
|
-
## 6. Revised Versions of the GNU Lesser General Public License.
|
|
137
|
-
|
|
138
|
-
The Free Software Foundation may publish revised and/or new versions
|
|
139
|
-
of the GNU Lesser General Public License from time to time. Such new
|
|
140
|
-
versions will be similar in spirit to the present version, but may
|
|
141
|
-
differ in detail to address new problems or concerns.
|
|
142
|
-
|
|
143
|
-
Each version is given a distinguishing version number. If the Library
|
|
144
|
-
as you received it specifies that a certain numbered version of the
|
|
145
|
-
GNU Lesser General Public License "or any later version" applies to
|
|
146
|
-
it, you have the option of following the terms and conditions either
|
|
147
|
-
of that published version or of any later version published by the
|
|
148
|
-
Free Software Foundation. If the Library as you received it does not
|
|
149
|
-
specify a version number of the GNU Lesser General Public License, you
|
|
150
|
-
may choose any version of the GNU Lesser General Public License ever
|
|
151
|
-
published by the Free Software Foundation.
|
|
152
|
-
|
|
153
|
-
If the Library as you received it specifies that a proxy can decide
|
|
154
|
-
whether future versions of the GNU Lesser General Public License shall
|
|
155
|
-
apply, that proxy's public statement of acceptance of any version is
|
|
156
|
-
permanent authorization for you to choose that version for the
|
|
157
|
-
Library.
|
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...
|