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

Question: Apply Scopes to Eager Loaded Modules #4625

Closed
dexwiz opened this issue Oct 8, 2015 · 6 comments
Closed

Question: Apply Scopes to Eager Loaded Modules #4625

dexwiz opened this issue Oct 8, 2015 · 6 comments

Comments

@dexwiz
Copy link

dexwiz commented Oct 8, 2015

I am trying to write a scope that returns select number of fields for serialization.

module.exports = function(sequelize, DataTypes) {
  var User= sequelize.define('User', {
    field1: {
      type: DataTypes.JSON,
      allowNull: false,
      defaultValue: {}
    },
    field2: {
      type: DataTypes.JSON,
      allowNull: false,
      defaultValue: {}
    }
  }, 
  {
    scopes: {
      json: {
        attributes: ['field1', 'field2' ]
      }
    },
    classMethods: {
      associate: function(models) {
        User.hasMany(models.Role);
      }
    },
  });

  return User;
};

This works in combination with user.get({ plain: true}) to produce a valid serialized object for exposing via an API. However, I cannot seem to find a way to to apply scopes to eagerly loaded models.

Tried

User.scope(['json']).findById(user.id, {
      include: [{
        model: Role,
        scope: 'json'
      }]
    })

Does not apply any scope

And specific scopes

User.scope(['userJson', 'roleJson']).findById(user.id, {
      include: [{
        model: Role,
        scope: 'json'
      }]
    })

Throws an error.

Is there is a there no way to apply scopes to eagerly loaded objects?

@dexwiz
Copy link
Author

dexwiz commented Oct 8, 2015

I was able to apply scopes, but it appears to only work for where clauses. Attribute filters do not seem to work.

User.scope(['json']).findById(user.id, {
      include: [{
        model: Role.scope('json')
      }]
    })

@mickhansen
Copy link
Contributor

That might actually not be supported. A solution could be to build in support for include.scope which then applies options and scopes the model.

/cc @janmeier

@dexwiz
Copy link
Author

dexwiz commented Oct 8, 2015

Its in the unit tests, but only seems to apply to some scope types

include: [{ model: this.ScopeMe.scope('isTony'), as: 'users'}]

@janmeier
Copy link
Member

janmeier commented Oct 8, 2015

Role.scope is the supported way for wheres, but yeah it might not support attributes

@mickhansen
Copy link
Contributor

Model.$injectScope(model.$scope, include);
we do call injectScope for include.

Model.$injectScope = function (scope, options) {

Doesn't look like $injectScope supports attributes at all actually @janmeier?

@mickhansen
Copy link
Contributor

Or actually it should copy attributes here:

_.defaults(options, filteredScope);

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