Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Footprint associations issues #11

Closed
cesar2064 opened this issue Jun 15, 2016 · 8 comments
Closed

Footprint associations issues #11

cesar2064 opened this issue Jun 15, 2016 · 8 comments

Comments

@cesar2064
Copy link

cesar2064 commented Jun 15, 2016

I have the following models:
User.js

module.exports = class User extends Model {

  static schema () {
    return {
      username: {
        type : String,
        unique: true,
        required: true,
        index:true,
        trim: true,
        lowercase: true
      },
      email: {
        type: String,
        unique: true,
        lowercase: true,
        trim: true,
        index:true,
        validate: {
          validator: function (val) {
            return true
          },
          message: '{VALUE} is not a valid email'
        }
      },
      password:{
        type:String,
        require:true
      },
      roles : {
        type : Schema.ObjectId,
        ref : 'Role'
      }
    }
  }

  static config () {
    return {
      // Collection name
      tableName: 'user',

      // Schema options
      schema: {
        timestamps: true,

        versionKey: false,

        toObject: {
          virtuals: true
        },

        toJSON: {
          virtuals: true
        }
      },
      methods:{
        toJSON:function(){
          var user = this.toObject()
          delete user.password

          return user
        }
      },
      // Schema statics
      statics: {
        getByEmail: function (email) {
          return this
            .findOne({
              email: _.trim(email)
            })
            .exec()
        },
      }
    }
  }
  /**
   * After Trails.js will create model Schema you could add anything you want here
   * @param  {mongoose.Schema} schema mongoose new Schema object
   */
  static onSchema (schema) {
    // lifecircle events

    schema.pre('save', function (next) {
      //encrypt user password

      let that = this

      bcrypt.hash(this.password, 10, function(err, hash) {
        that.password = hash
        next()
      })
    })
  }

}

Role.js

module.exports = class Role extends Model {

  static schema () {
    return {
      name:{
        type: String,
        required: true,
        unique: true,
        lowercase: true,
        trim: true,
        index:true
      },
      users : {
        type : Schema.ObjectId,
        ref : 'User'
      }
    }
  }

  static config () {
    return {
      // Collection name
      tableName: 'role',

      // Schema options
      schema: {
        timestamps: true,

        versionKey: false,

        toObject: {
          virtuals: false
        },

        toJSON: {
          virtuals: false
        }
      }
    }
  }
  /**
   * After Trails.js will create model Schema you could add anything you want here
   * @param  {mongoose.Schema} schema mongoose new Schema object
   */
  static onSchema (schema) {}

}

And when I try to access:

"Get" /api/v1/user/5751cb90a64b7abe1987e0be/roles?id=5751ce400bdad0ff1a4416da

"Get" /api/v1/user/5751cb90a64b7abe1987e0be/roles

"PUT", "/api/v1/user/5751cb90a64b7abe1987e0be/role?id=5751ce400bdad0ff1a4416da", data:{“name”,”rolex”}

"POST", "/api/v1/user/5751cb90a64b7abe1987e0be/role", data:{“name”,”rolex2”}

"DELETE", "/api/v1/user/5751cb90a64b7abe1987e0be/roles?id=5751ce400bdad0ff1a4416da"

The status of the request is pending, until the browser throws a timeout error

@konstantinzolotarev
Copy link
Collaborator

@cesar2064 Actually I didn't implement relations at all. because mongoose don't have native support. But will try to implement something. Soon

@cesar2064
Copy link
Author

Ok, I will wait for the implementation of that. Thanks for your help and availability.

@jaumard
Copy link
Contributor

jaumard commented Aug 24, 2016

👍 could be nice to have to have compatibility with other trailpacks that use the FootprintService, for now they fail.
Example: https://github.com/jaumard/trailpack-passport/blob/87e3bd140ebd6fb0249e1a0ac6d600c7e1730365/api/services/PassportService.js#L136

@konstantinzolotarev
Copy link
Collaborator

@jaumard
Copy link
Contributor

jaumard commented Nov 26, 2016

So now there are officially supported @konstantinzolotarev ? :) I see them on the master

@konstantinzolotarev
Copy link
Collaborator

konstantinzolotarev commented Nov 26, 2016 via email

@jaumard
Copy link
Contributor

jaumard commented Nov 26, 2016

Thanks, I'm closing this then :)

@jaumard jaumard closed this as completed Nov 26, 2016
@cesar2064
Copy link
Author

Good news, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants