Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. var Sequelize = require('sequelize');
  2. var bcrypt = require('bcrypt');
  3.  
  4.  
  5.  
  6. // create a sequelize instance with our local postgres database information.
  7. // var sequelize = new Sequelize('mysql://David@David:3307/IS');
  8. var sequelize = new Sequelize({
  9. host: 'data.velehradsky.cz',
  10. database: 'IS',
  11. port: 3307,
  12. username: 'David',
  13. password: 'David',
  14. dialect: 'mysql'
  15. });
  16.  
  17.  
  18.  
  19.  
  20. // setup User model and its fields.
  21. var User = sequelize.define('user_test', {
  22. UID: {
  23. type: Sequelize.INTEGER,
  24. unique: true,
  25. allowNull: false,
  26. autoIncrement: true,
  27. primaryKey: true
  28. },
  29. username: {
  30. type: Sequelize.STRING,
  31. unique: true,
  32. allowNull: false
  33. },
  34. email: {
  35. type: Sequelize.STRING,
  36. unique: true,
  37. allowNull: false
  38. },
  39. password: {
  40. type: Sequelize.STRING,
  41. allowNull: false
  42. }
  43.  
  44.  
  45. }, {
  46.  
  47. freezeTableName: true,
  48. hooks: {
  49. beforeCreate: (user) => {
  50. const salt = bcrypt.genSaltSync();
  51. user.password = bcrypt.hashSync(user.password, salt);
  52. }
  53. },
  54.  
  55. // instanceMethods: {
  56. // validPassword: function(password) {
  57. // return bcrypt.compareSync(password, this.password);
  58. // }
  59. // }
  60.  
  61. });
  62.  
  63. User.prototype.validPassword = function (password) {
  64. return bcrypt.compareSync(password, this.password);
  65. }
  66.  
  67.  
  68. // // create all the defined tables in the specified database.
  69. // sequelize.sync()
  70. // .then(() => console.log('users table has been successfully created, if one doesn\'t exist'))
  71. // .catch(error => console.log('This error occured', error));
  72.  
  73. // export User model for use in other files.
  74. module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement