sails-auth icon indicating copy to clipboard operation
sails-auth copied to clipboard

How to add fields to User model?

Open alerdenisov opened this issue 9 years ago • 4 comments

I need to profile and extra data on User, but doesn't want to change User model in module. What i need to do?

alerdenisov avatar Dec 16 '15 16:12 alerdenisov

I don't know what you mean by not change the User model. That's definitely the easiest way to do it:

// api/models/User.js

var _ = require('lodash');
var _super = require('sails-permissions/api/models/User');

_.merge(exports, _super);
_.merge(exports, {

  // Extend with custom logic here by adding additional fields, methods, etc.
  attributes: {
    name: {
      type: 'string'
    },
    some_other_data: {
      type: 'string'
    }
  }
});

DaAwesomeP avatar Dec 24 '15 01:12 DaAwesomeP

+1 for @DaAwesomeP solution

po5i avatar Feb 22 '16 19:02 po5i

@DaAwesomeP Absolutely! This can be the way to go.

var _super = require('sails-auth/api/models/User'); // Dev may not be using sails-permissions

@alerdenisov, If you really don't want to add profile data in User table, you can create a one-to-one mapping between your User model and Profile model. Check this link to see how to create one-to-one associations.

PS: For creating a separate model as well, you will have to create a one-to-one mapping field in the User and Profile Models. For adding the field, you will have to extend the User model as DaAwesomeP suggested. You're out of luck there IMO!

aman-gautam avatar Mar 22 '16 05:03 aman-gautam

@aman-gautam while you have a very good point, how do you set the profile that the autogenerated admin uses?

trex005 avatar Jan 25 '18 10:01 trex005