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 [count](#QuerySet+count),
[toRefArray](#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.
Kind: global class
- QuerySet
new QuerySet(modelClass, clauses, [opts])
withModels
withRefs
toRefArray()
⇒ Array.toModelArray()
⇒ Array.count()
⇒ numberexists()
⇒ Booleanat(index)
⇒ Model ⎮ undefinedfirst()
⇒ Modellast()
⇒ Modelall()
⇒ QuerySetfilter(lookupObj)
⇒ QuerySetexclude(lookupObj)
⇒ QuerySetorderBy(iteratees, [orders])
⇒ QuerySetupdate(mergeObj)
⇒ undefineddelete()
⇒ undefinedmap()
forEach()
new QuerySet(modelClass, clauses, [opts])
Creates a QuerySet. The constructor is mainly for internal use;
You should access QuerySet instances from [Model](Model).
Param | Type | Description |
---|---|---|
modelClass | Model | the model class of objects in this QuerySet. |
clauses | Array. | query clauses needed to evaluate the set. |
[opts] | Object | additional options |
withModels
withModels
Deprecated
Kind: instance property of QuerySet
withRefs
withRefs
Deprecated
Kind: instance property of QuerySet
toRefArray()
⇒ Array.<Object>
Returns an array of the plain objects represented by the QuerySet.
The plain objects are direct references to the store.
Kind: instance method of QuerySet
Returns: Array.
toModelArray()
⇒ Array.<Model>
Returns an array of [Model](Model) instances represented by the QuerySet.
Kind: instance method of QuerySet model instances represented by the QuerySet
Returns: Array.
count()
⇒ number
Returns the number of [Model](Model) instances represented by the QuerySet.
Kind: instance method of QuerySet
Returns: number -
length of the QuerySet
exists()
⇒ Boolean
Checks if the [QuerySet](#QuerySet) instance has any records matching the query
in the database.
Kind: instance method of QuerySet
Returns: Boolean -
true
if the QuerySet instance contains entities, else false
.
at(index)
⇒ Model,undefined
Returns the [Model](Model) instance at index index
in the [QuerySet](#QuerySet) instance if
withRefs
flag is set to false
, or a reference to the plain JavaScript
object in the model state if true
.
Kind: instance method of QuerySet
Returns: Model ⎮ undefined -
a Model instance at index
index
in the QuerySet instance,
or undefined if the index is out of bounds.
Param | Type | Description |
---|---|---|
index | number | index of the model instance to get |
first()
⇒ Model
Returns the [Model](Model) instance at index 0 in the [QuerySet](#QuerySet) instance.
Kind: instance method of QuerySet
last()
⇒ Model
Returns the [Model](Model) instance at index QuerySet.count() - 1
Kind: instance method of QuerySet
all()
⇒ QuerySet
Returns a new [QuerySet](#QuerySet) instance with the same entities.
Kind: instance method of QuerySet
Returns: QuerySet -
a new QuerySet with the same entities.
filter(lookupObj)
⇒ QuerySet
Returns a new [QuerySet](#QuerySet) instance with entities that match properties in lookupObj
.
Kind: instance method of QuerySet
Returns: QuerySet -
a new QuerySet instance with objects that passed the filter.
Param | Type | Description |
---|---|---|
lookupObj | Object | the properties to match objects with. Can also be a function. |
exclude(lookupObj)
⇒ QuerySet
Returns a new [QuerySet](#QuerySet) instance with entities that do not match
properties in lookupObj
.
Kind: instance method of QuerySet
Returns: QuerySet -
a new QuerySet instance with objects that did not pass the filter.
Param | Type | Description |
---|---|---|
lookupObj | Object | the properties to unmatch objects with. Can also be a function. |
orderBy(iteratees, [orders])
⇒ QuerySet
Returns a new [QuerySet](#QuerySet) instance with entities ordered by iteratees
in ascending
order, unless otherwise specified. Delegates to Lodash orderBy.
Kind: instance method of QuerySet
Returns: QuerySet -
a new QuerySet with objects ordered by iteratees
.
Param | Type | Description | ||
---|---|---|---|---|
iteratees | Array. | an array where each item can be a string or a | ||
[orders] | Array.<(Boolean | 'asc' | 'desc')> | the sort orders of |
update(mergeObj)
⇒ undefined
Records an update specified with mergeObj
to all the objects
in the [QuerySet](#QuerySet) instance.
Kind: instance method of QuerySet
Param | Type | Description |
---|---|---|
mergeObj | Object | an object to merge with all the objects in this |
delete()
⇒ undefined
Records a deletion of all the objects in this [QuerySet](#QuerySet) instance.
Kind: instance method of QuerySet
map()
map()
Deprecated
Kind: instance method of QuerySet
forEach()
forEach()
Deprecated
Kind: instance method of QuerySet