Skip to content

Commit

Permalink
fixes googleapis#38 datastore.get returns key/prop combined object.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jul 31, 2014
1 parent 83f1a37 commit 96ef76d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 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
17 changes: 9 additions & 8 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 || results.length < 0) {
return callback(err);
}
return callback(null, keys[0], objs[0]);
return callback(null, results[0]);
});
};

Expand All @@ -127,12 +127,13 @@ 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));
var results = resp.found.map(function(f) {
return {
key: entity.keyFromKeyProto(f.entity.key),
properties: entity.entityFromEntityProto(f.entity)
};
});
callback && callback(null, keys, results);
callback && callback(null, results);
});
};

Expand Down
26 changes: 13 additions & 13 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.properties, 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.properties, 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.properties, 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 @@ -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.properties, obj);
ds.del(entity.key, function(err) {
if (err) return done(err);
done();
})
Expand Down
31 changes: 17 additions & 14 deletions test/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ describe('Dataset', function() {
assert.equal(proto.keys.length, 1);
callback(null, mockResp_get);
};
ds.get(['Kind', 123], function(err, key, obj) {
assert.deepEqual(key, ['Kind', 5732568548769792]);
assert.strictEqual(obj.name, 'Burcu');
assert.deepEqual(obj.bytes, new Buffer('hello'));
assert.strictEqual(obj.done, false);
assert.deepEqual(obj.total, 6.7);
assert.strictEqual(obj.createdat.getTime(), 978307200000);
ds.get(['Kind', 123], function(err, entity) {
var properties = entity.properties;
assert.deepEqual(entity.key, ['Kind', 5732568548769792]);
assert.strictEqual(properties.name, 'Burcu');
assert.deepEqual(properties.bytes, new Buffer('hello'));
assert.strictEqual(properties.done, false);
assert.deepEqual(properties.total, 6.7);
assert.strictEqual(properties.createdat.getTime(), 978307200000);
done();
});
});
Expand All @@ -108,13 +109,15 @@ describe('Dataset', function() {
callback(null, mockResp_get);
};
ds.getAll([
['Kind', 123]], function(err, keys, objs) {
assert.deepEqual(keys[0], ['Kind', 5732568548769792]);
assert.strictEqual(objs[0].name, 'Burcu');
assert.deepEqual(objs[0].bytes, new Buffer('hello'));
assert.strictEqual(objs[0].done, false);
assert.deepEqual(objs[0].total, 6.7);
assert.strictEqual(objs[0].createdat.getTime(), 978307200000);
['Kind', 123]], function(err, entities) {
var entity = entities[0];
var properties = entity.properties;
assert.deepEqual(entity.key, ['Kind', 5732568548769792]);
assert.strictEqual(properties.name, 'Burcu');
assert.deepEqual(properties.bytes, new Buffer('hello'));
assert.strictEqual(properties.done, false);
assert.deepEqual(properties.total, 6.7);
assert.strictEqual(properties.createdat.getTime(), 978307200000);
done();
});
});
Expand Down

0 comments on commit 96ef76d

Please sign in to comment.