sails-mysql-transaction
is a Sails ORM Adapter for MySQL with transaction support.
This adapter essentially wraps around the popular sails-mysql
adapter and provides additional API to perform
operations that ties around a database transaction.
-
Add
sails-mysql-transactions
to your application’spackage.json
. Do not run install directly ifsails
is not already installed in your package. -
If you already have
sails-mysql
installed, it might interfere with operations of this module. Remove it from yourpackage.json
and uninstall the same usingnpm remove sails-mysql
. -
This package installs successfully only when sails is already installed in the package. If the package is already installed, then simply run
npm install sails-mysql-transactions --save
, otherwise runnpm install
and it will take care of rest.
This package overwrites the waterline
module inside Sails with a fork of Waterline maintained by Postman. As such,
if you ever re-install or update sails, ensure you re-install this adapter right after it.
Do check SailsJS compatibility list before upgrading your Sails version while already using this adapter.
The integration test Sails App located in tests/integration/app
directory of this repository has a fully functional
installation. Simply run npm install
within test/integration/app
directory.
module.exports = {
/* your other config stay as is */
connections: {
mySQLT: {
adapter: 'sails-mysql-transactions',
host: '{{your-db-host}}',
user: '{{your-db-username}}',
password: '{{your-db-password}}',
database: '{{your-db-tablename}}'
}
},
models: {
connection: 'mySQLT',
migrate: 'safe'
}
}
module.exports = {
schema: true,
autosubscribe: false,
attributes: {
property_one: {
type: 'string'
},
property_two: {
type: 'boolean',
defaultsTo: false
},
transactionId: {
type: 'string'
}
}
};
var Transaction = require('sails-mysql-transactions').Transaction;
module.exports = {
create: function (req, res) {
// start a new transaction
Transaction.start(function (err, transaction) {
if (err) {
// the first error might even fail to return a transaction object, so double-check.
transaction && transaction.rollback();
return res.serverError(err);
}
OneModel.transact(transaction).create(req.params.all(), function (err, modelInstance) {
if (err) {
transaction.rollback();
return res.serverError(err);
}
transaction.commit();
return res.json(modelInstance);
});
});
}
};
route = function (req, res) {
Transaction.start(function (err, transaction) {
OneModel.transact(transaction).create(/* ... */);
OneModel.transact(transaction).update(/* ... */);
OneModel.transact(transaction).find(/* ... */);
OneModel.transact(transaction).findOrCreate(/* ... */);
OneModel.transact(transaction).findOne(/* ... */);
OneModel.transact(transaction).destroy(/* ... */);
});
};
Other than those, update
, save
and association operations on instance methods work within transaction provided they
were either stemmed from the same transaction or wrapped (transaction.wrap(isntance)
) by a transaction.