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

Comments

3 participants
@dexwiz

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

This comment has been minimized.

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

This comment has been minimized.

Contributor

mickhansen commented Oct 8, 2015

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

This comment has been minimized.

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

This comment has been minimized.

Member

janmeier commented Oct 8, 2015

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

@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Oct 8, 2015

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

This comment has been minimized.

Contributor

mickhansen commented Oct 8, 2015

Or actually it should copy attributes here:

_.defaults(options, filteredScope);

@janmeier janmeier closed this in b503b51 Oct 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment