category.js 651 B

123456789101112131415161718192021222324252627282930313233
  1. let randomColor = require('randomcolor')
  2. module.exports = (sequelize, DataTypes) => {
  3. let Category = sequelize.define('Category', {
  4. name: {
  5. type: DataTypes.STRING,
  6. unique: true,
  7. set (val) {
  8. let underscored = val.trim().replace(/\s/g, '_').toUpperCase()
  9. this.setDataValue('name', val)
  10. this.setDataValue('value', underscored)
  11. }
  12. },
  13. value: {
  14. type: DataTypes.STRING,
  15. unique: true
  16. },
  17. color: {
  18. type: DataTypes.STRING,
  19. defaultValue () {
  20. return randomColor({ luminosity: 'bright' })
  21. }
  22. }
  23. }, {
  24. classMethods: {
  25. associate (models) {
  26. Category.hasMany(models.Thread)
  27. }
  28. }
  29. })
  30. return Category
  31. }