SHARE
TWEET

Untitled

a guest Jan 26th, 2019 134 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. import bcrypt from 'bcrypt'
  4. import {
  5.   uuid
  6. } from '../utils/uuid'
  7.  
  8. module.exports = (sequelize, DataTypes) => {
  9.   const User = sequelize.define('User', {
  10.     uuid: {
  11.       allowNull: false,
  12.       unique: true,
  13.       type: 'BINARY(16)',
  14.       defaultValue: () => Buffer(uuid(), 'hex'),
  15.       get: function() {
  16.         return Buffer.from(this.getDataValue('uuid'))
  17.           .toString('hex')
  18.       }
  19.     },
  20.     email: {
  21.       allowNull: false,
  22.       unique: true,
  23.       type: DataTypes.STRING,
  24.       validate: {
  25.         isEmail: true,
  26.       },
  27.     },
  28.     password: {
  29.       allowNull: false,
  30.       type: DataTypes.STRING,
  31.     }
  32.   }, {
  33.     tableName: 'users',
  34.     timestamps: true,
  35.   })
  36.  
  37.   User.associate = function(models) {
  38.     // associations
  39.   }
  40.  
  41.   // hooks
  42.   User.beforeSave(async (user, options) => {
  43.     if (user.changed('password')) {
  44.       const salt = await bcrypt.genSalt(10);
  45.       user.password = await bcrypt.hash(user.password, salt);
  46.     }
  47.   });
  48.  
  49.   // print
  50.   User.prototype.toWeb = function() {
  51.     const values = Object.assign({}, this.get())
  52.  
  53.     delete values.id
  54.     delete values.password
  55.  
  56.     return values
  57.   }
  58.  
  59.   return User
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top