Ghost/core/server/models/plugins/transaction-events.js
Katharina Irrgang fb79f24316
Fixed model events and transactions (#9524)
no issue

- if multiple queries run in a transaction, the model events are triggered before the txn finished
- if the txn rolls back, the events are anyway emitted
- the events are triggered too early
- solution:
  - `emitChange` needs to detect that a transaction is happening
  - it listens on a txn event to determine if events should be triggered
2018-04-06 18:19:45 +02:00

29 lines
714 B
JavaScript

'use strict';
/**
* This is a feature request in knex for 1.0.
* https://github.com/tgriesser/knex/issues/1641
*/
module.exports = function (bookshelf) {
const orig1 = bookshelf.transaction;
bookshelf.transaction = function (cb) {
return orig1.bind(bookshelf)(function (t) {
const orig2 = t.commit;
const orig3 = t.rollback;
t.commit = function () {
t.emit('committed', true);
return orig2.apply(t, arguments);
};
t.rollback = function () {
t.emit('committed', false);
return orig3.apply(t, arguments);
};
return cb(t);
});
};
};