Skip to content

Instantly share code, notes, and snippets.

@s3rgeym
Last active May 6, 2019 20:11
Show Gist options
  • Save s3rgeym/ebd5ac8fbd1d042ad1b4926a042d9666 to your computer and use it in GitHub Desktop.
Save s3rgeym/ebd5ac8fbd1d042ad1b4926a042d9666 to your computer and use it in GitHub Desktop.
'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' });
}
}
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,
}
@s3rgeym
Copy link
Author

s3rgeym commented May 6, 2019

Создание таблицы:

await User.sync({ force: false })

@s3rgeym
Copy link
Author

s3rgeym commented May 6, 2019

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.

@s3rgeym
Copy link
Author

s3rgeym commented May 6, 2019

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment