Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #38 return key/prop combined object. #75

Merged
merged 2 commits into from
Aug 1, 2014
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ TODO
Get operations require a valid key to retrieve the key identified entity from Datastore. Skip to the "Querying" section if you'd like to learn more about querying against Datastore.

~~~~ js
ds.get(['Company', 123], function(err, key, obj) {
ds.get(['Company', 123], function(err, entities) {

});
// alternatively, you can retrieve multiple entities at once.
ds.getAll([key1, key2, ...], function(err, keys, objs) {
ds.getAll([key1, key2, ...], function(err, entities) {

});
~~~~
Expand Down Expand Up @@ -150,7 +150,7 @@ also supported.
~~~~ js
// retrieves 5 companies
var q = ds.createQuery('Company').limit(5);
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
// nextQuery is not null if there are more results.
if (nextQuery) {
ds.runQuery(nextQuery, callback);
Expand Down
9 changes: 9 additions & 0 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ var keyToKeyProto = function(datasetId, key) {

module.exports.keyToKeyProto = keyToKeyProto;

module.exports.formatArray = function(results) {
return results.map(function (result) {
return {
key: keyFromKeyProto(result.entity.key),
data: entityFromEntityProto(result.entity)
};
});
};

This comment was marked as spam.

This comment was marked as spam.

module.exports.isKeyComplete = function(key) {
var proto = keyToKeyProto(null, key);
for (var i = 0; i < proto.path.length; i++) {
Expand Down
28 changes: 10 additions & 18 deletions lib/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ Transaction.prototype.finalize = function(callback) {
* @param {Function} callback
*/
Transaction.prototype.get = function(key, callback) {
this.getAll([key], function(err, keys, objs) {
if (err || objs.length < 0) {
this.getAll([key], function(err, results) {
if (err) {
return callback(err);
}
return callback(null, keys[0], objs[0]);
return callback(null, results[0]);
});
};

Expand All @@ -114,6 +114,7 @@ Transaction.prototype.get = function(key, callback) {
* @param {Function} callback
*/
Transaction.prototype.getAll = function(keys, callback) {
callback = callback || util.noop;
keysProto = [];
keys.forEach(function(k) {
keysProto.push(entity.keyToKeyProto(this.id, k));
Expand All @@ -127,12 +128,8 @@ Transaction.prototype.getAll = function(keys, callback) {
if (err) {
return callback(err);
}
var results = [], keys = [];
resp.found.forEach(function(f) {
keys.push(entity.keyFromKeyProto(f.entity.key));
results.push(entity.entityFromEntityProto(f.entity));
});
callback && callback(null, keys, results);

callback(null, entity.formatArray(resp.found));
});
};

Expand Down Expand Up @@ -245,7 +242,7 @@ Transaction.prototype.delAll = function(keys, callback) {
* available, a query to retrieve the next page is provided to
* the callback function. Example:
*
* t.runQuery(q, function(err, keys, objs, nextQuery) {
* t.runQuery(q, function(err, entities, nextQuery) {
* if (err) return;
* // if next page is available, retrieve more results
* if (nextQuery) {
Expand All @@ -256,6 +253,7 @@ Transaction.prototype.delAll = function(keys, callback) {
* @param {Function} opt_callback
*/
Transaction.prototype.runQuery = function(q, opt_callback) {
opt_callback = opt_callback || util.noop;
var req = {
readOptions: {
transaction: this.id
Expand All @@ -269,19 +267,13 @@ Transaction.prototype.runQuery = function(q, opt_callback) {
}
this.makeReq('runQuery', req, function(err, resp) {
if (err || !resp.batch || !resp.batch.entityResults) {
return opt_callback && opt_callback(err);
return opt_callback(err);
}
var results = resp.batch.entityResults,
keys = [], objs = [];
results.forEach(function(r) {
keys.push(entity.keyFromKeyProto(r.entity.key));
objs.push(entity.entityFromEntityProto(r.entity));
});
var nextQuery = null;
if (resp.batch.endCursor && resp.batch.endCursor != q.startVal) {
nextQuery = q.start(resp.batch.endCursor);
}
opt_callback && opt_callback(err, keys, objs, nextQuery);
opt_callback(null, entity.formatArray(resp.batch.entityResults), nextQuery);
});
};

Expand Down
90 changes: 45 additions & 45 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ describe('datastore', function() {
ds.save(['Post', postKeyName], post, function(err, key) {
if (err) return done(err);
assert.equal(key[1], postKeyName);
ds.get(['Post', postKeyName], function(err, key, obj) {
ds.get(['Post', postKeyName], function(err, entity) {
if (err) return done(err);
assert.deepEqual(obj, post);
assert.deepEqual(entity.data, post);
ds.del(['Post', postKeyName], function(err) {
if (err) return done(err);
done();
Expand All @@ -65,9 +65,9 @@ describe('datastore', function() {
ds.save(['Post', postKeyId], post, function(err, key) {
if (err) return done(err);
assert.equal(key[1], postKeyId);
ds.get(['Post', postKeyId], function(err, key, obj) {
ds.get(['Post', postKeyId], function(err, entity) {
if (err) return done(err);
assert.deepEqual(obj, post);
assert.deepEqual(entity.data, post);
ds.del(['Post', postKeyId], function(err) {
if (err) return done(err);
done();
Expand All @@ -90,9 +90,9 @@ describe('datastore', function() {
if (err) return done(err);
assert(key[1]);
var assignedId = key[1];
ds.get(['Post', assignedId], function(err, key, obj) {
ds.get(['Post', assignedId], function(err, entity) {
if (err) return done(err);
assert.deepEqual(obj, post);
assert.deepEqual(entity.data, post);
ds.del(['Post', assignedId], function(err) {
if (err) return done(err);
done();
Expand Down Expand Up @@ -126,9 +126,9 @@ describe('datastore', function() {
assert.equal(keys.length,2);
var firstKey = ['Post', keys[0][1]],
secondKey = ['Post', keys[1][1]];
ds.getAll([firstKey, secondKey], function(err, keys, objs) {
ds.getAll([firstKey, secondKey], function(err, entities) {
if (err) return done(err);
assert.equal(objs.length, 2);
assert.equal(entities.length, 2);
ds.delAll([firstKey, secondKey], function(err) {
if (err) return done(err);
done();
Expand Down Expand Up @@ -205,17 +205,17 @@ describe('datastore', function() {

it('should limit queries', function(done) {
var q = ds.createQuery('Character').limit(5);
ds.runQuery(q, function(err, keys, objs, secondQuery) {
ds.runQuery(q, function(err, firstEntities, secondQuery) {
if (err) return done(err);
assert.equal(objs.length, 5);
assert.equal(firstEntities.length, 5);
assert(secondQuery);
ds.runQuery(secondQuery, function(err, keys, objs, thirdQuery) {
ds.runQuery(secondQuery, function(err, secondEntities, thirdQuery) {
if (err) return done(err);
assert.equal(objs.length, 3);
assert.equal(secondEntities.length, 3);
// TODO(silvano): it currently requires an additional request that brings
// an empty page and a null query
//assert.equal(thirdQuery, null)
ds.runQuery(thirdQuery, function(err, keys, objs, fourthQuery) {
ds.runQuery(thirdQuery, function(err, thirdEntities, fourthQuery) {
if (err) return done(err);
assert.equal(fourthQuery, null);
done();
Expand All @@ -227,9 +227,9 @@ describe('datastore', function() {
it('should filter queries with simple indexes', function(done) {
var q = ds.createQuery('Character')
.filter('appearances >=', 20);
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
if (err) return done(err);
assert.equal(objs.length, 6);
assert.equal(entities.length, 6);
done();
});
});
Expand All @@ -238,42 +238,42 @@ describe('datastore', function() {
var q = ds.createQuery('Character')
.filter('family =', 'Stark')
.filter('appearances >=', 20);
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
if (err) return done(err);
assert.equal(objs.length, 6);
assert.equal(entities.length, 6);
done();
});
});

it('should filter by ancestor', function(done) {
var q = ds.createQuery('Character').hasAncestor(['Character', 'Eddard']);
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
if (err) return done(err);
assert.equal(objs.length, 5);
assert.equal(entities.length, 5);
done();
});
});

it('should order queries', function(done) {
var q = ds.createQuery('Character').order('+appearances');
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
if (err) return done(err);
assert.equal(objs[0].name, characters[0].name);
assert.equal(objs[7].name, characters[3].name);
assert.equal(entities[0].data.name, characters[0].name);
assert.equal(entities[7].data.name, characters[3].name);
done();
});
});

it('should select projections', function(done) {
var q = ds.createQuery('Character')
.select(['name', 'family']);
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
if (err) return done(err);
assert.deepEqual(objs[0], {
assert.deepEqual(entities[0].data, {
name: 'Arya',
family: 'Stark'
});
assert.deepEqual(objs[8], {
assert.deepEqual(entities[8].data, {
name: 'Sansa',
family: 'Stark'
});
Expand All @@ -286,15 +286,15 @@ describe('datastore', function() {
.offset(2)
.limit(3)
.order('+appearances');
ds.runQuery(q, function(err, keys, objs, secondQuery) {
ds.runQuery(q, function(err, entities, secondQuery) {
if (err) return done(err);
assert.equal(objs.length, 3);
assert.equal(objs[0].name, 'Robb');
assert.equal(objs[2].name, 'Catelyn');
ds.runQuery(secondQuery, function(err, keys, objs, thirdQuery) {
assert.equal(objs.length, 3);
assert.equal(objs[0].name, 'Sansa');
assert.equal(objs[2].name, 'Arya');
assert.equal(entities.length, 3);
assert.equal(entities[0].data.name, 'Robb');
assert.equal(entities[2].data.name, 'Catelyn');
ds.runQuery(secondQuery, function(err, secondEntities, thirdQuery) {
assert.equal(secondEntities.length, 3);
assert.equal(secondEntities[0].data.name, 'Sansa');
assert.equal(secondEntities[2].data.name, 'Arya');
done();
});
});
Expand All @@ -305,17 +305,17 @@ describe('datastore', function() {
.offset(2)
.limit(2)
.order('+appearances');
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
if (err) return done(err);
var startCursor = nextQuery.startVal;
var cursorQuery = ds.createQuery('Character')
.order('+appearances')
.start(startCursor);
ds.runQuery(cursorQuery, function(err, keys, objs, nextQuery) {
ds.runQuery(cursorQuery, function(err, secondEntities, nextQuery) {
if (err) return done(err);
assert.equal(objs.length, 4);
assert.equal(objs[0].name, 'Catelyn');
assert.equal(objs[3].name, 'Arya');
assert.equal(secondEntities.length, 4);
assert.equal(secondEntities[0].data.name, 'Catelyn');
assert.equal(secondEntities[3].data.name, 'Arya');
done();
});
});
Expand All @@ -324,9 +324,9 @@ describe('datastore', function() {
it('should group queries', function(done) {
var q = ds.createQuery('Character')
.groupBy('alive');
ds.runQuery(q, function(err, keys, objs, nextQuery) {
ds.runQuery(q, function(err, entities, nextQuery) {
if (err) return done(err);
assert.equal(objs.length, 2);
assert.equal(entities.length, 2);
done();
});
})
Expand All @@ -350,9 +350,9 @@ describe('datastore', function() {
'url': 'www.google.com'
};
ds.runInTransaction(function(t, tDone) {
ds.get(key, function(err, keyRes, objRes) {
ds.get(key, function(err, entity) {
if (err) return done(err);
if (objRes) {
if (entity) {
tDone();
return;
} else {
Expand All @@ -365,10 +365,10 @@ describe('datastore', function() {
});
}, function(err) {
if (err) throw (err);
ds.get(key, function(err, keyRes, objRes) {
ds.get(key, function(err, entity) {
if (err) return done(err);
assert.deepEqual(objRes, obj);
ds.del(keyRes, function(err) {
assert.deepEqual(entity.data, obj);
ds.del(entity.key, function(err) {
if (err) return done(err);
done();
})
Expand Down
Loading