Skip to content

How can I define model relation in common Mixins? #2453

@Emiya0306

Description

@Emiya0306

I need to define relation between models which use Mixins, but when I console.log(app.models.role), I cannot get the Model. It is a good work flow that Mixins is happened under the ModelBuilder, but I guess that Mixins is defined before the ModelBuilder.

So how can I get the ModelBuilder in mixins?

module.exports = function (Model, option = {}) {

  Model.getApp((err, app) => {

    console.log(app.models.role); // it is undefined

    //Model.hasMany(app.models.role, {as: 'roles', foreignKey: `${Model.modelName}Id`});

    Model.role = function (role, cb) {

      app.models.role.create(role).then((createdRole) => { // it can get in remote method

        cb(null, createdRole);

        console.log(Model.modelName);

      }).then(()=>{

      }).catch((err)=> {

        cb(err, null);

      })

    };

    Model.remoteMethod(
      'role', {
        description: 'Create a role for this Model.',
        http: {path: '/authorization/role', verb: 'post'},
        accepts: {arg: 'role', type: 'role', http: {source: 'body'}},
        returns: {arg: 'role', type: 'role'}
      }
    );

  })

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions