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

query namespace

Turns a caller's serialized query into Sequelize options, and back out as SQL.

Signature:

export declare namespace query 

Remarks

The reason this package exists. A GraphQL API receives a filter, a page, an order and the set of fields the query selected — all of it plain JSON, none of it in a shape Sequelize accepts. These helpers translate: autoQuery is the one that composes the others, toWhereOptions turns a serialized filter into a where with the joins its nested parts imply, and toLimitOptions and toOrderOptions cover paging and ordering.

The efficiency comes from the fields map. A column nobody asked for is not selected and a relation nobody reached into is not joined, so the statement narrows as the caller's selection narrows rather than being fixed by the resolver.

Below that sit the escape hatches — sql, L and E — for the cases no option object can express.

Functions

Function

Description

autoCountQuery(model, fields, merge)

The counting counterpart of query.autoQuery(), for the same fields and filter.

autoQuery(model, fields, merge)

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

buildWhereFromArray(data)

ORs an array of filter values into one condition.

createEntity(model, input, fields, transaction)

Recursively creates entity and all it's relations from a given input using a given model.

E(input)

Renders a value as a SQL constant: a number as itself, a string quoted and escaped, anything else as NULL.

filtered(attributes, fields, model)

Intersects a set of attributes with the names a caller asked for.

foreignKeys(model, relations)

Foreign-key column names on a model for the given relations.

foreignKeysMap(parent, model)

Returns foreign key map for a given pair of parent model and related model.

getInclude(queryOptions, path)

Finds the include options for a model reached through a chain of includes.

L(str, values)

Builds a Sequelize literal from a string or a template — the escape hatch for SQL that no query option can express.

mergeQuery(queryOptions, merge)

Merges query-option fragments into one options object.

needNesting(model, fields)

Whether a fields map asks for any of the model's relations.

orNull(value)

Matches a value, or no value at all.

overrideJoin(queryOptions, options)

Traverses given query object, lookups for includes matching the given arguments of include options and overrides those are matching by model and alias with the provided option.

primaryKeys(model)

Return names of primary key fields for a given model.

pureFields(model, fields)

Narrows a requested fields map to the model's own columns.

safeSqlSpaceCleanup(input)

Collapses whitespace outside quoted literals, so a statement fits on one line.

skip(obj, props)

Deletes properties from an object.

sql(sqlQuery, values)

Normalises a SQL string: one-lined, whitespace collapsed, ending in a single semicolon.

toLimitOptions(pageOptions)

Builds proper paging options query part

toOrderOptions(orderBy)

Turns a serialized order into Sequelize's order option.

toWhereOptions(filter, inputType)

Turns a serializable filter into Sequelize where options.

withRangeFilters(filter)

Rewrites <column>Range filter properties onto the columns they belong to.

Interfaces

Interface

Description

ForeignKeyMap

Foreign key map representation, where related property name references parent property name.

PureDataFunction

The two shapes query.pureData accepts: one record, or many.

Variables

Variable

Description

pureData

Keeps only the properties a model actually declares.