Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. const like = sequelize.define('like', {
  2. id: {
  3. allowNull: false,
  4. autoIncrement: true,
  5. primaryKey: true,
  6. type: DataTypes.INTEGER,
  7. },
  8. user_id: DataTypes.INTEGER,
  9. other_user_id: DataTypes.INTEGER,
  10. }, { freezeTableName: true, timestamps: false });
  11. like.associate = (model) => {
  12. // 1 to many with like table
  13. like.belongsTo(model.user, { foreignKey: 'user_id' });
  14. };
  15. return like;
  16.  
  17. const user = sequelize.define('user', {
  18. id: {
  19. allowNull: false,
  20. autoIncrement: true,
  21. primaryKey: true,
  22. type: DataTypes.INTEGER,
  23. },
  24. username: {
  25. type: DataTypes.STRING,
  26. unique: {
  27. args: true,
  28. msg: 'Username already in use!',
  29. },
  30. },
  31. password: DataTypes.STRING,
  32. }, { freezeTableName: true, timestamps: false });
  33. user.associate = (model) => {
  34. // 1 to many with like table
  35. user.hasMany(model.like, {
  36. foreignKey: 'user_id',
  37. });
  38. };
  39. return user;
  40.  
  41. attributes: ['id', [sequelize.fn('count', sequelize.col('id')), 'count']],
  42. include: [
  43.  
  44. {
  45. model: user,
  46. },
  47.  
  48. ],
  49. where: {user.id: 40},
  50.  
  51. group: ['user.id'],
  52.  
  53. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement