Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}, options.timeout);
}
cb(err, transaction);
});
} else {
process.nextTick(function() {
const err = new Error(g.f('{{Transaction}} is not supported'));
cb(err);
});
}
return cb.promise;
};
// Promisify the transaction apis
if (Transaction) {
jutil.mixin(Transaction.prototype, ObserverMixin);
/**
* Commit a transaction and release it back to the pool.
*
* Example:
*
* ```js
* MyModel.beginTransaction('READ COMMITTED', function(err, tx) {
* // some crud operation of your choice
* tx.commit(function(err) {
* // release the connection pool upon committing
* tx.close(err);
* });
* });
* ```
*
* @callback {Function} cb Callback function.
* Example:
*
* ```js
* MyModel.beginTransaction('READ COMMITTED', function(err, tx) {
* // some crud operation of your choice
* tx.commit(function(err) {
* // release the connection pool upon committing
* tx.close(err);
* });
* });
* ```
*
* @callback {Function} cb Callback function.
* @returns {Promise|undefined} Returns a callback promise.
*/
Transaction.prototype.commit = function(cb) {
cb = cb || utils.createPromiseCallback();
if (this.ensureActive(cb)) {
const context = {
transaction: this,
operation: 'commit',
};
this.notifyObserversAround('commit', context,
done => {
this.connector.commit(this.connection, done);
},
err => {
// Deference the connection to mark the transaction is not active
// The connection should have been released back the pool
this.connection = null;
cb(err);
});
});
}
return cb.promise;
};
Transaction.prototype.ensureActive = function(cb) {
// Report an error if the transaction is not active
if (!this.connection) {
process.nextTick(() => {
cb(new Error(g.f('The {{transaction}} is not active: %s', this.id)));
});
}
return !!this.connection;
};
Transaction.prototype.toJSON = function() {
return this.id;
};
Transaction.prototype.toString = function() {
return this.id;
};
}
TransactionMixin.Transaction = Transaction;
Transaction.prototype.ensureActive = function(cb) {
// Report an error if the transaction is not active
if (!this.connection) {
process.nextTick(() => {
cb(new Error(g.f('The {{transaction}} is not active: %s', this.id)));
});
}
return !!this.connection;
};
Transaction.prototype.toJSON = function() {
return this.id;
};
Transaction.prototype.toString = function() {
return this.id;
};
}
TransactionMixin.Transaction = Transaction;
* Example:
*
* ```js
* MyModel.beginTransaction('READ COMMITTED', function(err, tx) {
* // some crud operation of your choice
* tx.rollback(function(err) {
* // release the connection pool upon committing
* tx.close(err);
* });
* });
* ```
*
* @callback {Function} cb Callback function.
* @returns {Promise|undefined} Returns a callback promise.
*/
Transaction.prototype.rollback = function(cb) {
cb = cb || utils.createPromiseCallback();
if (this.ensureActive(cb)) {
const context = {
transaction: this,
operation: 'rollback',
};
this.notifyObserversAround('rollback', context,
done => {
this.connector.rollback(this.connection, done);
},
err => {
// Deference the connection to mark the transaction is not active
// The connection should have been released back the pool
this.connection = null;
cb(err);
});
};
this.notifyObserversAround('rollback', context,
done => {
this.connector.rollback(this.connection, done);
},
err => {
// Deference the connection to mark the transaction is not active
// The connection should have been released back the pool
this.connection = null;
cb(err);
});
}
return cb.promise;
};
Transaction.prototype.ensureActive = function(cb) {
// Report an error if the transaction is not active
if (!this.connection) {
process.nextTick(() => {
cb(new Error(g.f('The {{transaction}} is not active: %s', this.id)));
});
}
return !!this.connection;
};
Transaction.prototype.toJSON = function() {
return this.id;
};
Transaction.prototype.toString = function() {
return this.id;
};