Merging

Several scopes can be applied simultaneously by passing an array of scopes to .scope, or by passing the scopes as consecutive arguments.

  1. // These two are equivalent
  2. Project.scope('deleted', 'activeUsers').findAll();
  3. Project.scope(['deleted', 'activeUsers']).findAll();
  1. SELECT * FROM projects
  2. INNER JOIN users ON projects.userId = users.id
  3. WHERE projects.deleted = true
  4. AND users.active = true

If you want to apply another scope alongside the default scope, pass the key defaultScope to .scope:

  1. Project.scope('defaultScope', 'deleted').findAll();
  1. SELECT * FROM projects WHERE active = true AND deleted = true

When invoking several scopes, keys from subsequent scopes will overwrite previous ones (similarly to Object.assign), except for where and include, which will be merged. Consider two scopes:

  1. {
  2. scope1: {
  3. where: {
  4. firstName: 'bob',
  5. age: {
  6. [Op.gt]: 20
  7. }
  8. },
  9. limit: 2
  10. },
  11. scope2: {
  12. where: {
  13. age: {
  14. [Op.gt]: 30
  15. }
  16. },
  17. limit: 10
  18. }
  19. }

Calling .scope('scope1', 'scope2') will yield the following query

  1. WHERE firstName = 'bob' AND age > 30 LIMIT 10

Note how limit and age are overwritten by scope2, while firstName is preserved. The limit, offset, order, paranoid, lock and raw fields are overwritten, while where is shallowly merged (meaning that identical keys will be overwritten). The merge strategy for include will be discussed later on.

Note that attributes keys of multiple applied scopes are merged in such a way that attributes.exclude are always preserved. This allows merging several scopes and never leaking sensitive fields in final scope.

The same merge logic applies when passing a find object directly to findAll (and similar finders) on a scoped model:

  1. Project.scope('deleted').findAll({
  2. where: {
  3. firstName: 'john'
  4. }
  5. })
  1. WHERE deleted = true AND firstName = 'john'

Here the deleted scope is merged with the finder. If we were to pass where: { firstName: 'john', deleted: false } to the finder, the deleted scope would be overwritten.

Merging includes

Includes are merged recursively based on the models being included. This is a very powerful merge, added on v5, and is better understood with an example.

Consider four models: Foo, Bar, Baz and Qux, with has-many associations as follows:

  1. class Foo extends Model {}
  2. class Bar extends Model {}
  3. class Baz extends Model {}
  4. class Qux extends Model {}
  5. Foo.init({ name: Sequelize.STRING }, { sequelize });
  6. Bar.init({ name: Sequelize.STRING }, { sequelize });
  7. Baz.init({ name: Sequelize.STRING }, { sequelize });
  8. Qux.init({ name: Sequelize.STRING }, { sequelize });
  9. Foo.hasMany(Bar, { foreignKey: 'fooId' });
  10. Bar.hasMany(Baz, { foreignKey: 'barId' });
  11. Baz.hasMany(Qux, { foreignKey: 'bazId' });

Now, consider the following four scopes defined on Foo:

  1. {
  2. includeEverything: {
  3. include: {
  4. model: this.Bar,
  5. include: [{
  6. model: this.Baz,
  7. include: this.Qux
  8. }]
  9. }
  10. },
  11. limitedBars: {
  12. include: [{
  13. model: this.Bar,
  14. limit: 2
  15. }]
  16. },
  17. limitedBazs: {
  18. include: [{
  19. model: this.Bar,
  20. include: [{
  21. model: this.Baz,
  22. limit: 2
  23. }]
  24. }]
  25. },
  26. excludeBazName: {
  27. include: [{
  28. model: this.Bar,
  29. include: [{
  30. model: this.Baz,
  31. attributes: {
  32. exclude: ['name']
  33. }
  34. }]
  35. }]
  36. }
  37. }

These four scopes can be deeply merged easily, for example by calling Foo.scope('includeEverything', 'limitedBars', 'limitedBazs', 'excludeBazName').findAll(), which would be entirely equivalent to calling the following:

  1. Foo.findAll({
  2. include: {
  3. model: this.Bar,
  4. limit: 2,
  5. include: [{
  6. model: this.Baz,
  7. limit: 2,
  8. attributes: {
  9. exclude: ['name']
  10. },
  11. include: this.Qux
  12. }]
  13. }
  14. });

Observe how the four scopes were merged into one. The includes of scopes are merged based on the model being included. If one scope includes model A and another includes model B, the merged result will include both models A and B. On the other hand, if both scopes include the same model A, but with different options (such as nested includes or other attributes), those will be merged recursively, as shown above.

The merge illustrated above works in the exact same way regardless of the order applied to the scopes. The order would only make a difference if a certain option was set by two different scopes - which is not the case of the above example, since each scope does a different thing.

This merge strategy also works in the exact same way with options passed to .findAll, .findOne and the like.