Skip to content
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

docs: clarify how the limit option works #13985

Merged
merged 13 commits into from Jan 27, 2022
41 changes: 39 additions & 2 deletions types/lib/model.d.ts
Expand Up @@ -555,7 +555,41 @@ export interface FindOptions<TAttributes = any>
group?: GroupOption;

/**
* Limit the results
* Limits how many items will be retrieved by the operation.
*
* If `limit` and `include` are used together, Sequelize will turn the `subQuery` option on by default.
* This is done to ensure that `limit` only impacts the Model on the same level as the `limit` option.
*
* You can disable this behavior by explicitly setting `subQuery: false`, however `limit` will then
* affect the total count of returned values, including eager-loaded associations, instead of just one table.
*
* @example
* // in the following query, `limit` only affects the "User" model.
* // This will return 2 users, each including all of their projects.
* User.findAll({
* limit: 2,
* include: [User.associations.projects],
* });
*
* @example
* // in the following query, `limit` affects the total number of returned values, eager-loaded associations included.
* // This may return 2 users, each with one project,
* // or 1 user with 2 projects.
* User.findAll({
* limit: 2,
* include: [User.associations.projects],
* subQuery: false,
* });
*
* @example
* // in the following query, `limit` only affects how many projects are returned for each user.
* // This may return all users, each with up to 2 project,
* User.findAll({
* include: [{
* association: User.associations.projects,
* limit: 2,
* }],
* });
*/
limit?: number;

Expand Down Expand Up @@ -589,7 +623,10 @@ export interface FindOptions<TAttributes = any>
having?: WhereOptions<any>;

/**
* Use sub queries (internal)
* Use sub queries (internal).
*
* If unspecified, this will `true` by default if `limit` is specified, and `false` otherwise.
* See {@link FindOptions#limit} for more information.
*/
subQuery?: boolean;
}
Expand Down