J
J
jenya77712019-04-17 18:08:58
PostgreSQL
jenya7771, 2019-04-17 18:08:58

How to make a multi-level referral system using SequelizeJs?

Hello, how can I implement a multi-level referral system using SequelizeJs?
Now I have a single-level one, in the table with the user there is a ref field, and a link has been added. Thus, I can get a user and a referral, but I can no longer get a referral of a referral.

const users = sequelize.define('users', {
  login:  { 
    type: Sequelize.STRING, 
    allowNull: false,
    unique: true
  },
  password: { 
    type: Sequelize.STRING, 
    allowNull: false
  },
  ref: { 
    type: Sequelize.INTEGER ,
    allowNull: true,
    foreignKey : true
  }
})

users.hasMany(users, {foreignKey: 'ref', as: 'userRefList'})
users.belongsTo(users, {foreignKey : 'ref', as: 'userRef'})

Is it possible to get the whole chain of referrals in such an implementation?
Or suggest another, more convenient and correct way.
Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question