繁体   English   中英

跟踪表 nodejs 中的更改

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM