Complex include options

interface IncludeOptions {
    as?: string;
    association?: string | Association<Model<any, any>, Model<any, any>, string, NormalizedAssociationOptions<string>>;
    attributes?: FindAttributeOptions<any>;
    duplicating?: boolean;
    include?: AllowArray<Includeable>;
    limit?: number | Nullish | Literal;
    model?: ModelStatic<Model<any, any>>;
    on?: WhereOptions<any>;
    or?: boolean;
    order?: Order;
    paranoid?: boolean;
    required?: boolean;
    right?: boolean;
    separate?: boolean;
    subQuery?: boolean;
    through?: IncludeThroughOptions;
    where?: WhereOptions<any>;
}

Hierarchy (view full)

Properties

as?: string

The alias of the association. Used along with IncludeOptions.model.

This must be specified if the association has an alias (i.e. "as" was used when defining the association). For hasOne / belongsTo, this should be the singular name, and for hasMany / belongsToMany, it should be the plural.

Deprecated

using "as" is the same as using IncludeOptions.association because "as" is always the name of the association.

association?: string | Association<Model<any, any>, Model<any, any>, string, NormalizedAssociationOptions<string>>

The association you want to eagerly load. Either one of the values available in Model.associations, or the name of the association.

This can be used instead of providing a model/as pair.

This is the recommended method.

attributes?: FindAttributeOptions<any>

If an array: a list of the attributes that you want to select. Attributes can also be raw SQL (literal), fn, col, and cast

To rename an attribute, you can pass an array, with two elements:

  • The first is the name of the attribute (or literal, fn, col, cast),
  • and the second is the name to give to that attribute in the returned instance.

If include is used: selects all the attributes of the model, plus some additional ones. Useful for aggregations.

Example

{ attributes: { include: [[literal('COUNT(id)'), 'total']] }

If exclude is used: selects all the attributes of the model, except the one specified in exclude. Useful for security purposes

Example

{ attributes: { exclude: ['password'] } }
duplicating?: boolean

Mark the include as duplicating, will prevent a subquery from being used.

Load further nested related models

limit?: number | Nullish | Literal

Limit include.

Only available when setting IncludeOptions.separate to true.

model?: ModelStatic<Model<any, any>>

The model you want to eagerly load.

This option only works if this model is only associated once to the parent model of this query. We recommend specifying IncludeOptions.association instead.

on?: WhereOptions<any>

Custom ON clause, overrides default.

or?: boolean

Whether to bind the ON and WHERE clause together by OR instead of AND.

Default

false
order?: Order

Order include. Only available when setting separate to true.

paranoid?: boolean

If true, only non-deleted records will be returned. If false, both deleted and non-deleted records will be returned.

Only applies if InitOptions.paranoid is true for the model.

Default

true
required?: boolean

If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children.

True if include.where is set, false otherwise.

right?: boolean

If true, converts to a right join if dialect support it.

Incompatible with IncludeOptions.required.

separate?: boolean

If true, runs a separate query to fetch the associated instances.

Default

false
subQuery?: boolean

Use sub queries. This should only be used if you know for sure the query does not result in a cartesian product.

Through Options

where?: WhereOptions<any>

Where clauses to apply to the child model

Note that this converts the eager load to an inner join, unless you explicitly set IncludeOptions.required to false