Usage

Scopes are applied by calling .scope on the model definition, passing the name of one or more scopes. .scope returns a fully functional model instance with all the regular methods: .findAll, .update, .count, .destroy etc. You can save this model instance and reuse it later:

  1. const DeletedProjects = Project.scope('deleted');
  2. DeletedProjects.findAll();
  3. // some time passes
  4. // let's look for deleted projects again!
  5. DeletedProjects.findAll();

Scopes apply to .find, .findAll, .count, .update, .increment and .destroy.

Scopes which are functions can be invoked in two ways. If the scope does not take any arguments it can be invoked as normally. If the scope takes arguments, pass an object:

  1. Project.scope('random', { method: ['accessLevel', 19]}).findAll();
  1. SELECT * FROM projects WHERE someNumber = 42 AND accessLevel >= 19