Skip to content

Commit

Permalink
feat: add hook example
Browse files Browse the repository at this point in the history
  • Loading branch information
mahieyin-rahmun committed Jul 26, 2022
1 parent 69ceaa5 commit c4eb87f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/models/users.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "sequelize";
import { IUser } from "@interfaces/users.interface";
import { ITimestamps } from "@interfaces/common.interface";
import bcrypt from "bcrypt";

export const UsersTableName = "Users";
export type UserCreationAttributes = Optional<IUser, "id">;
Expand Down Expand Up @@ -54,6 +55,11 @@ export default function (sequelize: Sequelize): typeof UserModel {
UserModel.init(UserModelAttributes, {
tableName: UsersTableName,
sequelize,
hooks: {
beforeCreate: (userInstance, _) => {
userInstance.password = bcrypt.hashSync(userInstance.password, 8);
},
},
});

return UserModel;
Expand Down

0 comments on commit c4eb87f

Please sign in to comment.