[英]track changes in table nodejs
如何使用節點 js 跟蹤表中的更改? 對於連接,我使用 Sequelize。 任務是觀察用戶數據的變化,並在發生變化時將事件發送到前端。 我使用套接字 io,並且不會使用超時。 有沒有辦法跟蹤像'table field xxx where user_id = yyy was changed'之類的事件並獲得新的字段值。
在續集中嘗試鈎子。 在您的 conf/connection.js 中,您可以定義全局鈎子。 這樣你就可以攔截一切。
鈎子定義:
const hooks = {
beforeBulkUpdate: (options) => {
options.individualHooks = true;
return;
},
beforeBulkDestroy: (options) => {
options.individualHooks = true;
return;
},
beforeCreate: (instance, options) => {
sails.log.info(("%s %s"), 'beforeCreate', instance.$modelOptions.tableName);
return;
},
beforeUpdate: (instance, options) => {
sails.log.info(("%s %s"), 'beforeUpdate', instance.$modelOptions.tableName);
var payload = {
before: instance._previousDataValues,
after: instance.dataValues,
changed: instance._changed
};
----> here you can write on the web socket
return;
},
beforeDestroy: (instance, options) => {
sails.log.info(("%s %s"), 'beforeDestroy', instance.$modelOptions.tableName);
return;
}
}
連接:
mydb: {
user: 'myuser',
password: 'mypassword',
database: 'mydatabase',
options: {
dialect: 'mysql',
host: 'myhost',
logging: true,
define: {
hooks: hooks
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.