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.

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:

collection :Collection

The collection on which this query operates.

Type:
Source:

data :Array

The 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

dbg()

Inherited From:
Source:

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()

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

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