Skip to content

Commit

Permalink
datastore: default ordering to ascending. fixes #134.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 25, 2014
1 parent bcba835 commit d65e2b4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 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 regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('datastore', function() {
});

it('should order queries', function(done) {
var q = ds.createQuery('Character').order('+appearances');
var q = ds.createQuery('Character').order('appearances');
ds.runQuery(q, function(err, entities) {
assert.ifError(err);
assert.equal(entities[0].data.name, characters[0].name);
Expand Down Expand Up @@ -313,7 +313,7 @@ describe('datastore', function() {
var q = ds.createQuery('Character')
.offset(2)
.limit(3)
.order('+appearances');
.order('appearances');
ds.runQuery(q, function(err, entities, secondQuery) {
assert.ifError(err);
assert.equal(entities.length, 3);
Expand All @@ -332,13 +332,13 @@ describe('datastore', function() {
var q = ds.createQuery('Character')
.offset(2)
.limit(2)
.order('+appearances');
.order('appearances');
ds.runQuery(q, function(err, entities, nextQuery) {
assert.ifError(err);
var startCursor = nextQuery.startVal;
var cursorQuery =
ds.createQuery('Character')
.order('+appearances')
.order('appearances')
.start(startCursor);
ds.runQuery(cursorQuery, function(err, secondEntities) {
assert.ifError(err);
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 d65e2b4

Please sign in to comment.