Skip to content

Commit

Permalink
feat(general): added user model
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Peshkov committed Jan 29, 2020
1 parent f082945 commit ed40ab2
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
79 changes: 79 additions & 0 deletions migrations/20200128175455-create-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
email: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
password: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
mail_confirmed_at: {
type: Sequelize.DATE,
allowNull: true,
defaultValue: Sequelize.NOW
},
active: {
type: Sequelize.BOOLEAN,
allowNull: true,
defaultValue: true
},
superadmin: {
type: Sequelize.BOOLEAN,
allowNull: true,
defaultValue: false
},
first_name: {
type: Sequelize.STRING,
allowNull: false
},
last_name: {
type: Sequelize.STRING,
allowNull: false
},
date_of_birth: {
type: Sequelize.DATEONLY,
allowNull: true
},
gender: {
type: Sequelize.STRING,
allowNull: true
},
phone: {
type: Sequelize.STRING,
allowNull: true
},
address: {
type: Sequelize.TEXT,
allowNull: true
},
about_me: {
type: Sequelize.TEXT,
allowNull: true
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => {
return queryInterface.dropTable('users');
}
};
118 changes: 118 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const bcrypt = require('bcrypt');

const { Sequelize, sequelize } = require('../lib/sequelize');
const config = require('../config');

const User = sequelize.define('user', {
username: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Username should be set.' },
},
unique: true
},
email: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Email should be set.' },
isEmail: { msg: 'Email should be valid.' }
},
unique: true
},
password: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Password should be set.' },
},
unique: true
},
mail_confirmed_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW
},
active: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true
},
superadmin: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
},
first_name: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'First name should be set.' },
}
},
last_name: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Last name should be set.' },
}
},
date_of_birth: {
type: Sequelize.DATEONLY,
allowNull: true
},
gender: {
type: Sequelize.STRING,
allowNull: true,
defaultValue: ''
},
phone: {
type: Sequelize.STRING,
allowNull: true,
defaultValue: ''
},
address: {
type: Sequelize.TEXT,
allowNull: true,
defaultValue: ''
},
about_me: {
type: Sequelize.TEXT,
allowNull: true,
defaultValue: ''
}
}, {
underscored: true,
tableName: 'users',
createdAt: 'created_at',
updatedAt: 'updated_at',
defaultScope: {
attributes: { exclude: ['password'] }
},
scopes: {
withPassword: {
attributes: { exclude: [] }
},
noExtraFields: {
attributes: { exclude: ['password', 'superadmin', 'active', 'mail_confirmed_at'] }
}
}
});

User.afterValidate(async (user) => {
if (user.changed('password')) {
user.password = await bcrypt.hash(user.password, config.salt_rounds);
}
});

User.prototype.checkPassword = async function checkPassword(password) {
return bcrypt.compare(password, this.password);
};

module.exports = User;
5 changes: 5 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const User = require('./User');

module.exports = {
User
};

0 comments on commit ed40ab2

Please sign in to comment.