# query.autoQuery() function · @imqueue/pg-sequelize

Source: https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.query.autoquery/
Published: 2026-08-01
Author: @imqueue maintainers (https://github.com/imqueue)
Package: @imqueue/pg-sequelize 4.2.0 — generated reference, not hand-written

Builds Sequelize find options from a requested fields map, joins included.

**Signature:**

```typescript
export function autoQuery<T>(model: any, fields?: any, ...merge: (Partial<T> | undefined)[]): T;
```

## Parameters


| Parameter | Type | Description |
| --- | --- | --- |
| model | any | Model to build the query for. |
| fields | any | _(Optional)_ Requested fields map, or an array of column names. |
| merge | (Partial<T> \| undefined)\[\] | Option fragments to merge in, typically from the helpers below. |


**Returns:**

T

Find options, typed as the caller asks.

## Remarks

The helper the others compose into. It turns a [FieldsInput](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.fieldsinput/) into `attributes` plus a nested `include` for every relation the map mentions, recursing into each level, and adds the foreign keys a join needs whether or not they were requested. Its rest parameter then merges the fragments the sibling helpers return, which is the intended shape of a paginated read.

A value in the map that is not `false` doubles as a filter for that column, so a fields map can carry a where clause; see [FieldsInput](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.fieldsinput/) for why presence rather than value is what selects.

Two things to know. It MUTATES the `fields` map, adding any column named in a merged `order` that the map omits, so ordering cannot break selection. And `fields` may also be a plain array of names, which selects those columns and builds no joins at all.

## Example


```typescript
const where = toWhereOptions(withRangeFilters(filter));
const rows = await LeadModel.findAll(autoQuery<FindOptions>(
    LeadModel,
    fields,
    where,
    toLimitOptions(pageOptions),
    toOrderOptions(orderBy),
));
```

