/* jshint indent: 2 */ let MD5 = require('crypto').createHash('md5'); module.exports = function (sequelize, DataTypes) { return sequelize.define('Account', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true }, name: { type: DataTypes.STRING(255), allowNull: true, // 從數據庫查詢到數據,經過以下處理后給用戶 get() { return "dear " + this.getDataValue('name'); } }, age: { type: DataTypes.INTEGER(11), allowNull: true, // 數據校驗返回異常 customFunc自定義的校驗 validate: { max: { args: 100, msg: "age is larger" }, min: { args: 1, msg: 'age is small' }, customFunc(val) { if (val === 50) { console.log('dddd'); throw new Error('Only even values are allowed!') } } } }, passwd: { type: DataTypes.STRING(255), allowNull: true, // 當插入或者修改時,經過以下處理后再寫入數據庫 set(val) { val = MD5.update(val).digest('hex'); this.setDataValue("passwd", val); } } }, { tableName: 'account', // setterMethods,getterMethods這個是相當與在存取時都添加了changeName這個虛擬字段 setterMethods: { changeName(val) { return this.setDataValue('name', val.slice(0, -1)); } }, getterMethods: { changeName() { return this.name + 'changeName'; } } }); };