egg icon indicating copy to clipboard operation
egg copied to clipboard

egg sequelize:hooks里面的beforeBulkCreate不起作用

Open oneWalker opened this issue 7 years ago • 6 comments

What happens?

egg sequelize里面提供了一个维护时间的,就是timestamps,在不注释掉的情况下,我们能够在hooks里面挂载更改时间,从而实现时间维护。 例如这样: { hooks:{ beforeValidate:(User)=> { User.updatedAt=Date.now(); }, beforeCreate:(User)=> { User.createdAt=Date.now(); } } }

但是当我使用beforeBulkCreate()这个hooks的功能的时候不起作用了,如下 { hooks:{ beforeBulkCreate:(User)=> { User.createdAt=Date.now(); } } } 维护的时间还是timestamps的

相关环境信息

  • 操作系统:window10
  • Node 版本:v12.10.0
  • Egg 版本:2.23.0

oneWalker avatar Sep 18 '19 14:09 oneWalker

beforeBulkCreate的第一个参数是个数组

    hooks: {
      beforeBulkCreate: async (users, options) => {
        for (const user of users) {
          user.createdAt = Date.now()
        }
      },
    },

supperchong avatar Sep 19 '19 10:09 supperchong

beforeBulkCreate的第一个参数是个数组

    hooks: {
      beforeBulkCreate: async (users, options) => {
        for (const user of users) {
          user.createdAt = Date.now()
        }
      },
    },

所有带bulk的方法是不是第一个都必须是数组

oneWalker avatar Sep 20 '19 06:09 oneWalker

可以查看d.ts文件hooks, 带[]标识是数组,如果用的vscode的话,可以在model文件里添加JSdoc注释,这样就可以有vscode智能提示,查看model.define的参数类型了

'use strict'
/**
 * @param {Egg.Application} app
 */
module.exports = app => {

supperchong avatar Sep 20 '19 06:09 supperchong

`'use strict'; /** @type Egg.EggPlugin */ module.exports = app => { // the use of your model const { INTEGER,BIGINT,DATE, STRING } = app.Sequelize;

const User = app.model.define('users',{
    id:{
        type:INTEGER, 
        primaryKey:true, 
        autoIncrement:true
    },
    username:{
        type:STRING(30),
    },
    password:{
        type:STRING(100),
    },
    status:{
        type:INTEGER(4),
    },
    name:{
        type:STRING, 
    },
    gender:STRING(4),
    phone:{
        type:INTEGER(18),
    },
    email:STRING(64),
    lasttime:BIGINT(13),
    createdBy:{
        type:INTEGER(11)
    },
    createdAt:{
        type:BIGINT(13)
    },
    updatedAt:{
        type:BIGINT(13),
    },
    updatedBy:{
        type:BIGINT(13),
    },
    other:STRING(128),
    thirdId:INTEGER(24)
},{
    hooks:{
        beforeValidate:(User)=>
        {
            User.updatedAt=Date.now();
        },
        beforeCreate:(User)=>
        {
            User.createdAt=Date.now();
        }
    }
}
);


User.associate = function(){
    User.belongsToMany(app.model.Role, {
        through: {
            model:app.model.Userrole,
            as:'userRole'
        },
        constraints: false,
    });
    User.belongsToMany(app.model.Area, {
        through: {
            model:app.model.Range,
            as:'userArea',
        },
        constraints: false,
    });
    User.belongsToMany(app.model.Department, {
        through:
        {
            model: app.model.Userdept,
            as:'userDept'
        },
        constraints: false,
        
    });
}
return User;

}; ` 比如我的定义是这个样子的怎么也不可能是数组啊!应该是一个但对象,怎么转换成数组呢?

oneWalker avatar Sep 20 '19 15:09 oneWalker

 beforeBulkCreate(instances: M[], options: BulkCreateOptions): HookReturn;
 afterBulkCreate(instances: M[], options: BulkCreateOptions): HookReturn;
 afterFind(instancesOrInstance: M[] | M | null, options: FindOptions): HookReturn;

beforeBulkCreate和afterBulkCreate第一个参数是数组,afterFind第一个参数可能是数组,其它的比如beforeValidate,beforeCreate都是一个对象

supperchong avatar Sep 21 '19 02:09 supperchong

明白,也就是beforeBulkCreate和afterBulkCreate不能由hooks挂载到定义的对象.............,那这个函数基本等于没什么用................

oneWalker avatar Sep 21 '19 02:09 oneWalker