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:
const DeletedProjects = Project.scope('deleted');
DeletedProjects.findAll();
// some time passes
// let's look for deleted projects again!
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:
Project.scope('random', { method: ['accessLevel', 19]}).findAll();
SELECT * FROM projects WHERE someNumber = 42 AND accessLevel >= 19