# new Transaction(source, methodopt, actionopt, metadataopt)
Manages transaction lifecycle, including creation, execution, and cleanup. Provides mechanisms for binding transactions to objects and methods, ensuring proper transaction context propagation.
Core transaction management class
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
source |
string
|
The source/origin of the transaction (typically a class name) |
|
method |
string
|
<optional> |
The method name associated with the transaction |
action |
function
|
<optional> |
The function to execute within the transaction |
metadata |
Array.<any>
|
<optional> |
Additional metadata to associate with the transaction |
Example
// Creating and submitting a transaction
const transaction = new Transaction(
'UserService',
'createUser',
async () => {
// Transaction logic here
await db.insert('users', { name: 'John' });
}
);
Transaction.submit(transaction);
// Using the transactional decorator
class UserService {
Methods
# bindToTransaction(obj) → {any}
Binds a transactional decorated object to the transaction by ensuring all transactional methods automatically receive the current transaction as their first argument
Binds an object to the current transaction context
Parameters:
Name | Type | Description |
---|---|---|
obj |
any
|
The object to bind to the transaction |
The bound object with transaction-aware method wrappers
any
# bindTransaction(nextTransaction) → {void}
Binds a new transaction operation to the current transaction, transferring logs and binding methods to maintain transaction context
Links a new transaction to the current one
Parameters:
Name | Type | Description |
---|---|---|
nextTransaction |
Transaction
|
The new transaction to bind to the current one |
void
# fire() → {any}
Fires the transaction by executing its associated action function, throwing an error if no action is defined
Executes the transaction action
The result of the transaction action
any
# getMetadata() → {Array.<any>|undefined}
Returns a copy of the metadata associated with this transaction, ensuring the original metadata remains unmodified
Retrieves transaction metadata
A copy of the transaction metadata or undefined if no metadata exists
Array.<any>
|
undefined
# toString(withIdopt, withLogopt) → {string}
Overrides the default toString method to provide a formatted string representation of the transaction, optionally including the transaction ID and log
Provides a string representation of the transaction
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
withId |
boolean
|
<optional> |
true | Whether to include the transaction ID in the output |
withLog |
boolean
|
<optional> |
false | Whether to include the transaction log in the output |
A string representation of the transaction
string
# static getLock() → {TransactionLock}
Gets the current transaction lock instance, creating a default SyncronousLock if none exists
Retrieves the current transaction lock
The current transaction lock implementation
# static push(issuer, callbackMethod, args) → {void}
Pushes a transaction to the queue and waits for its resolution. Creates a new transaction with the provided issuer and callback method, then submits it to the transaction lock.
Queues a transaction for execution
Parameters:
Name | Type | Description |
---|---|---|
issuer |
any
|
Any class instance that will be used as 'this' when calling the callbackMethod |
callbackMethod |
function
|
Callback function containing the transaction logic, will be called with the issuer as 'this' |
args |
Array.<any>
|
Arguments to pass to the method. Last one must be the callback function |
void
# async static release(erropt) → {Promise.<void>}
Releases the current transaction lock, optionally with an error, allowing the next transaction to proceed
Releases the transaction lock
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
err |
Error
|
<optional> |
Optional error that occurred during transaction execution |
A promise that resolves when the lock has been released
Promise.<void>
# static setLock(lock) → {void}
Sets the lock implementation to be used for transaction management, allowing customization of the transaction behavior
Configures the transaction lock implementation
Parameters:
Name | Type | Description |
---|---|---|
lock |
TransactionLock
|
The lock implementation to use for managing transactions |
void
# static submit(transaction) → {void}
Submits a transaction to the current transaction lock for processing and execution
Submits a transaction for processing
Parameters:
Name | Type | Description |
---|---|---|
transaction |
Transaction
|
The transaction to submit for processing |
void