Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions lib/datastore/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
'use strict';

var arrify = require('arrify');
var extend = require('extend');

/**
* Build a Query object.
Expand Down Expand Up @@ -79,9 +78,8 @@ function Query(namespace, kinds) {
* @return {module:datastore/query}
*/
Query.prototype.autoPaginate = function(autoPaginateVal) {
var query = extend(true, new Query(), this);
query.autoPaginateVal = autoPaginateVal !== false;
return query;
this.autoPaginateVal = autoPaginateVal !== false;
return this;
};

/**
Expand Down Expand Up @@ -109,18 +107,16 @@ Query.prototype.autoPaginate = function(autoPaginateVal) {
*/
Query.prototype.filter = function(filter, value) {
// TODO: Add filter validation.
var query = extend(true, new Query(), this);
var operatorRegex = /[><=]/g;
filter = filter.trim();
var fieldName = filter.replace(operatorRegex, '').trim();
var op = filter.substr(fieldName.length, filter.length).trim();
query.filters = query.filters || [];
query.filters.push({
this.filters.push({
name: fieldName,
op: op,
val: value
});
return query;
return this;
};

/**
Expand All @@ -135,9 +131,8 @@ Query.prototype.filter = function(filter, value) {
* var ancestoryQuery = query.hasAncestor(dataset.key(['Parent', 123]));
*/
Query.prototype.hasAncestor = function(key) {
var query = extend(true, new Query(), this);
query.filters.push({ name: '__key__', op: 'HAS_ANCESTOR', val: key });
return query;
this.filters.push({ name: '__key__', op: 'HAS_ANCESTOR', val: key });
return this;
};

/**
Expand All @@ -157,15 +152,13 @@ Query.prototype.hasAncestor = function(key) {
* var companiesDescending = companyQuery.order('-size');
*/
Query.prototype.order = function(property) {
var query = extend(true, new Query(), this);
var sign = '+';
if (property[0] === '-' || property[0] === '+') {
sign = property[0];
property = property.substr(1);
}
query.orders = query.orders || [];
query.orders.push({ name: property, sign: sign });
return query;
this.orders.push({ name: property, sign: sign });
return this;
};

/**
Expand All @@ -178,9 +171,8 @@ Query.prototype.order = function(property) {
* var groupedQuery = companyQuery.groupBy(['name', 'size']);
*/
Query.prototype.groupBy = function(fieldNames) {
var query = extend(true, new Query(), this);
query.groupByVal = arrify(fieldNames);
return query;
this.groupByVal = arrify(fieldNames);
return this;
};

/**
Expand All @@ -202,9 +194,8 @@ Query.prototype.groupBy = function(fieldNames) {
* var selectQuery = companyQuery.select(['name', 'size']);
*/
Query.prototype.select = function(fieldNames) {
var query = extend(true, new Query(), this);
query.selectVal = arrify(fieldNames);
return query;
this.selectVal = arrify(fieldNames);
return this;
};

/**
Expand All @@ -222,9 +213,8 @@ Query.prototype.select = function(fieldNames) {
* var startQuery = companyQuery.start(cursorToken);
*/
Query.prototype.start = function(start) {
var query = extend(true, new Query(), this);
query.startVal = start;
return query;
this.startVal = start;
return this;
};

/**
Expand All @@ -242,9 +232,8 @@ Query.prototype.start = function(start) {
* var endQuery = companyQuery.end(cursorToken);
*/
Query.prototype.end = function(end) {
var query = extend(true, new Query(), this);
query.endVal = end;
return query;
this.endVal = end;
return this;
};

/**
Expand All @@ -260,9 +249,8 @@ Query.prototype.end = function(end) {
* var limitQuery = companyQuery.limit(10);
*/
Query.prototype.limit = function(n) {
var query = extend(true, new Query(), this);
query.limitVal = n;
return query;
this.limitVal = n;
return this;
};

/**
Expand All @@ -278,9 +266,8 @@ Query.prototype.limit = function(n) {
* var offsetQuery = companyQuery.offset(100);
*/
Query.prototype.offset = function(n) {
var query = extend(true, new Query(), this);
query.offsetVal = n;
return query;
this.offsetVal = n;
return this;
};

module.exports = Query;
11 changes: 10 additions & 1 deletion lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var request = require('request').defaults({
});
var split = require('split-array-stream');
var through = require('through2');
var extend = require('extend');

/**
* @type {module:datastore/entity}
Expand All @@ -45,6 +46,12 @@ var entity = require('./entity.js');
*/
var pb = require('./pb.js');

/**
* @type {module:datastore/query}
* @private
*/
var Query = require('./query');

/**
* @type {module:common/streamrouter}
* @private
Expand Down Expand Up @@ -620,7 +627,9 @@ DatastoreRequest.prototype.runQuery = function(query, callback) {

if (resp.batch.end_cursor && entities.length > 0) {
var endCursor = resp.batch.end_cursor.toBase64();
nextQuery = query.start(endCursor).offset(0);

nextQuery = extend(true, new Query(), query);
nextQuery.start(endCursor).offset(0);
}

callback(null, entities, nextQuery, resp);
Expand Down
78 changes: 30 additions & 48 deletions test/datastore/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ describe('Query', function() {
assert.strictEqual(query.autoPaginateVal, true);
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.autoPaginate(false);
var nextQuery = query.autoPaginate(false);

assert.notEqual(query.autoPaginateVal, newQuery.autoPaginateVal);
assert.strictEqual(query, nextQuery);
});

});
Expand Down Expand Up @@ -136,20 +136,11 @@ describe('Query', function() {
assert.equal(query.filters[0].op, '<');
});

it('should create the filters property if it is a falsy value', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var nextQuery = query.filter('count <', 5);

query.filters = false;
query = query.filter('count <=', 5);

assert.equal(query.filters.length, 1);
});

it('should return a new query', function() {
var query = new Query(['kind1']);
var newQuery = query.filter('count <', 5);

assert.notDeepEqual(query.filters, newQuery.filters);
assert.strictEqual(query, nextQuery);
});

});
Expand All @@ -164,11 +155,11 @@ describe('Query', function() {
assert.deepEqual(query.filters[0].val, ['kind2', 123]);
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.hasAncestor(['kind2', 123]);
var nextQuery = query.hasAncestor(['kind2', 123]);

assert.notDeepEqual(query.filters, newQuery.filters);
assert.strictEqual(query, nextQuery);
});

});
Expand Down Expand Up @@ -207,20 +198,11 @@ describe('Query', function() {
assert.equal(query.orders[1].sign, '-');
});

it('should create the orders property if it is a falsy value', function() {
var query = new Query(['kind1']);

query.orders = false;
query = query.order('size');

assert.equal(query.orders.length, 1);
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.order('count');
var nextQuery = query.order('name');

assert.notDeepEqual(query.orders, newQuery.orders);
assert.strictEqual(query, nextQuery);
});

});
Expand All @@ -239,11 +221,11 @@ describe('Query', function() {
assert.deepEqual(query.groupByVal, ['name']);
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.groupBy(['name']);
var nextQuery = query.groupBy(['name', 'size']);

assert.notDeepEqual(query.groupByVal, newQuery.groupByVal);
assert.strictEqual(query, nextQuery);
});

});
Expand All @@ -262,11 +244,11 @@ describe('Query', function() {
assert.deepEqual(query.selectVal, ['name']);
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.select('name');
var nextQuery = query.select(['name', 'size']);

assert.notDeepEqual(query.selectVal, newQuery.selectVal);
assert.strictEqual(query, nextQuery);
});

});
Expand All @@ -279,11 +261,11 @@ describe('Query', function() {
assert.equal(query.startVal, 'X');
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.start('Y');
var nextQuery = query.start('X');

assert.notEqual(query.startVal, newQuery.startVal);
assert.strictEqual(query, nextQuery);
});

});
Expand All @@ -296,11 +278,11 @@ describe('Query', function() {
assert.equal(query.endVal, 'Z');
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.end('W');
var nextQuery = query.end('Z');

assert.notEqual(query.endVal, newQuery.endVal);
assert.strictEqual(query, nextQuery);
});

});
Expand All @@ -313,11 +295,11 @@ describe('Query', function() {
assert.strictEqual(query.limitVal, 20);
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.limit(20);
var nextQuery = query.limit(20);

assert.notEqual(query.limitVal, newQuery.limitVal);
assert.strictEqual(query, nextQuery);
});

});
Expand All @@ -330,11 +312,11 @@ describe('Query', function() {
assert.strictEqual(query.offsetVal, 100);
});

it('should return a new query', function() {
it('should return the query instance', function() {
var query = new Query(['kind1']);
var newQuery = query.offset(10);
var nextQuery = query.offset(100);

assert.notEqual(query.offsetVal, newQuery.offsetVal);
assert.strictEqual(query, nextQuery);
});

});
Expand Down