Mailspring/core/transaction-log.js

38 lines
1 KiB
JavaScript
Raw Normal View History

2016-06-21 08:28:26 +08:00
const DeltaStreamQueue = require('./delta-stream-queue')
2016-06-21 07:03:14 +08:00
class TransactionLog {
constructor(db) {
this.db = db;
}
parseHookData({dataValues, _changed, $modelOptions}) {
return {
objectId: dataValues.id,
modelName: $modelOptions.name.singular,
changedFields: _changed,
}
}
isTransaction({$modelOptions}) {
return $modelOptions.name.singular === "Transaction"
}
transactionLogger(type) {
return (sequelizeHookData) => {
if (this.isTransaction(sequelizeHookData)) return;
2016-06-21 08:28:26 +08:00
const transactionData = Object.assign({type: type},
2016-06-21 07:03:14 +08:00
this.parseHookData(sequelizeHookData)
2016-06-21 08:28:26 +08:00
);
this.db.Transaction.create(transactionData);
DeltaStreamQueue.notify(this.db.accountId, transactionData)
2016-06-21 07:03:14 +08:00
}
}
setupSQLHooks(sequelize) {
sequelize.addHook("afterCreate", this.transactionLogger("create"))
sequelize.addHook("afterUpdate", this.transactionLogger("update"))
sequelize.addHook("afterDelete", this.transactionLogger("delete"))
}
}
module.exports = TransactionLog