Class ydn.db.Query

The class Query  represents a query for retrieving entities from a storage instance. query

Query is not directly instantiated, but instead obtained from from method of a storage instance.

var q = db.from('store name');
q.list(function(x) {
  console.log(x)
});
Methods
count()
Count number of records.
db.from('author').where('company', '=', 'Google').count().done(function(cnt) {
  console.log('Number of authors from Google:' + cnt);
});
Returns:
{!ydn.db.Request} Returns a request object.
done: {number} Return number of records.
fail: {Error} Resulting error object on executing the query.

list(limit)
Execute query into array.

List method is the primary way of executing an query.

db.from('author').where('hobby', '=', 'camping').list(10).done(function(authors) {
  console.log(authors); // 10 campers
});

Although a query is immutable, query has state with cursor position. A cursor position is defined by effective key and primary key. When an query is execute again, it resume from its cursor position. The following query will list next 10 author having 'camping' hobby.

db.from('author').where('hobby', '=', 'camping').list(10).done(function(authors) {
  console.log(authors); // next 10 campers
});
Parameters:
{number=} limit
Optional. Limit number of results, default to 100.
Returns:
{!ydn.db.Request} Returns a request object.
done: {!Array} Return list of records.
fail: {!Array.<Error>} Resulting error object on executing the query.

open(callback, scope)
Open iterator to perform iteration process.
Parameters:
{!function(!ydn.db.ICursor): (Object|boolean|IDBKey|undefined)} next_callback
Callback function to receive stream of cursors. Optionally return next cursor advancement.
{*=} scope
Optional. An optional scope to call the callback in.
Returns:
{!ydn.db.Request} Returns a request object.
done: {} Result not defined.
fail: {Error} Resulting error object on executing the query.

order(field_name)
Build a query using given sort order.
db.from('author').order('born').list(10).done(function(user) {
  console.log(user); // 10 youngest authors
});
Parameters:
{string|!Array.<string>} field_name
Field name to be ordered in query result. By default ordering is in ascending direction.
Returns:
{!ydn.db.Query} Returns a newly created query object.

patch(arg1, arg2)
Update field(s) of record value.
db.from('author', '=', 1).patch({company: 'Facebook'});

Or using two arguments

db.from('author').where('company', '=', 'Google').patch('company', 'Facebook');
Parameters:
{!Object|string|!Array.<string>} arg1
If only one argument is given, it is an object. If optional second argument is given, this is field name or array of field names.
{boolean=} arg2
Optional. Corresponding field value(s).
Returns:
{!ydn.db.Request} Returns a request object.
done: {number} Result not defined.
fail: {Error} Resulting error object on executing the query.

reverse()
Build a query in reverse ordering of result set.
Returns:
{!ydn.db.Query} Returns a newly created query object.

unique()
Build an query with distinct primary key record.
Returns:
{!ydn.db.Query} Returns a newly created query object.

where(field_name, op, value, op2, value2)
Build a query with where clause.
db.from('author').where('first', '^', 'B').list(10).done(function(authors) {
  console.log(authors); // list 10 authors having first name started with 'B'
});
Parameters:
{string} field_name
Field name.
{string} op
Operator symbol. One of '<', '<=', '=', '>', '>=', '^'. The last operator is for string value having starts with.
{IDBKey} value
Left value for the operator.
{string=} op2
Optional. Operator symbol.
{IDBKey=} value2
Optional. Left value for the operator.
Returns:
{!ydn.db.Query} Returns a newly created query object.