Class: QuerySet

QuerySet

This class is used to build and make queries to the database and operating the resulting set (such as updating attributes or deleting the records).

The queries are built lazily. For example:

const qs = Book.all()
    .filter(book => book.releaseYear > 1999)
    .orderBy('name');

Doesn't execute a query. The query is executed only when you need information from the query result, such as QuerySet#count, QuerySet#toRefArray. After the query is executed, the resulting set is cached in the QuerySet instance.

QuerySet instances also return copies, so chaining filters doesn't mutate the previous instances.

Constructor

new QuerySet(modelClass, clauses, optsopt)

Creates a QuerySet. The constructor is mainly for internal use; You should access QuerySet instances from Model.

Parameters:
Name Type Attributes Description
modelClass Model

the model class of objects in this QuerySet.

clauses Array.<any>

query clauses needed to evaluate the set.

opts Object <optional>

additional options

Source:

Methods

all() → {QuerySet}

Returns a new QuerySet instance with the same entities.

Source:
Returns:

a new QuerySet with the same entities.

Type
QuerySet

at(index) → {Model|undefined}

Returns the Model instance at index index in the QuerySet instance if withRefs flag is set to false, or a reference to the plain JavaScript object in the model state if true.

Parameters:
Name Type Description
index number

index of the model instance to get

Source:
Returns:

a Model instance at index index in the QuerySet instance, or undefined if the index is out of bounds.

Type
Model | undefined

count() → {number}

Returns the number of Model instances represented by the QuerySet.

Source:
Returns:

length of the QuerySet

Type
number

delete() → {undefined}

Records a deletion of all the objects in this QuerySet instance.

Source:
Returns:
Type
undefined

exclude(lookupObj) → {QuerySet}

Returns a new QuerySet instance with entities that do not match properties in lookupObj.

Parameters:
Name Type Description
lookupObj Object

the properties to unmatch objects with.

Source:
Returns:

a new QuerySet instance with objects that passed the filter.

Type
QuerySet

exists() → {Boolean}

Checks if the QuerySet instance has any records matching the query in the database.

Source:
Returns:

true if the QuerySet instance contains entities, else false.

Type
Boolean

filter(lookupObj) → {QuerySet}

Returns a new QuerySet instance with entities that match properties in lookupObj.

Parameters:
Name Type Description
lookupObj Object

the properties to match objects with.

Source:
Returns:

a new QuerySet instance with objects that passed the filter.

Type
QuerySet

first() → {Model}

Returns the Model instance at index 0 in the QuerySet instance.

Source:
Returns:
Type
Model

last() → {Model}

Returns the Model instance at index QuerySet.count() - 1

Source:
Returns:
Type
Model

orderBy(iteratees, ordersopt) → {QuerySet}

Returns a new QuerySet instance with entities ordered by iteratees in ascending order, unless otherwise specified. Delegates to lodash.orderBy.

Parameters:
Name Type Attributes Description
iteratees Array.<string> | Array.<function()>

an array where each item can be a string or a function. If a string is supplied, it should correspond to property on the entity that will determine the order. If a function is supplied, it should return the value to order by.

orders Array.<Boolean> <optional>

the sort orders of iteratees. If unspecified, all iteratees will be sorted in ascending order. true and 'asc' correspond to ascending order, and false and 'desc to descending order.

Source:
Returns:

a new QuerySet with objects ordered by iteratees.

Type
QuerySet

toModelArray() → {Array.<Model>}

Returns an array of Model instances represented by the QuerySet.

Source:
Returns:

model instances represented by the QuerySet

Type
Array.<Model>

toRefArray() → {Array.<Object>}

Returns an array of the plain objects represented by the QuerySet. The plain objects are direct references to the store.

Source:
Returns:

references to the plain JS objects represented by the QuerySet

Type
Array.<Object>

update(mergeObj) → {undefined}

Records an update specified with mergeObj to all the objects in the QuerySet instance.

Parameters:
Name Type Description
mergeObj Object

an object to merge with all the objects in this queryset.

Source:
Returns:
Type
undefined