How to create a TRIGGER in SEQUELIZE (nodeJS)?

ElTête

I'm trying to create a trigger using sequelize.. the main idea is to create an instance of CONFIG after creating a USER.

// 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 would I do it?

Evan Siroky

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

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 on an afterCreate event:

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;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Create a table in Sequelize to store in Postgressql with NodeJS

How to create mysql database with sequelize (nodejs)

How to create a delete method in nodejs(express) with sequelize(sqlite) as database

How to create new data with relation in nodejs using sequelize

how to create one to many relational record using nodejs and sequelize-typescript

How to create prepared statements in Sequelize?

How to create assocations in Sequelize migrations?

How to create an object of a relationship in sequelize?

How to create a Trigger in PostgreSql?

How to create "trigger" in MongoDB

How to create a trigger for this situation?

How to trigger profiling in NodeJS at runtime?

Nodejs Sequelize

how to update Database using hooks/sequelize in nodejs

How to use 'select if' function with sequelize in nodejs

How to prevent sql-injection in nodejs and sequelize?

how to set model validation with sequelize in nodejs?

How to adding default value to Sequelize model with NodeJS?

Unable to create a many to many relationship using sequelize for mysql in nodejs

How to create join table with foreign keys with sequelize or sequelize-cli

How can I trigger the beforeCreate hook when bulkCreating in Sequelize?

How can create trigger in phpmyadmin

How to properly create a trigger in workbench?

How to create a trigger in a hospital database?

How to use Sequelize create with nested objects and relations

Sequelize how to properly create a User Data with constraints?

Sequelize CLI how to create migrations from models?

How to create immutable sequelize model properties?

How to create a foreign key on the same table with Sequelize?