mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-31 12:30:14 +08:00
Add transaction logging
This commit is contained in:
parent
83dfb664e1
commit
8ed94442c4
3 changed files with 55 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
const Sequelize = require('sequelize');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const TransactionLog = require('./transaction-log')
|
||||
|
||||
const STORAGE_DIR = path.join(__base, 'storage');
|
||||
if (!fs.existsSync(STORAGE_DIR)) {
|
||||
|
@ -69,6 +70,9 @@ class DatabaseConnectionFactory {
|
|||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
|
||||
const transactionLog = new TransactionLog(db);
|
||||
transactionLog.setupSQLHooks(sequelize)
|
||||
|
||||
return sequelize.authenticate().then(() =>
|
||||
sequelize.sync()
|
||||
).thenReturn(db);
|
||||
|
|
18
core/models/account/transaction.js
Normal file
18
core/models/account/transaction.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
module.exports = (sequelize, Sequelize) => {
|
||||
const Transaction = sequelize.define('Transaction', {
|
||||
type: Sequelize.STRING,
|
||||
objectId: Sequelize.STRING,
|
||||
modelName: Sequelize.STRING,
|
||||
changedFields: {
|
||||
type: Sequelize.STRING,
|
||||
get: function get() {
|
||||
return JSON.parse(this.getDataValue('changedFields'))
|
||||
},
|
||||
set: function set(val) {
|
||||
this.setDataValue('changedFields', JSON.stringify(val));
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return Transaction;
|
||||
};
|
33
core/transaction-log.js
Normal file
33
core/transaction-log.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
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;
|
||||
this.db.Transaction.create(Object.assign({type: type},
|
||||
this.parseHookData(sequelizeHookData)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
setupSQLHooks(sequelize) {
|
||||
sequelize.addHook("afterCreate", this.transactionLogger("create"))
|
||||
sequelize.addHook("afterUpdate", this.transactionLogger("update"))
|
||||
sequelize.addHook("afterDelete", this.transactionLogger("delete"))
|
||||
}
|
||||
}
|
||||
module.exports = TransactionLog
|
Loading…
Reference in a new issue