--- layout: docs title: ModelQuery edit_url: "https://github.com/nylas/N1/blob/master/src/flux/models/query.coffee" ---
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.labels.contains('label-id')])
.order([Thread.attributes.lastMessageReceivedTimestamp.descending()])
.limit(100).offset(50)
.then (threads) ->
# array of threads
Add one or more where clauses to the query
This method is chainable.
Argument | Description |
---|---|
matchers |
An Array of Matcher objects that add where clauses to the underlying query. |
Include specific joined data attributes in result objects.
This method is chainable.
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. |
Include all of the available joined data attributes in returned models.
This method is chainable.
Apply a sort order to the query.
This method is chainable.
Argument | Description |
---|---|
orders |
An Array of one or more SortOrder objects that determine the sort order of returned models. |
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 the number of query results.
This method is chainable.
Argument | Description |
---|---|
limit |
Number The number of models that should be returned. |
This method is chainable.
Argument | Description |
---|---|
offset |
Number The start offset of the query. |
Set the count
flag - instead of returning inflated models,
the query will return the result COUNT
.
This method is chainable.
Short-hand syntax that calls run().then(fn) with the provided function.
Return Values |
---|
Returns a Promise that resolves with the Models returned by the query, or rejects with an error from the Database layer. |
Return Values |
---|
Returns a Promise that resolves with the Models returned by the query, or rejects with an error from the Database layer. |