如何在SEQUELIZE(nodeJS)中创建TRIGGER?

时间:2022-09-16 09:07:36

Im trying to create a trigger using sequelize.. the main idea is to create an instance of CONFIG after create a USER.

我试图使用sequelize创建一个触发器..主要的想法是在创建一个USER后创建一个CONFIG实例。

//USER MODEL
module.exports = function(sequelize, DataTypes) {    
    var User = sequelize.define('User', {
        name        : DataTypes.STRING(255),
        email       : DataTypes.STRING(255),
        username    : DataTypes.STRING(45),
        password    : DataTypes.STRING(100),
    }, {
        classMethods : {
            associate : function(models) {
                User.hasOne(models.Config)
            }
        }
    });    
    return User;
};

//CONFIG MODEL
module.exports = function(sequelize, DataTypes) {
    var Config = sequelize.define('Config', {
        notifications   : DataTypes.INTEGER
    }, {
        classMethods : {
            associate : function(models) {
                Config.belongsTo(models.User)
            }
        }
    });

    return Config;
};

As you can see, a "user" has one "config" and a "config" belongs to a "user", so after a user is created I want to create his config row automatically.

如您所见,“用户”有一个“配置”,“配置”属于“用户”,所以在创建用户后我想自动创建他的配置行。

The goal is to do:

目标是:

DELIMITER //
CREATE TRIGGER create_config AFTER INSERT ON user
  FOR EACH ROW
BEGIN
    insert into config    (user_id)     values(new.user_id);
END; //
DELIMITER ;

Now, what I do to simulate that is the following:

现在,我要做的是模拟以下内容:

.then(function(user){
   return dao.Config.create(req.body, user, t);
})

Once a User is created I create his configuration like that... it works but is not what I'm searching.

一旦创建了用户,我就会创建他的配置...它可以工作,但不是我正在搜索的。

How I'd do it?

我怎么做的?

Thank you!

谢谢!

1 个解决方案

#1


19  

You can do this one of two ways. As you note, you could create a trigger in the database itself. You could run a raw sequelize query to accomplish this:

你可以用两种方法之一做到这一点。如您所知,您可以在数据库本身中创建触发器。你可以运行一个原始的sequelize查询来完成这个:

sequelize.query('CREATE TRIGGER create_config AFTER INSERT ON users' +
  ' FOR EACH ROW' +
  ' BEGIN' +
  ' insert into configs (UserId) values(new.id);' +
  'END;')

Or, you could create a hook on the user model that performs an action after an create event occurs:

或者,您可以在创建事件发生后执行操作的用户模型上创建一个钩子:

module.exports = function(sequelize, DataTypes) {    
  var User = sequelize.define('User', {
    name        : DataTypes.STRING(255),
    email       : DataTypes.STRING(255),
    username    : DataTypes.STRING(45),
    password    : DataTypes.STRING(100),
  }, {
    classMethods : {
      associate : function(models) {
        User.hasOne(models.Config)
      }
    },
    hooks: {
      afterCreate: function(user, options) {
        models.Config.create({
          UserId: user.id
        })
      }
    }
  });    
  return User;
};

#1


19  

You can do this one of two ways. As you note, you could create a trigger in the database itself. You could run a raw sequelize query to accomplish this:

你可以用两种方法之一做到这一点。如您所知,您可以在数据库本身中创建触发器。你可以运行一个原始的sequelize查询来完成这个:

sequelize.query('CREATE TRIGGER create_config AFTER INSERT ON users' +
  ' FOR EACH ROW' +
  ' BEGIN' +
  ' insert into configs (UserId) values(new.id);' +
  'END;')

Or, you could create a hook on the user model that performs an action after an create event occurs:

或者,您可以在创建事件发生后执行操作的用户模型上创建一个钩子:

module.exports = function(sequelize, DataTypes) {    
  var User = sequelize.define('User', {
    name        : DataTypes.STRING(255),
    email       : DataTypes.STRING(255),
    username    : DataTypes.STRING(45),
    password    : DataTypes.STRING(100),
  }, {
    classMethods : {
      associate : function(models) {
        User.hasOne(models.Config)
      }
    },
    hooks: {
      afterCreate: function(user, options) {
        models.Config.create({
          UserId: user.id
        })
      }
    }
  });    
  return User;
};