Class: Query

Query


new Query(collection)

A class used by the Collection class to build queries to be executed against the collection's data. An instance of Query is returned by Collection#query. Query instances are typically short-lived.

import {Query} from 'js-data'
Parameters:
Name Type Description
collection Collection

The collection on which this query operates.

Source:

Extends

Members


<static> ops :Object

TODO

Type:
  • Object
Source:

_listeners :Object

Event listeners attached to this Component. Do not modify. Use Component#on and Component#off instead.

Type:
  • Object
Inherited From:
Source:

collection :Collection

The Collection on which this query operates.

Type:
Source:

data :Array

The current data result of this query.

Type:
  • Array
Source:

Methods


between(leftKeys, rightKeys, opts)

Find all entities between two boundaries.

Get the users ages 18 to 30

const users = query.between(18, 30, { index: 'age' }).run()

Same as above

const users = query.between([18], [30], { index: 'age' }).run()
Parameters:
Name Type Argument Description
leftKeys Array

Keys defining the left boundary.

rightKeys Array

Keys defining the right boundary.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Default Description
index string <optional>

Name of the secondary index to use in the query. If no index is specified, the main index is used.

leftInclusive boolean <optional>
true

Whether to include entities on the left boundary.

rightInclusive boolean <optional>
false

Whether to include entities on the left boundary.

limit boolean <optional>

Limit the result to a certain number.

offset boolean <optional>

The number of resulting entities to skip.

Source:
Returns:

A reference to itself for chaining.

Type
Query

compare(orderBy, index, a, b)

The comparison function used by the Query class.

Parameters:
Name Type Description
orderBy Array

An orderBy clause used for sorting and sub-sorting.

index number

The index of the current orderBy clause being used.

a *

The first item in the comparison.

b *

The second item in the comparison.

Source:
Returns:

-1 if b should preceed a. 0 if a and b are equal. 1 if a should preceed b.

Type
number

dbg()

TODO

Inherited From:
Source:

emit(event, args)

Trigger an event on this Component.

Parameters:
Name Type Argument Description
event string

Name of event to emit.

args * <optional>
<repeatable>

Arguments to pass to any listeners.

Inherited From:
Source:

evaluate(value, op, predicate)

Predicate evaluation function used by the Query class.

Parameters:
Name Type Description
value *

The value to evaluate.

op string

The operator to use in this evaluation.

predicate *

The predicate to use in this evaluation.

Source:
Returns:

Whether the value passed the evaluation or not.

Type
boolean

filter(queryOrFn, thisArg)

Find the entity or entities that match the provided query or pass the provided filter function.

Example

Get the draft posts created less than three months

const posts = query.filter({
  where: {
    status: {
      '==': 'draft'
    },
    created_at_timestamp: {
      '>=': (new Date().getTime() - (1000 * 60 * 60 * 24 * 30 * 3)) // 3 months ago
    }
  }
}).run()

Use a custom filter function

const posts = query.filter(function (post) {
  return post.isReady()
}).run()
Parameters:
Name Type Argument Default Description
queryOrFn Object | function <optional>
{}

Selection query or filter function.

thisArg function <optional>

Context to which to bind queryOrFn if queryOrFn is a function.

Source:
Returns:

A reference to itself for chaining.

Type
Query

forEach(forEachFn, thisArg)

Iterate over all entities.

Parameters:
Name Type Argument Description
forEachFn function

Iteration function.

thisArg * <optional>

Context to which to bind forEachFn.

Source:
Returns:

A reference to itself for chaining.

Type
Query

get(keyList, opts)

Find the entity or entities that match the provided key.

Example

Get the entity whose primary key is 25

const entities = query.get(25).run()

Same as above

const entities = query.get([25]).run()

Get all users who are active and have the "admin" role

const activeAdmins = query.get(['active', 'admin'], {
  index: 'activityAndRoles'
}).run()

Get all entities that match a certain weather condition

const niceDays = query.get(['sunny', 'humid', 'calm'], {
  index: 'weatherConditions'
}).run()
Parameters:
Name Type Argument Description
keyList Array

Key(s) defining the entity to retrieve. If keyList is not an array (i.e. for a single-value key), it will be wrapped in an array.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Description
string string <optional>

Name of the secondary index to use in the query. If no index is specified, the main index is used.

Source:
Returns:

A reference to itself for chaining.

Type
Query

getAll(keyList, opts)

Find the entity or entities that match the provided keyLists.

Example

Get the posts where "status" is "draft" or "inReview"

const posts = query.getAll('draft', 'inReview', { index: 'status' }).run()

Same as above

const posts = query.getAll(['draft'], ['inReview'], { index: 'status' }).run()
Parameters:
Name Type Argument Description
keyList Array <optional>
<repeatable>

Provide one or more keyLists, and all entities matching each keyList will be retrieved. If no keyLists are provided, all entities will be returned.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Description
index string <optional>

Name of the secondary index to use in the query. If no index is specified, the main index is used.

Source:
Returns:

A reference to itself for chaining.

Type
Query

getData()

Return the current data result of this query.

Source:
Returns:

The data in this query.

Type
Array

limit(num)

Limit the result.

Example

Get only the first 10 draft posts

const posts = query.get('draft', { index: 'status' }).limit(10).run()
Parameters:
Name Type Description
num number

The maximum number of entities to keep in the result.

Source:
Returns:

A reference to itself for chaining.

Type
Query

log()

TODO

Inherited From:
Source:

map(mapFn, thisArg)

Apply a mapping function to the result data.

Parameters:
Name Type Argument Description
mapFn function

Mapping function.

thisArg * <optional>

Context to which to bind mapFn.

Source:
Returns:

A reference to itself for chaining.

Type
Query

mapCall(funcName)

Return the result of calling the specified function on each item in this collection's main index.

Parameters:
Name Type Description
funcName string

Name of function to call

Source:
Returns:

A reference to itself for chaining.

Type
Query

off(event, listener)

Remove an event listener from this Component. If no listener is provided, then all listeners for the specified event will be removed. If no event is specified then all listeners for all events will be removed.

Parameters:
Name Type Argument Description
event string <optional>

Name of event to unsubsribe to.

listener function <optional>

Listener to remove.

Inherited From:
Source:

on(event, listener, ctx)

Register a new event listener on this Component.

Parameters:
Name Type Argument Description
event string

Name of event to subsribe to.

listener function

Listener function to handle the event.

ctx * <optional>

Optional content in which to invoke the listener.

Inherited From:
Source:

run()

Complete the execution of the query and return the resulting data.

Source:
Returns:

The result of executing this query.

Type
Array

skip(num)

Skip a number of results.

Example

Get all but the first 10 draft posts

const posts = query.get('draft', { index: 'status' }).skip(10).run()
Parameters:
Name Type Description
num number

The number of entities to skip.

Source:
Returns:

A reference to itself for chaining.

Type
Query