Skip to content

Instantly share code, notes, and snippets.

@devdbrandy
Last active Mar 24, 2019
Embed
What would you like to do?
Sequelize User model in es6
import Sequelize from 'sequelize';
/**
* A model class representing user resource
*
* @param {Sequelize} sequelize - Sequelize object
* @param {Sequelize.DataTypes} DataTypes - A convinient object holding data types
* @return {Sequelize.Model} - User model
*/
export default (sequelize, DataTypes) => {
/** @type {Sequelize.Model} */
const User = sequelize.define('User', {
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
is: /^[a-zA-Z0-9]+$/i,
},
set(val) {
this.setDataValue('username', val.toLowerCase()); // convert to lowercase
},
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: { msg: 'Must be a valid email address' },
},
},
bio: DataTypes.STRING,
image: DataTypes.STRING,
hash: DataTypes.STRING,
salt: DataTypes.STRING,
createdAt: {
type: DataTypes.DATE,
defaultValue: sequelize.NOW,
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: sequelize.NOW,
onUpdate: sequelize.NOW,
},
});
/**
* User relationship
*
* @param {Sequelize.Model} models - Sequelize model (destructured)
* @returns {void}
*/
User.associate = (models) => {
// association comes in here
};
return User;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment