对于MySQL数据库,我具有以下Employee
模型:
var bcrypt = require('bcrypt');
module.exports = (sequelize, DataTypes) => {
const Employee = sequelize.define(
"Employee",
{
username: DataTypes.STRING,
password: DataTypes.STRING,
}, {}
);
return Employee;
};
通过raw queries读取包含10,000多名员工的.sql
文件来完成数据库的播种:
sequelize.query(mySeedingSqlFileHere);
问题在于SQL文件中的密码是纯文本,我想在插入数据库之前使用bcrypt
对它们进行哈希处理。我以前从未做过大容量插入,所以我一直在研究Sequelize docs for adding a hook to the Employee
model,就像这样:
hooks: {
beforeBulkCreate: (employees, options) => {
for (employee in employees) {
if (employee.password) {
employee.password = await bcrypt.hash(employee.password, 10);
}
}
}
}
这不起作用,因为在重新播种后我仍然获得纯文本值-我是否应该寻找另一种方法?我正在调查sequelize capitalize name before saving in database - instance hook