### findAndCountAll - Search for multiple elements in the database, returns both data and total count
### findAndCountAll - Search for multiple elements in the database, returns both data and total count
This is a convienience method that combines`findAll`()and `count`()(see below), this is useful when dealing with queries related to pagination where you want to retrieve data with a `limit` and `offset` but also need to know the total number of records that match the query.
This is a convienience method that combines`findAll` and `count` (see below) this is useful when dealing with queries related to pagination where you want to retrieve data with a `limit` and `offset` but also need to know the total number of records that match the query:
The success handler will always receive an object with two properties:
The success handler will always receive an object with two properties:
*`count` - an integer, total number records (matching the where clause)
*`count` - an intege, total number records matching the where clause
*`rows` - an array of objects, the records (matching the where clause) within the limit/offset range
*`rows` - an array of objects, the records matching the where clause, within the limit and offset range
```js
```js
Project
Project
.findAndCountAll({
.findAndCountAll({
...
@@ -107,7 +107,33 @@ Project
...
@@ -107,7 +107,33 @@ Project
});
});
```
```
The options [object] that you pass to`findAndCountAll`()is the same as for`findAll`()(described below).
`findAndCountAll` also supports includes. Only the includes that are marked as `required` will be added to the count part:
Suppose you want to find all users who have a profile attached:
```js
User.findAndCountAll({
include:[
{model:Profile,required:true}
],
limit3
});
```
Because the include for `Profile` has `required` set it will result in an inner join, and only the users who have a profile will be counted. If we remove `required` from the include, both users with and without profiles will be counted. Adding a `where` clause to the include automatically makes it required:
```js
User.findAndCountAll({
include:[
{model:Profile,where:{active:true}}
],
limit3
});
```
The query above will only count users who have an active profile, because `required` is implicitly set to true when you add a where clause to the include.
The options object that you pass to `findAndCountAll` is the same as for `findAll` (described below).
### findAll - Search for multiple elements in the database
### findAll - Search for multiple elements in the database
* In the above example, `result.rows` will contain rows 13 through 24, while `result.count` will return the total number of rows that matched your query.
* In the above example, `result.rows` will contain rows 13 through 24, while `result.count` will return the total number of rows that matched your query.
*
*
* When you add includes, only those which are required (either because they have a where clause, or because `required` is explicitly set to true on the include) will be added to the count part.
*
* Suppose you want to find all users who have a profile attached:
* ```js
* User.findAndCountAll({
* include: [
* { model: Profile, required: true}
* ],
* limit 3
* });
* ```
* Because the include for `Profile` has `required` set it will result in an inner join, and only the users who have a profile will be counted. If we remove `required` from the include, both users with and without profiles will be counted
*
* @param {Object} [findOptions] See findAll
* @param {Object} [findOptions] See findAll
*
*
* @see {Model#findAll} for a specification of find and query options
* @see {Model#findAll} for a specification of find and query options