ModelQuery

Summary

ModelQuery exposes an ActiveRecord-style syntax for building database queries that return models and model counts. Model queries are returned from the factory methods DatabaseStore::find, DatabaseStore::findBy, DatabaseStore::findAll, and DatabaseStore::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 DatabaseStore::persistModel and DatabaseStore::unpersistModel.

Simple Example: Fetch a thread

query = DatabaseStore.find(Thread, '123a2sc1ef4131')
query.then (thread) ->
  // thread or null

Advanced Example: Fetch 50 threads in the inbox, in descending order

query = DatabaseStore.findAll(Thread)
query.where([Thread.attributes.categories.contains('label-id')])
     .order([Thread.attributes.lastMessageReceivedTimestamp.descending()])
     .limit(100).offset(50)
     .then (threads) ->
  // array of threads

Instance Methods

where(matchers)

Add one or more where clauses to the query This method is chainable.

Parameters

Argument Description
matchers An {Array} of {Matcher} objects that add where clauses to the underlying query.

include(attr)

Include specific joined data attributes in result objects. This method is chainable.

Parameters

Argument Description
attr A {AttributeJoinedData} that you want to be populated in the returned models. Note: This results in a LEFT OUTER JOIN. See {AttributeJoinedData} for more information.

includeAll()

Include all of the available joined data attributes in returned models. This method is chainable.

order(orders)

Apply a sort order to the query. This method is chainable.

Parameters

Argument Description
orders An {Array} of one or more {SortOrder} objects that determine the sort order of returned models.

one()

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.

limit(limit)

Limit the number of query results. This method is chainable.

Parameters

Argument Description
limit {Number} The number of models that should be returned.

offset(offset)

This method is chainable.

Parameters

Argument Description
offset {Number} The start offset of the query.

page()

A convenience method for setting both limit and offset given a desired page size.

count()

Set the `count` flag - instead of returning inflated models, the query will return the result `COUNT`. This method is chainable.

then()

Short-hand syntax that calls run().then(fn) with the provided function.

Returns

Return Values
Returns a {Promise} that resolves with the Models returned by the query, or rejects with an error from the Database layer.

run()

Returns
Return Values
Returns a {Promise} that resolves with the Models returned by the query, or rejects with an error from the Database layer.

results matching ""

    No results matching ""