2016-03-09 20:46:15 +00:00
|
|
|
/**
|
|
|
|
* @param {number} transactionId
|
|
|
|
* @param {UserCredentials} userCredentials
|
|
|
|
* @param {EventStoreNodeConnection} connection
|
|
|
|
* @constructor
|
|
|
|
* @property {number} transactionId
|
|
|
|
*/
|
|
|
|
function EventStoreTransaction(transactionId, userCredentials, connection) {
|
|
|
|
this._transactionId = transactionId;
|
|
|
|
this._userCredentials = userCredentials;
|
|
|
|
this._connection = connection;
|
|
|
|
|
|
|
|
this._isCommitted = false;
|
|
|
|
this._isRolledBack = false;
|
2016-03-17 06:18:48 +00:00
|
|
|
|
|
|
|
Object.defineProperties(this, {
|
|
|
|
transactionId: {
|
|
|
|
enumerable: true, get: function() { return this._transactionId; }
|
|
|
|
}
|
|
|
|
});
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit (async)
|
|
|
|
* @returns {Promise.<WriteResult>}
|
|
|
|
*/
|
|
|
|
EventStoreTransaction.prototype.commit = function() {
|
|
|
|
if (this._isRolledBack) throw new Error("Can't commit a rolledback transaction.");
|
|
|
|
if (this._isCommitted) throw new Error("Transaction is already committed.");
|
|
|
|
this._isCommitted = true;
|
|
|
|
return this._connection.commitTransaction(this, this._userCredentials);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write events (async)
|
2016-03-18 21:04:07 +00:00
|
|
|
* @param {EventData|EventData[]} eventOrEvents
|
2016-03-09 20:46:15 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2016-03-18 21:04:07 +00:00
|
|
|
EventStoreTransaction.prototype.write = function(eventOrEvents) {
|
2016-03-09 20:46:15 +00:00
|
|
|
if (this._isRolledBack) throw new Error("can't write to a rolledback transaction");
|
|
|
|
if (this._isCommitted) throw new Error("Transaction is already committed");
|
2016-03-18 21:04:07 +00:00
|
|
|
var events = Array.isArray(eventOrEvents) ? eventOrEvents : [eventOrEvents];
|
2016-03-09 20:46:15 +00:00
|
|
|
return this._connection.transactionalWrite(this, events);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback
|
|
|
|
*/
|
|
|
|
EventStoreTransaction.prototype.rollback = function() {
|
|
|
|
if (this._isCommitted) throw new Error("Transaction is already committed");
|
|
|
|
this._isRolledBack = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = EventStoreTransaction;
|