diff --git a/README.md b/README.md index 8578c97866b..cf108558e9c 100644 --- a/README.md +++ b/README.md @@ -193,8 +193,8 @@ var q = ds.createQuery('Child') You can sort the results by a property name ascendingly or descendingly. ~~~~ js -// sorts by size ascendingly. -var q = ds.createQuery('Company').order('+size'); +// sorts by size ascendingly. (default) +var q = ds.createQuery('Company').order('size'); // sorts by size descendingly. var q = ds.createQuery('Company').order('-size'); diff --git a/lib/datastore/query.js b/lib/datastore/query.js index 5cbe7d055ec..35f0168c0c0 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 9e36c65f8ad..2d1c6076b9e 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 67d48d1df01..ab8883978d3 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() {