0

I have two models in sails

1. User_type
2. User
  1. User_type Model:

    module.exports = {
    schema: true,
    connection: 'mongo',
    attributes: {
    
      user_type: {
      type: 'string',
      required: true,
      enum: ['superadmin', 'admin', 'follower']
    },    
    
    toJSON() {
    return this.toObject();
    }
    },
    
    beforeUpdate: (values, next) => next(),
    beforeCreate: (values, next) => next()
    };
    
  2. User Model:

    module.exports = {
      schema: true,
    
      attributes: {
    
    
        fname: {
          type: 'string',
          required: true,
        },    
    
        user_login_type: {
          // This user_login_type should be having values from 
          // User_type Model with a reference to field 'user_type', that is
          // whatever values(superadmin', 'admin', 'follower') are in the
          // 'user_type' of collection(or model) 'User_type' should only be
          // allowed to enter here, and no else value
        },    
    
    
        toJSON() {
          return this.toObject();
        }
      },
    
      beforeUpdate: (values, next) => next(),
      beforeCreate: (values, next) => next()
    };
    

I referred to different docs, questions and answers, but am not getting the exact flow, how to do this in sails

Every help is really appreciable

Matt
  • 14,906
  • 27
  • 99
  • 149

1 Answers1

1

Have a look at associations. You can create the reference using the model property:

...
user_login_type: {
    model: 'User_type'
},
...

Add required: true if you want to make it mandatory.

Note that the value of user_login_type won't be one of 'superadmin', 'admin', 'follower', but a reference to the associated model.

Viktor
  • 3,436
  • 1
  • 33
  • 42