ip.js 625 B

12345678910111213141516171819202122232425262728293031
  1. module.exports = (sequelize, DataTypes) => {
  2. let Ip = sequelize.define('Ip', {
  3. ip: {
  4. type: DataTypes.STRING(45),
  5. validate: {
  6. isIP: true
  7. }
  8. }
  9. }, {
  10. classMethods: {
  11. associate (models) {
  12. Ip.belongsToMany(models.User, { through: 'UserIp' })
  13. },
  14. async createIfNotExists (ipAddress, user) {
  15. let existingIp = await Ip.findOne({
  16. where: { ip: ipAddress },
  17. include: [{
  18. model: sequelize.models.User,
  19. where: { id: user.id }
  20. }]
  21. })
  22. if(!existingIp) {
  23. let ip = await Ip.create({ ip: ipAddress })
  24. await ip.addUser(user)
  25. }
  26. }
  27. }
  28. })
  29. return Ip
  30. }