From d65e2b406fc4f024cfb3cfa41dcfed9da6e12f2b Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Mon, 25 Aug 2014 15:47:20 -0400 Subject: [PATCH] datastore: default ordering to ascending. fixes #134. --- lib/datastore/query.js | 20 ++++++++++---------- regression/datastore.js | 8 ++++---- test/datastore/query.js | 8 ++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/datastore/query.js b/lib/datastore/query.js index 5cbe7d055ece..35f0168c0c02 100644 --- a/lib/datastore/query.js +++ b/lib/datastore/query.js @@ -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; }; diff --git a/regression/datastore.js b/regression/datastore.js index 9e36c65f8ad7..2d1c6076b9e9 100644 --- a/regression/datastore.js +++ b/regression/datastore.js @@ -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); @@ -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); @@ -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); diff --git a/test/datastore/query.js b/test/datastore/query.js index 67d48d1df014..ab8883978d3c 100644 --- a/test/datastore/query.js +++ b/test/datastore/query.js @@ -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() {