Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore hooks?

Tags:

sequelize.js

I have following model:

sequelize.define("User", {
    id: {
        type: DataTypes.INTEGER(11),
        autoIncrement: true,
        primaryKey: true
    },
    email: {
        type: DataTypes.STRING(255),
        allowNull: false,
        unique: true,
    },
    password: {
        type: DataTypes.STRING(60),
        allowNull: false
    },
    role: {
        type: DataTypes.STRING(32),
        allowNull: false,
        defaultValue: 'user',
    }
},{

    instanceMethods: {
        verifyPassword: function(password,cb) {
            crypt.compare(password,this.password,cb);
        },
    },

    hooks: {
        beforeUpdate: hashPassword,
        beforeCreate: hashPassword,
    }

});

I would like to create it like this while ignoring beforeUpdate/Create hooks:

User.create({ email: '[email protected]', role: 'admin', password: '############' },['email','password','role']).done(function(err,admin){ ... })

How?

like image 794
user606521 Avatar asked Dec 20 '13 15:12

user606521


1 Answers

You can do something like this:

return User.findOrCreate({
    where: {email: "[email protected]"},
    defaults: {
        email: "[email protected]",
        role: "admin",
        password: "pyxBOZUNYNZCegyhthKCR9hWpgYO"
    },
    hooks: false //this will ignore hooks
})
like image 189
Edudjr Avatar answered Oct 24 '22 07:10

Edudjr