Skip to content

Instantly share code, notes, and snippets.

@s3rgeym
Last active April 17, 2026 14:26
Show Gist options
  • Select an option

  • Save s3rgeym/ebd5ac8fbd1d042ad1b4926a042d9666 to your computer and use it in GitHub Desktop.

Select an option

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

s3rgeym commented May 6, 2019

Copy link
Copy Markdown
Author

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

await User.sync({ force: false })

@s3rgeym

s3rgeym commented May 6, 2019

Copy link
Copy Markdown
Author

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

s3rgeym commented May 6, 2019

Copy link
Copy Markdown
Author

image
image

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