added member entity to backend (progress)
This commit is contained in:
parent
f2cfe0c4e3
commit
16717f2405
|
|
@ -0,0 +1,72 @@
|
||||||
|
const { Model } = require('sequelize');
|
||||||
|
const bcrypt = require("bcryptjs");
|
||||||
|
|
||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
class Member extends Model {
|
||||||
|
/**
|
||||||
|
* Helper method for defining associations.
|
||||||
|
* This method is not a part of Sequelize lifecycle.
|
||||||
|
* The `models/index` file will call this method automatically.
|
||||||
|
*/
|
||||||
|
static associate(models) {
|
||||||
|
Member.belongsTo(models.User, {
|
||||||
|
foreignKey: 'loginId',
|
||||||
|
targetKey: 'id'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
User.init({
|
||||||
|
firstName: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
lastName: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
dateOfBirth: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
height: {
|
||||||
|
type: DataTypes.INTEGER
|
||||||
|
},
|
||||||
|
weight: {
|
||||||
|
type: DataTypes.INTEGER
|
||||||
|
},
|
||||||
|
handedness: {
|
||||||
|
type: DataTypes.ENUM('LeftHandedness', 'RightHandedness'),
|
||||||
|
},
|
||||||
|
position: {
|
||||||
|
type: DataTypes.ENUM
|
||||||
|
},
|
||||||
|
preferredPosition: {
|
||||||
|
type: DataTypes.ENUM
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'User',
|
||||||
|
tableName: "Users",
|
||||||
|
hooks: {
|
||||||
|
beforeCreate: async (user) => {
|
||||||
|
const salt = await bcrypt.genSalt(10);
|
||||||
|
user.password = await bcrypt.hash(user.password, salt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
defaultScope: {
|
||||||
|
attributes: { exclude: ['password'] },
|
||||||
|
},
|
||||||
|
scopes: {
|
||||||
|
withSecretColumns: {
|
||||||
|
attributes: { include: ['password'] },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
User.prototype.validPassword = function (password) {
|
||||||
|
return bcrypt.compareSync(password, this.password);
|
||||||
|
};
|
||||||
|
|
||||||
|
return User;
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue