user3631341
user3631341

Reputation: 525

sequelize beforeCreate hook not called

Im defining my beforeCreate hook using direct method (Method 3 in sequelize docs) like this:

module.exports = (sequelize, datatypes)=>{
    var tag = sequelize.define(...)

    tag.beforeCreate(tag,options=>{...})

    tag.beforeBulkCreate(tags,options=>{...})
}

beforeBulkCreate is being fired when I do tag.bulkCreate but beforeCreate is not when I do tag.create. Why is that?

Upvotes: 0

Views: 2393

Answers (2)

RullDawg
RullDawg

Reputation: 221

I felt it was important to default individualHooks to be true when defining my model as opposed to assuming all callers of bulkCreate would know to pass {individualHooks: true}. The first response is correct, but I think it would be better to control how hooks are used within the Model definition and not make the caller responsible.

This is what I did in my model definition.

var baseBulkCreate = User.bulkCreate;
User.bulkCreate = function(instances, options) {
    options = options || {};

    // We want to make sure that when we do things in bulk we always run the individual hooks too.  It isn't clear
    // why this isn't the default.
    options.individualHooks = true;

    return baseBulkCreate.call(this, instances, options);
};

Upvotes: 0

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28788

You should use individualHooks: true if you want to run create hooks for each row http://docs.sequelizejs.com/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance

Upvotes: 3

Related Questions