Last active
May 6, 2019 20:11
-
-
Save s3rgeym/ebd5ac8fbd1d042ad1b4926a042d9666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import Sequelize from 'sequelize'; | |
import bcrypt from 'bcryptjs'; | |
export default class User extends Sequelize.Model { | |
static init(sequelize) { | |
return super.init({ | |
username: { | |
type: Sequelize.STRING, | |
unique: true, | |
allowNull: false | |
}, | |
password: { | |
type: Sequelize.STRING, | |
allowNull: false, | |
validate: { | |
notEmpty: { msg: 'Password can\'t be empty' } | |
}, | |
set(value) { | |
this.setDataValue('password', bcrypt.hashSync(value, 10)); | |
} | |
}, | |
userCategoryId: { | |
type: Sequelize.INTEGER, | |
references: { | |
model: sequelize.models.UserCategory, | |
key: 'id' | |
} | |
}, | |
email: { | |
type: Sequelize.STRING, | |
unique: true, | |
allowNull: false, | |
validate: { | |
isEmail: { msg: 'Please provide valid e-mail address' } | |
} | |
} | |
}, { | |
sequelize: sequelize, | |
tableName: 'users', | |
timestamps: true, | |
paranoid: true | |
}); | |
} | |
static associate(models) { | |
this.belongsTo(models.UserCategory, { foreignKey: 'userCategoryId' }); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Sequelize = require('sequlize') | |
class User extends Sequlize.Model { | |
static init(sequelize) { | |
return super.init({ | |
id: { | |
type: Sequelize.INTEGER, | |
primaryKey: true, | |
autoIncrement: true, | |
}, | |
}, { | |
sequelize, | |
tableName: 'users', | |
schema: 'app' | |
}) | |
} | |
} | |
module.exports = { | |
User, | |
} |
If you don't define a primaryKey then sequelize uses id by default.
If you want to set your own, just use primaryKey: true on your column.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Создание таблицы: