ModelQuery
ModelQuery exposes an ActiveRecord-style syntax for building database queries that return models and model counts. Model queries are returned from the factory methods {RxDatabase::find}, {RxDatabase::findBy}, {RxDatabase::findAll}, and {RxDatabase::count}, and are the primary interface for retrieving data from the app's local cache.
ModelQuery does not allow you to modify the local cache. To create, update or delete items from the local cache, see {RxDatabase::inTransaction} and {DatabaseTransaction::persistModel}.
Simple Example:* Fetch a thread
const query = db.find(Thread, '123a2sc1ef4131');
query.then((thread) {
// thread or null
});
Advanced Example:* Fetch 50 threads in the inbox, in descending order
const query = db.findAll(Thread);
query.where([Thread.attributes.categories.contains('label-id')])
.order([Thread.attributes.lastMessageReceivedTimestamp.descending()])
.limit(100).offset(50)
.then((threads) {
// array of threads
});
Constructor Summary
Public Constructor | ||
public |
constructor(class: Model, database: RxDatabase) |
Method Summary
Public Methods | ||
public |
clone(): Query |
|
public |
count(): * Set the |
|
public |
distinct(): * |
|
public |
finalize(): * |
|
public |
formatResult(inflated: *): * |
|
public |
idsOnly(): * |
|
public |
include(attr: AttributeJoinedData): * Include specific joined data attributes in result objects. |
|
public |
includeAll(): * Include all of the available joined data attributes in returned models. |
|
public |
inflateResult(result: *): * |
|
public |
Limit the number of query results. |
|
public |
matcherValueForModelKey(key: *): * |
|
public |
matchers(): * |
|
public |
matchersFlattened(): * |
|
public |
objectClass(): * |
|
public |
observe(objectPattern: {"allowQueryChanges": *, "name": *}): * |
|
public |
|
|
public |
one(): * Set the |
|
public |
Apply a sort order to the query. |
|
public |
orderSortDescriptors(): * |
|
public |
A convenience method for setting both limit and offset given a desired page size. |
|
public |
range(): * |
|
public |
|
|
public |
search(query: *): * |
|
public |
|
|
public |
Short-hand syntax that calls run().then(fn) with the provided function. |
|
public |
Add one or more where clauses to the query This method is chainable. |
|
public |
whereAny(matchers: *): * |
Public Constructors
public constructor(class: Model, database: RxDatabase) source
Params:
Name | Type | Attribute | Description |
class | Model | A {Model} class to query |
|
database | RxDatabase |
|
An optional reference to a {RxDatabase} the query will be executed on. |
Public Methods
public count(): * source
Set the count
flag - instead of returning inflated models,
the query will return the result COUNT
.
This method is chainable.
Return:
* |
public formatResult(inflated: *): * source
Params:
Name | Type | Attribute | Description |
inflated | * |
Return:
* |
public include(attr: AttributeJoinedData): * source
Include specific joined data attributes in result objects.
Params:
Name | Type | Attribute | Description |
attr | AttributeJoinedData | Attribute that you want to be populated in the returned models. Note: This results in a LEFT OUTER JOIN. See {AttributeJoinedData} for more information. This method is chainable. |
Return:
* |
public includeAll(): * source
Include all of the available joined data attributes in returned models.
This method is chainable.
Return:
* |
public limit(limit: Number): * source
Limit the number of query results.
Params:
Name | Type | Attribute | Description |
limit | Number | The number of models that should be returned. This method is chainable. |
Return:
* |
public matcherValueForModelKey(key: *): * source
Params:
Name | Type | Attribute | Description |
key | * |
Return:
* |
public observe(objectPattern: {"allowQueryChanges": *, "name": *}): * source
Params:
Name | Type | Attribute | Description |
objectPattern | {"allowQueryChanges": *, "name": *} |
|
Return:
* |
public offset(offset: Number): * source
Params:
Name | Type | Attribute | Description |
offset | Number | The start offset of the query. This method is chainable. |
Return:
* |
public one(): * source
Set the singular
flag - only one model will be returned from the
query, and a LIMIT 1
clause will be used.
This method is chainable.
Return:
* |
public order(ordersOrOrder: Array): * source
Apply a sort order to the query.
Params:
Name | Type | Attribute | Description |
ordersOrOrder | Array | An {Array} of one or more {SortOrder} objects that determine the sort order of returned models. This method is chainable. |
Return:
* |
public page(start: *, end: *, pageSize: number, pagePadding: number): * source
A convenience method for setting both limit and offset given a desired page size.
Return:
* |
public run(): Promise source
Return:
Promise | A Promise that resolves with the Models returned by the query, or rejects with an error from the Database layer. |
public then(next: *): Promise source
Short-hand syntax that calls run().then(fn) with the provided function.
Params:
Name | Type | Attribute | Description |
next | * |
Return:
Promise | A promise that resolves with the Models returned by the query, or rejects with an error from the Database layer. |