Skip to content

Commit

Permalink
datastore: default ordering to ascending. fixes googleapis#134.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 25, 2014
1 parent bcba835 commit 079cdeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
20 changes: 10 additions & 10 deletions lib/datastore/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,32 +119,32 @@ Query.prototype.hasAncestor = function(key) {
};

/**
* Sort the results by a property name ascendingly or descendingly.
* Sort the results by a property name ascendingly or descendingly. By default,
* an ascending sort order will be used.
*
* *Reference: {@link http://goo.gl/mfegFR}*
*
* @param {string} order - Operator (+, -) + property to order by.
* @param {string} property - Optional operator (+, -) and property to order by.
* @return {module:datastore/query}
*
* @example
* ```js
* // Sort by size ascendingly.
* var companiesAscending = companyQuery.order('+size');
* var companiesAscending = companyQuery.order('size');
*
* // Sort by size descendingly.
* var companiesDescending = companyQuery.order('-size');
* ```
*/
Query.prototype.order = function(order) {
Query.prototype.order = function(property) {
var q = util.extend(this, new Query());
var sign = order[0];
var fieldName = order.substr(1);
if (sign !== '-' && sign !== '+' ) {
throw new Error(
'Invalid order pattern. Expected "+fieldName" or "-fieldName".');
var sign = '+';
if (property[0] === '-' || property[0] === '+') {
sign = property[0];
property = property.substr(1);
}
q.orders = q.orders || [];
q.orders.push({ name: fieldName, sign: sign });
q.orders.push({ name: property, sign: sign });
return q;
};

Expand Down
8 changes: 4 additions & 4 deletions test/datastore/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ describe('Query', function() {
assert.equal(query.orders[1].sign, '-');
});

it('should throw error is invalid sort sign is provided', function() {
assert.throws(function() {
new Query(['kind1']).order('*name');
}, /Invalid order pattern/);
it('should default ordering to ascending', function() {
var query = new Query(['kind1']).order('name');
assert.equal(query.orders[0].name, 'name');
assert.equal(query.orders[0].sign, '+');
});

it('should provide pagination with offset and limit', function() {
Expand Down

0 comments on commit 079cdeb

Please sign in to comment.