Skip to content

Commit

Permalink
Merge pull request #65 from rakyll/createQuery
Browse files Browse the repository at this point in the history
datastore: Create query with optional namespace
  • Loading branch information
silvolu committed Jul 29, 2014
2 parents 1bf92e9 + d61ec2f commit 22a8436
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
25 changes: 10 additions & 15 deletions lib/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,20 @@ function Dataset(opts) {
/**
* Creates a query from the current dataset to query the specified
* kinds.
* @param {Array<String>} kinds A list of kinds to query.
* @return {Query}
*/
Dataset.prototype.createQuery = function(kinds) {
kinds = util.arrayize(kinds);
return new Query('', kinds);
};

/**
* Creates a query for collections identified with the specified
* namespace and kinds.
*
* Example usage:
* ds.createQueryNS('zoo', ['Lion', 'Chimp'])
* @param {string} ns Namespace to query entities from.
* @param {Array<object>} A list of kinds to query.
* ds.createQuery(['Lion', 'Chimp'])
* ds.createQuery('zoo', ['Lion', 'Chimp'])
*
* @param {string=} ns Optional namespace to query entities from.
* @param {Array<String>} kinds A list of kinds to query.
* @return {Query}
*/
Dataset.prototype.createQueryNS = function(ns, kinds) {
Dataset.prototype.createQuery = function(ns, kinds) {
if (!kinds) {
kinds = ns;
ns = '';
}
kinds = util.arrayize(kinds);
return new Query(ns, kinds);
};
Expand Down
4 changes: 2 additions & 2 deletions test/datastore.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ describe('Query', function() {
});

it('should use support custom namespaces', function(done) {
var q = ds.createQueryNS('ns', ['kind1']);
var q = ds.createQuery('ns', ['kind1']);
assert.equal(q.namespace, 'ns');
done();
});

it('should support querying multiple kinds', function(done) {
var q = ds.createQuery(['kind1', 'kind2']);
var qNS = ds.createQueryNS('ns', ['kind1', 'kind2']);
var qNS = ds.createQuery('ns', ['kind1', 'kind2']);

assert.strictEqual(q.namespace, '');
assert.equal(q.kinds[0], 'kind1');
Expand Down

0 comments on commit 22a8436

Please sign in to comment.