QueryResult<T>
A QueryResult is a list of key-value pairs that has been generated as a result of a Query
.
It contains the result of a query at a specific time.
The keys are tags (e.g. myTag
) that correspond to a CloudObject
(e.g. myCloudObject
).
When the Query contains more than one Query.andReturn() clause,
keys are composite tags (e.g. 'tag1.tag2.tag3'
) and values are tuples of CloudObjects
(e.g. [cloudObj1, cloudObj2, cloudObj3]
).
Such a QueryResult can be obtained via:
Query.instancesOf(myModel1).andReturn.follow(myRelation1).andReturn.follow(myRelation2).andReturn().execute().
When a query is subscribed, the returned observable generates multiple QueryResults over time.
A QueryResult can be easily manipulated and transformed to a standard Array.
Type Parameters
Type Parameter |
---|
T extends CloudObject | CloudObject [] |
Constructors
new QueryResult()
new QueryResult<T>(): QueryResult<T>
Returns
QueryResult
<T
>
Methods
empty()
static empty(): QueryResult<any>
Returns
QueryResult
<any
>
empty QueryResult
[iterator]()
iterator: IterableIterator<[string, T]>
Symbol iterator for for…in
syntactic sugar
Returns
IterableIterator
<[string
, T
]>
iterator over the key-values pairs of this QueryResult
concat()
concat(...others): QueryResult<T>
Return a new QueryResult that contains the concatenation of two or more QueryResults.
Concatenating two QueryResults results in a new QueryResult with the following:
- the keys of the QueryResults are concatenated
- the values of the QueryResults are concatenated
- the arrays returned by QueryResult.getAdded are concatenated
- the arrays returned by QueryResult.getRemoved are concatenated
Example :
const result1 = Query.fromInstances(myModel1).executeFromCache();
// result1 is {tag1: cloudObj1, tag2: cloudObj2}
const result2 = Query.fromInstances(myModel2).executeFromCache();
// result2 is {tag3: cloudObj3}
const concatenatedResult = result2.concat(result1);
// concatenatedResult is {tag3: cloudObj3, tag1: cloudObj1, tag2: cloudObj2}
Parameters
Parameter | Type | Description |
---|---|---|
...others | QueryResult <T >[] | QueryResults to append to this QueryResult |
Returns
QueryResult
<T
>
concatenated query results
entries()
entries(): IterableIterator<[string, T]>
Get an iterable iterator over the key-value pairs of this QueryResult
Returns
IterableIterator
<[string
, T
]>
iterator
filter()
filter(predicate): T[]
Returns an array containing the values matching the provided filter predicate.
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (entry , index ) => boolean | filter Predicate |
Returns
T
[]
array of matching values
find()
find(predicate): T
Returns the first value matching the predicate, null
otherwise.
find
calls predicate
once for each element of the array until it returns true.
If such an element is found, find immediately returns that element value.
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (entry , index ) => boolean |
Returns
T
the first value evaluating to true or null
forEach()
forEach(callback): void
Execute the specified callback function for each value of this QueryResult
Parameters
Parameter | Type | Description |
---|---|---|
callback | (entry , index ) => void |
Returns
void
get()
get(key): T
Get the value corresponding to a given key
Parameters
Parameter | Type | Description |
---|---|---|
key | string | tag or composite tag |
Returns
T
matching tuple or null
getAdded()
getAdded(): T[]
Query Results, when used in a subscription, can be used to observe changes compare to the previous QueryResult. This method returns the values that have added from the previous QueryResult.
Returns
T
[]
array of added values (CloudObjects or tuples of CloudObjects)
getAt()
getAt(index): T
Get the value at the specified index
Parameters
Parameter | Type | Description |
---|---|---|
index | number |
Returns
T
value at position index
or null
if index is out of bounds
getFirst()
getFirst(): T
Get the first value
Returns
T
first value or null
if empty
getRemoved()
getRemoved(): string[]
Query Results, when used in a subscription, can be used to observe changes compare to the previous QueryResult. This method returns the values the keys (eg: tags) of values that have been removed from the previous QueryResult.
Returns
string
[]
array of removed keys (tags or tags concatenation (tuples))
has()
has(key): boolean
Check whether a key / value is contained in this QueryResult
.
Returns true if the cloud array contains an element with the specified key.
If no corresponding key is found, returns true if the cloud array contains an element with the specified value.
Parameters
Parameter | Type | Description |
---|---|---|
key | string | T | the value to search for |
Returns
boolean
true if the value key
is found
indexOf()
indexOf(key): number
Get index of a value in the keys or values of this QueryResult
.
If the key is a string, retrieve the index from the string
keys of this QueryResult
.
If the key is not a string, get the index from the CloudObject
tuples
Parameters
Parameter | Type | Description |
---|---|---|
key | string | T | the value to find the index of |
Returns
number
index of the argument value or -1 if the value is not found
keys()
keys(): IterableIterator<string>
Get an iterable iterator over the keys of this QueryResult
Returns
IterableIterator
<string
>
iterator
map()
map<S>(callback): S[]
Execute the specified callback function for each value of this QueryResult
.
It returns a new array that contains the results of the callback execution.
Type Parameters
Type Parameter |
---|
S |
Parameters
Parameter | Type | Description |
---|---|---|
callback | (entry , index ) => S |
Returns
S
[]
new array of transformed values
pop()
pop(): T[]
Returns the QueryResult values as an array whose last value has been removed.
If QueryResult.size is 0 or 1, this method returns an empty array.
This operation returns a new array and does not modify this QueryResult.
Returns
T
[]
new values array with last element removed
push()
push(...object): T[]
Returns an array equivalent to this.toArray().push(...object)
This operation returns a new array and does not modify this QueryResult.
Parameters
Parameter | Type | Description |
---|---|---|
...object | T [] | new values to push in the values array. |
Returns
T
[]
new values array with new elements appended
reduce()
reduce<S>(reducer, initial): S
Return a reduced value for this QueryResult.
The specified reducer
callback reduces the QueryResult values. Similar to Array.reduce()
.
Type Parameters
Type Parameter |
---|
S |
Parameters
Parameter | Type | Description |
---|---|---|
reducer | (accumulator , entry , index ) => S | callback |
initial | S | initial value of the accumulator |
Returns
S
accumulated result applying the reducer
function iteratively
shift()
shift(): T[]
Returns the QueryResult values as an array whose first value has been removed.
If QueryResult.size is 0 or 1, this method returns an empty array.
This operation returns a new array and does not modify this QueryResult.
Returns
T
[]
new values array with first element removed
size()
size(): number
Get the number of key-value pairs this QueryResult
contains
Returns
number
size of this QueryResult
sort()
sort(comparator): QueryResult<T>
Return a sorted copy of this QueryResult.
The specified comparator
used to determine the order of the QueryResult key-value pairs.
It compares two QueryResult values and should return a negative number if the first value shall be present before the second, zero if they're at the same position, and a positive
number otherwise.
Parameters
Parameter | Type | Description |
---|---|---|
comparator | (b1 , b2 ) => number | comparator returning a negative, zero or positive number |
Returns
QueryResult
<T
>
sorted copy of this QueryResult
toArray()
toArray(): T[]
Get an array containing the values of this QueryResult
Returns
T
[]
array of this QueryResult
's values
values()
values(): IterableIterator<T>
Get an iterable iterator over the values of this QueryResult
Returns
IterableIterator
<T
>
iterator