$ open source · GPL-3.0 — need a commercial license? imqueue.com →

query.autoQuery() function

Builds Sequelize find options from a requested fields map, joins included.

Signature:

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 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 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

const where = toWhereOptions(withRangeFilters(filter));
const rows = await LeadModel.findAll(autoQuery<FindOptions>(
    LeadModel,
    fields,
    where,
    toLimitOptions(pageOptions),
    toOrderOptions(orderBy),
));