Skip to content

Commit d65e2b4

Browse files
datastore: default ordering to ascending. fixes googleapis#134.
1 parent bcba835 commit d65e2b4

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

lib/datastore/query.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -119,32 +119,32 @@ Query.prototype.hasAncestor = function(key) {
119119
};
120120

121121
/**
122-
* Sort the results by a property name ascendingly or descendingly.
122+
* Sort the results by a property name ascendingly or descendingly. By default,
123+
* an ascending sort order will be used.
123124
*
124125
* *Reference: {@link http://goo.gl/mfegFR}*
125126
*
126-
* @param {string} order - Operator (+, -) + property to order by.
127+
* @param {string} property - Optional operator (+, -) and property to order by.
127128
* @return {module:datastore/query}
128129
*
129130
* @example
130131
* ```js
131132
* // Sort by size ascendingly.
132-
* var companiesAscending = companyQuery.order('+size');
133+
* var companiesAscending = companyQuery.order('size');
133134
*
134135
* // Sort by size descendingly.
135136
* var companiesDescending = companyQuery.order('-size');
136137
* ```
137138
*/
138-
Query.prototype.order = function(order) {
139+
Query.prototype.order = function(property) {
139140
var q = util.extend(this, new Query());
140-
var sign = order[0];
141-
var fieldName = order.substr(1);
142-
if (sign !== '-' && sign !== '+' ) {
143-
throw new Error(
144-
'Invalid order pattern. Expected "+fieldName" or "-fieldName".');
141+
var sign = '+';
142+
if (property[0] === '-' || property[0] === '+') {
143+
sign = property[0];
144+
property = property.substr(1);
145145
}
146146
q.orders = q.orders || [];
147-
q.orders.push({ name: fieldName, sign: sign });
147+
q.orders.push({ name: property, sign: sign });
148148
return q;
149149
};
150150

regression/datastore.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('datastore', function() {
284284
});
285285

286286
it('should order queries', function(done) {
287-
var q = ds.createQuery('Character').order('+appearances');
287+
var q = ds.createQuery('Character').order('appearances');
288288
ds.runQuery(q, function(err, entities) {
289289
assert.ifError(err);
290290
assert.equal(entities[0].data.name, characters[0].name);
@@ -313,7 +313,7 @@ describe('datastore', function() {
313313
var q = ds.createQuery('Character')
314314
.offset(2)
315315
.limit(3)
316-
.order('+appearances');
316+
.order('appearances');
317317
ds.runQuery(q, function(err, entities, secondQuery) {
318318
assert.ifError(err);
319319
assert.equal(entities.length, 3);
@@ -332,13 +332,13 @@ describe('datastore', function() {
332332
var q = ds.createQuery('Character')
333333
.offset(2)
334334
.limit(2)
335-
.order('+appearances');
335+
.order('appearances');
336336
ds.runQuery(q, function(err, entities, nextQuery) {
337337
assert.ifError(err);
338338
var startCursor = nextQuery.startVal;
339339
var cursorQuery =
340340
ds.createQuery('Character')
341-
.order('+appearances')
341+
.order('appearances')
342342
.start(startCursor);
343343
ds.runQuery(cursorQuery, function(err, secondEntities) {
344344
assert.ifError(err);

test/datastore/query.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ describe('Query', function() {
101101
assert.equal(query.orders[1].sign, '-');
102102
});
103103

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

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

0 commit comments

Comments
 (0)